2010-10-03 55 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