我有一个如下的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>
谁能建议我如何产生所需的输出?
好的,我想我已经了解了你们的要求。如何:
<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>
这里$catValue
被指定为参数,因此您可以从外部传递不同的值。当在示例XML上运行此命令时,结果是:
<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>
更短更简单:
<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>
当对提供的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>
如果您想为 cat
的所有区分值产生类似的结果,则仍然是简短的:
<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>
<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>