2010-10-03 59 views
1

我正在创建一个XSLT,并且我想选择一个特定的节点,只要其中一个子元素的值在一个范围之间。范围将使用xsl文件中的参数指定。限制按范围在XSLT输出

的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> 

我想从正在处理其<首席限制的组织节点>节点的值不在范围内

+0

我也想要限制,该节点不应该有一个父属性以及范围 – charudatta 2010-10-03 21:16:47

+0

好问题再次(+1)。看到我的答案有两个完整的解决方案:XSLT 1.0和XSLT 2.0 :) – 2010-10-03 23:51:02

回答

0
//org[chief &lt; $fromRange and not(@parent)] 
    |//org[chief > $toRange and not(@parent)] 

该表达式将排除0范围内的所有节点和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 &lt; $fromRange and not(@parent)]|//org[chief > $toRange and not(@parent)]"/> 
    </xsl:element> 
    </xsl:template> 

</xsl:stylesheet> 
+0

我认为OP希望在范围节点中。 – 2010-10-04 15:35:31

3

我想选择一个特定的节点, 只有当它的子元素的 价值之一,是一个范围之间。范围是 ,使用 xsl文件中的参数指定。

我也想了 节点不应该有paren吨 属性与范围

使用此表达的<xsl:apply-templates>select属性的值一起的限制:

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更好的解决方案,因为它更“推式”。

+0

对模式差异的变量/参数进行+1。 – 2010-10-04 15:36:23

+0

优雅地完成。 – 2010-11-08 15:00:31