我希望选择满足特定条件的节点列表,在处理这些节点之后,我希望选择其余的节点。我如何在XSLT和XPath中做到这一点。
下面是一个场景,我有这个xml
<books>
<book name="Basic XML">
<type>Educational</type>
<grouping>A</grouping>
</book>
<book name="Basic XML">
<type>Tutorial</type>
<grouping>A</grouping>
</book>
<book name="Basic XSLT">
<type>Educational</type>
<grouping>A</grouping>
</book>
<book name="Basic XSLT">
<type>Tutorial</type>
<grouping>A</grouping>
</book>
<book name="Basic Java">
<type>Educational</type>
<grouping>A</grouping>
</book>
<book name="Basic Java">
<type>Tutorial</type>
<grouping>A</grouping>
</book>
<book name="Web Service">
<type>Educational</type>
<grouping>A</grouping>
</book>
<book name="C Programming">
<type>Educational</type>
<grouping>A</grouping>
</book>
</books><type>为“<book>”的所有教程节点,下面是输出<books>
<book name="Basic XML">
<type>Tutorial</type>
<grouping>A</grouping>
</book>
<book name="Basic XSLT">
<type>Tutorial</type>
<grouping>A</grouping>
</book>
<book name="Basic Java">
<type>Tutorial</type>
<grouping>A</grouping>
</book>
</books>然后,
<book>节点,这些节点的<type>不是"Tutorial“,并且与在#1中选择的节点的@name不同,输出仅为:<books>
<book name="Web Service">
<type>Educational</type>
<grouping>A</grouping>
</book>
<book name="C Programming">
<type>Educational</type>
<grouping>A</grouping>
</book>
</books>发布于 2012-03-27 09:46:47
对于第一个查询:
<xsl:apply-templates select="/books/book[type='Tutorial']"/>对于第二个查询:
<xsl:apply-templates select="/books/book[type!='Tutorial']"/>然后,您将需要适当的模板来处理它们:
<xsl:template match="/books/book[type='Tutorial']">
Do Something...
</xsl:template>最后一步是检查当前节点是否也有教程节点
<xsl:template match="/books/book[type!='Tutorial']">
<xsl:variable name="bookname">
<xsl:value-of select="@name"/>
</xsl:variable>
<xsl:if test="count('/books/book[@name=$bookname and type='Tutorial']')=0">
Do Something...
</xsl:if>
</xsl:template>发布于 2012-03-27 11:40:32
这里的是一个完整的解决方案
<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:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:variable name="vTutBooks" select="book[type = 'Tutorial']"/>
<books>
<xsl:apply-templates select="$vTutBooks"/>
</books>
<books>
<xsl:apply-templates select=
"book[not(type = 'Tutorial')
and
not(@name = $vTutBooks/@name)
]"/>
</books>
</xsl:template>
</xsl:stylesheet>在所提供的XML文档上应用此转换时为
<books>
<book name="Basic XML">
<type>Educational</type>
<grouping>A</grouping>
</book>
<book name="Basic XML">
<type>Tutorial</type>
<grouping>A</grouping>
</book>
<book name="Basic XSLT">
<type>Educational</type>
<grouping>A</grouping>
</book>
<book name="Basic XSLT">
<type>Tutorial</type>
<grouping>A</grouping>
</book>
<book name="Basic Java">
<type>Educational</type>
<grouping>A</grouping>
</book>
<book name="Basic Java">
<type>Tutorial</type>
<grouping>A</grouping>
</book>
<book name="Web Service">
<type>Educational</type>
<grouping>A</grouping>
</book>
<book name="C Programming">
<type>Educational</type>
<grouping>A</grouping>
</book>
</books>生成所需的正确结果
<books>
<book name="Basic XML">
<type>Tutorial</type>
<grouping>A</grouping>
</book>
<book name="Basic XSLT">
<type>Tutorial</type>
<grouping>A</grouping>
</book>
<book name="Basic Java">
<type>Tutorial</type>
<grouping>A</grouping>
</book>
</books>
<books>
<book name="Web Service">
<type>Educational</type>
<grouping>A</grouping>
</book>
<book name="C Programming">
<type>Educational</type>
<grouping>A</grouping>
</book>
</books>https://stackoverflow.com/questions/9882239
复制相似问题