对于每个xml,使用xslt对xml进行排序和分组
我有一个XML,如下所示。对于每个测试,我需要对cat、title和group进行排序。 对于cat值abc,我需要获取标题和设置值。 任何集合值都符合按升序在集合节点内添加标题值的需要。在这里,我硬编码了cat值。但在我的场景中,cat值是从外部源获取的对于每个xml,使用xslt对xml进行排序和分组,xslt,xslt-1.0,xslt-grouping,Xslt,Xslt 1.0,Xslt Grouping,我有一个XML,如下所示。对于每个测试,我需要对cat、title和group进行排序。 对于cat值abc,我需要获取标题和设置值。 任何集合值都符合按升序在集合节点内添加标题值的需要。在这里,我硬编码了cat值。但在我的场景中,cat值是从外部源获取的 <?xml version="1.0" encoding="utf-8" ?> <compound> <test> <title>k</title> <ce
<?xml version="1.0" encoding="utf-8" ?>
<compound>
<test>
<title>k</title>
<cetval>
<cat>abc</cat>
<cat>fcg</cat>
</cetval>
<value>1</value>
<set>
<color>blue</color>
</set>
</test>
<test>
<title>p</title>
<cetval>
<cat>fcg</cat>
<cat>klm</cat>
</cetval>
<value>10</value>
<set>
<color>pink</color>
</set>
</test>
<test>
<title>j</title>
<cetval>
<cat>fcg</cat>
<cat>klm</cat>
<cat>abc</cat>
</cetval>
<value>484</value>
<set>
<color>yellow</color>
</set>
</test>
<test>
<title>v</title>
<cetval>
<cat>dji</cat>
<cat>kfjsdlk</cat>
</cetval>
<value>93</value>
</test>
<test>
<title>r</title>
<cetval>
<cat>deiu</cat>
<cat>kfjdf</cat>
<cat>abc</cat>
</cetval>
<value>10</value>
<set>
<color>blue</color>
</set>
<test>
<title>i</title>
<cetval>
<cat>fcg</cat>
<cat>klm</cat>
<cat>abc</cat>
</cetval>
<value>10</value>
</test>
<test>
<title>w</title>
<cetval>
<cat>diweif</cat>
<cat>fjf</cat>
<cat>abc</cat>
</cetval>
<value>10</value>
<set>
<color>yellow</color>
</set>
</test>
</test>
</compound>
K
abc
fcg
1.
蓝色
P
fcg
荷航
10
粉红色
J
fcg
荷航
abc
484
黄色的
v
dji
kfjsdlk
93
R
德尤
kfjdf
abc
10
蓝色
我
fcg
荷航
abc
10
W
迪维夫
fjf
abc
10
黄色的
我需要如下输出:
<?xml version="1.0" encoding="utf-8" ?>
<compound>
<cat>abc</cat>
<set>
<color>blue</color>
<title>k</title>
<title>r</title>
</set>
<set>
<color>yellow</color>
<title>j</title>
<title>w</title>
</set>
<title>i</title>
</compound>
abc
蓝色
K
R
黄色的
J
W
我
我的转换如下所示:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="compound/test">
<xsl:sort select="./cetval/cat" order="ascending"/>
<xsl:sort select="./title" order="ascending"/>
<xsl:for-each select="./cetval/cat">
<xsl:choose>
<xsl:when test="./cat = 'abc' > 0">
<xsl:value-of select="ancestor::test/title"/>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
有谁能建议我如何生成所需的输出吗?好的,我想我已经理解了您的要求。这是怎么回事
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:key name="kColor" match="test" use="set/color" />
<xsl:param name="catValue" select="'abc'" />
<xsl:template match="/*">
<xsl:copy>
<cat>
<xsl:value-of select ="$catValue"/>
</cat>
<xsl:apply-templates
select="//test[generate-id() =
generate-id(
key('kColor', set/color)
[cetval/cat = $catValue][1]
)
]" />
<xsl:apply-templates
select="//test[cetval/cat = $catValue and not(set/color)]
/title">
<xsl:sort select="."/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="test">
<set>
<xsl:apply-templates select="set/color" />
<xsl:apply-templates
select="key('kColor', set/color)[cetval/cat = $catValue]/title">
<xsl:sort select="." />
</xsl:apply-templates>
</set>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
非常简短和简单:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kTestByCat" match="test" use="cetval/cat"/>
<xsl:key name="kTestByColor" match="test[cetval/cat='abc']"
use="set/color"/>
<xsl:template match="/">
<compound>
<cat>abc</cat>
<xsl:apply-templates select=
"/*/*[generate-id()
=
generate-id(key('kTestByColor',set/color)[1])]"/>
</compound>
</xsl:template>
<xsl:template match="test">
<set>
<color><xsl:value-of select="set/color"/></color>
<xsl:copy-of select="key('kTestByColor',set/color)/title"/>
</set>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kCatByVal" match="cat" use="."/>
<xsl:key name="kColorByVal" match="color" use="."/>
<xsl:key name="kTestByCat" match="test" use="cetval/cat"/>
<xsl:variable name="vDistinctColors" select=
"/*/*/set/color[generate-id()=generate-id(key('kColorByVal',.)[1])]"/>
<xsl:template match="/">
<xsl:apply-templates select=
"/*/*/cetval/cat
[generate-id()
= generate-id(key('kCatByVal',.)[1])]"/>
</xsl:template>
<xsl:template match="cat">
<xsl:variable name="vcurCat" select="."/>
<compound>
<cat><xsl:apply-templates/></cat>
<xsl:for-each select="$vDistinctColors">
<xsl:apply-templates select=
"(key('kCatByVal',$vcurCat)/../../set/color[.=current()])[1]">
<xsl:with-param name="pCat" select="$vcurCat"/>
</xsl:apply-templates>
</xsl:for-each>
</compound>
</xsl:template>
<xsl:template match="color">
<xsl:param name="pCat"/>
<xsl:copy-of select="."/>
<xsl:copy-of select="key('kTestByCat',$pCat)[set/color=current()]/title"/>
</xsl:template>
</xsl:stylesheet>
应用于相同的XML文档(如上)时,现在的结果是:
<compound>
<test>
<title>k</title>
<cetval>
<cat>abc</cat>
<cat>fcg</cat>
</cetval>
<value>1</value>
<set>
<color>blue</color>
</set>
</test>
<test>
<title>p</title>
<cetval>
<cat>fcg</cat>
<cat>klm</cat>
</cetval>
<value>10</value>
<set>
<color>pink</color>
</set>
</test>
<test>
<title>j</title>
<cetval>
<cat>fcg</cat>
<cat>klm</cat>
<cat>abc</cat>
</cetval>
<value>484</value>
<set>
<color>yellow</color>
</set>
</test>
<test>
<title>v</title>
<cetval>
<cat>dji</cat>
<cat>kfjsdlk</cat>
</cetval>
<value>93</value>
</test>
<test>
<title>r</title>
<cetval>
<cat>deiu</cat>
<cat>kfjdf</cat>
<cat>abc</cat>
</cetval>
<value>10</value>
<set>
<color>blue</color>
</set>
<test>
<title>i</title>
<cetval>
<cat>fcg</cat>
<cat>klm</cat>
<cat>abc</cat>
</cetval>
<value>10</value>
</test>
<test>
<title>w</title>
<cetval>
<cat>diweif</cat>
<cat>fjf</cat>
<cat>abc</cat>
</cetval>
<value>10</value>
<set>
<color>yellow</color>
</set>
</test>
</test>
</compound>
<compound>
<cat>abc</cat>
<set>
<color>blue</color>
<title>k</title>
<title>r</title>
</set>
<set>
<color>yellow</color>
<title>j</title>
<title>w</title>
</set>
</compound>
<compound>
<cat>abc</cat>
<color>blue</color>
<title>k</title>
<title>r</title>
<color>yellow</color>
<title>j</title>
<title>w</title>
</compound>
<compound>
<cat>fcg</cat>
<color>blue</color>
<title>k</title>
<color>pink</color>
<title>p</title>
<color>yellow</color>
<title>j</title>
</compound>
<compound>
<cat>klm</cat>
<color>pink</color>
<title>p</title>
<color>yellow</color>
<title>j</title>
</compound>
<compound>
<cat>dji</cat>
</compound>
<compound>
<cat>kfjsdlk</cat>
</compound>
<compound>
<cat>deiu</cat>
<color>blue</color>
<title>r</title>
</compound>
<compound>
<cat>kfjdf</cat>
<color>blue</color>
<title>r</title>
</compound>
abc
蓝色
K
R
黄色的
J
W
fcg
蓝色
K
粉红色
P
黄色的
J
荷航
粉红色
P
黄色的
J
dji
kfjsdlk
德尤
蓝色
R
kfjdf
蓝色
R
蓝色的标题不应该是“k”和“r”,而不是“k”和“p”?蒂姆,我已经编辑了我的输出。I
的选择标准是什么?如果cat=abc,我需要选择标题…@user1490088:将其标记为正确答案(单击答案左侧的复选标记)
<compound>
<cat>abc</cat>
<color>blue</color>
<title>k</title>
<title>r</title>
<color>yellow</color>
<title>j</title>
<title>w</title>
</compound>
<compound>
<cat>fcg</cat>
<color>blue</color>
<title>k</title>
<color>pink</color>
<title>p</title>
<color>yellow</color>
<title>j</title>
</compound>
<compound>
<cat>klm</cat>
<color>pink</color>
<title>p</title>
<color>yellow</color>
<title>j</title>
</compound>
<compound>
<cat>dji</cat>
</compound>
<compound>
<cat>kfjsdlk</cat>
</compound>
<compound>
<cat>deiu</cat>
<color>blue</color>
<title>r</title>
</compound>
<compound>
<cat>kfjdf</cat>
<color>blue</color>
<title>r</title>
</compound>