在XSLT中按范围限制输出
我正在创建一个XSLT,并且我想要选择一个特定的节点,前提是它的一个子元素的值在一个范围之间。使用xsl文件中的参数指定范围 XML文件类似于在XSLT中按范围限制输出,xslt,xpath,xslt-2.0,Xslt,Xpath,Xslt 2.0,我正在创建一个XSLT,并且我想要选择一个特定的节点,前提是它的一个子元素的值在一个范围之间。使用xsl文件中的参数指定范围 XML文件类似于 <root> <org> <name>foo</name> <chief>100</chief> </org> <org parent="foo"> <name>foo2</name> <chief>1
<root>
<org>
<name>foo</name>
<chief>100</chief>
</org>
<org parent="foo">
<name>foo2</name>
<chief>106</chief>
</org>
</root>
福
100
食物2
106
到目前为止,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>
99
105
我想限制处理//org[chief < $fromRange and not(@parent)]
|//org[chief > $toRange and not(@parent)]
此表达式将排除fromRange
和toRange
指定范围内的所有节点
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<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[chief < $fromRange and not(@parent)]|//org[chief > $toRange and not(@parent)]"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
99
105
我想选择一个特定的节点,
仅当其子元素的
值介于一个范围之间。范围是
使用中的参数指定
xsl文件
我还想知道
节点不应具有paren
t
属性以及范围
使用此表达式作为
的选择属性的值:
org[not(@parent) and chief >= $fromRange and not(chief > $toRange)]
在XSLT2.0中,在匹配模式中使用变量/参数是合法的
因此,可以这样写:
<xsl:template match=
"org[@parent or not(chief >= $fromRange ) or chief > $toRange]"/>
这比XSLT 1.0解决方案要好,因为它更具“推送风格”。我还希望限制节点不应在rangeGood问题中包含父属性(+1)。关于两个完整的解决方案,请参见我的答案:XSLT1.0和XSLT2.0:),我认为OP需要范围节点。
<xsl:template match="/">
<orgo>
<xsl:apply-templates/>
</orgo>
</xsl:template>