2013-05-25 36 views
0

我有這個源XML樹:XPath表達式來排除一些子節點

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <foo> 
    <bar> 
     <baz> 
     <item> 
      <methods> 
      <item> 
       <id>1</id> 
      </item> 
      </methods> 
      <id>1</id> 
     </item> 
     <item> 
      <methods> 
      <item> 
       <id>19</id> 
      </item> 
      </methods> 
      <id>2</id> 
     </item> 
     </baz> 
    </bar> 
    </foo> 
    <bar_method> 
    <root> 
     <bla id="1"> 
     <methods> 
      <method id="1"> 
      <calc md="ck" /> 
      <tm m="14" /> 
      <price_list> 
       <price mse="0"> 
       <ins re="0" /> 
       </price> 
      </price_list> 
      </method> 
      <method id="2"> 
      <calc md="qck" /> 
      <tm m="4" /> 
      <price_list> 
       <price mse="1"> 
       <ins re="0" /> 
       </price> 
      </price_list> 
      </method> 
     </methods> 
     </bla> 
     <bla id="2"> 
     <methods> 
      <method id="19"> 
      <calc md="dd" /> 
      <tm m="3" /> 
      <price_list> 
       <price mse="01"> 
       <ins re="0" /> 
       </price> 
      </price_list> 
      </method> 
     </methods> 
     </bla> 
    </root> 
    </bar_method> 
</root> 

現在我需要使用XPath把這個樹的片段變量。片段應該是這樣的:

<bla id="1"> 
    <methods> 
    <method id="1"> 
     <calc md="ck" /> 
     <tm m="14" /> 
     <price_list> 
     <price mse="0"> 
      <ins re="0" /> 
     </price> 
     </price_list> 
    </method> 
    </methods> 
</bla> 
<bla id="2"> 
    <methods> 
    <method id="19"> 
     <calc md="dd" /> 
     <tm m="3" /> 
     <price_list> 
     <price mse="01"> 
      <ins re="0" /> 
     </price> 
     </price_list> 
    </method> 
    </methods> 
</bla> 

這些都是不含method節點,id屬性bla節點這/root/foo/bar/baz/item/methods/item/id失蹤。我用下面的表達,但它與重複選擇的所有節點:

<xsl:variable name="meth" select="/root/bar_method/root//*[not(name() = 'method' and count(/root/foo/bar/baz//methods/item[id = @id]) = 0)]" /> 
+0

爲什麼你需要把片段放在一個變量中。您希望從XSL獲得什麼輸出?像你一樣需要模板給我。 –

+0

我只是想知道是否有可能。 –

+0

看起來你想要所有'bla'元素,並且只需要在它們中的第一個'methods/method'元素。是對的嗎?你不能在單個XPath表達式中這樣做,因爲你只能限制被選中的元素 - 你也不能過濾掉它們的一些後代。 – Borodin

回答

0

看起來你希望所有的bla元素,只是在每個人的第一methods/method元素。是對的嗎?

你不能在單個XPath表達式中這樣做,因爲你只能限制被選中的元素 - 你也不能過濾掉它們的一些後代。但是可以使用模板。

此樣式表創建變量$meth並使用copy-of輸出它。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:strip-space elements="*"/> 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 

    <xsl:template match="/"> 
    <xsl:variable name="meth"> 
     <xsl:apply-templates select="root/bar_method/root/bla"/> 
    </xsl:variable> 
    <xsl:copy-of select="$meth"/> 
    </xsl:template> 

    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="methods"> 
    <xsl:copy> 
     <xsl:apply-templates select="method[1]"/> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

輸出

<bla id="1"> 
    <methods> 
     <method id="1"> 
     <calc md="ck"/> 
     <tm m="14"/> 
     <price_list> 
      <price mse="0"> 
       <ins re="0"/> 
      </price> 
     </price_list> 
     </method> 
    </methods> 
</bla> 
<bla id="2"> 
    <methods> 
     <method id="19"> 
     <calc md="dd"/> 
     <tm m="3"/> 
     <price_list> 
      <price mse="01"> 
       <ins re="0"/> 
      </price> 
     </price_list> 
     </method> 
    </methods> 
</bla> 
1

的XPath只能選擇節點,也改變不了他們。也就是說,您選擇的節點的子節點和子節點將始終與源文檔中的節點完全相同。

如果要創建與輸入樹不同的樹,則需要XSLT或XQuery。

相關問題