我有一个如下的 XML。对于每个测试,我需要对 cat 和 title 和 group 进行排序。对于 cat 值 abc,我需要获取标题并设置值。任何集合值都符合在集合节点内按升序添加标题值的需要。在这里,我确实对 cat 值进行了硬编码。但在我的场景中,cat 值是从外部源获取的。
<?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>
我需要如下输出:
<?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>
我的变换如下所示:
<?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>
有人可以建议我如何生成所需的输出吗?