我正在创建XSLT,并且仅当其子元素值之一在范围之间时,才想选择一个特定节点。该范围将使用xsl文件中的参数指定。
XML文件就像
<root>
<org>
<name>foo</name>
<chief>100</chief>
</org>
<org parent="foo">
<name>foo2</name>
<chief>106</chief>
</org>
</root>
到目前为止,XSLT是
<xsl:param name="fromRange">99</xsl:param>
<xsl:param name="toRange">105</xsl:param>
<xsl:template match="/">
<xsl:element name="orgo">
<xsl:apply-templates select="//org[not(@parent)]"/>
</xsl:element>
</xsl:template>
我想限制正在处理其
最佳答案
我想选择一个特定的节点,
仅当其子元素的
值在一个范围之间。范围是
使用中的参数指定
xsl文件。
我也想要限制
节点不应具有paren
t
属性和范围
将此表达式用作select
的<xsl:apply-templates>
属性的值:
org[not(@parent) and chief >= $fromRange and not(chief > $toRange)]
在XSLT 2.0中,在匹配模式中包含变量/参数是合法的。
因此,可以这样写:
<xsl:template match=
"org[@parent or not(chief >= $fromRange ) or chief > $toRange]"/>
因此有效地将所有这些
org
元素排除在处理之外。然后,与文档节点匹配的模板就是:
<xsl:template match="/">
<orgo>
<xsl:apply-templates/>
</orgo>
</xsl:template>
这比XSLT 1.0解决方案更好,因为它更“推式”。
关于xslt - 在XSLT中按范围限制输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3851621/