2010-07-29 76 views
1
的謂詞

另一個的XPath/XSL問題:)什麼是電流()的謂詞

如果我有一個節點樹等:

- B(anymal類型=寵物)

---- C(類型=鳥)

---- C(類型=貓)

---- C(=型狗)

- B(工作-動物)

---- C(=型牛)

---- C(=類型大象)

-A
...

,並列出一個需要在給定anymal型類型的另一個XML文件($ XMLFILE)

-PET

----貓

----狗

木材加工動物

----大象

怎麼辦我只選擇$ xmlFile給我的這些動物嗎?:

什麼在這種情況下電流()是指: - 它是該節點上的模板(「A」) 匹配 - 或者是它當前的C節點被評估。

什麼是正確的方式來到達當前正在評估的C節點並且向上一步(到B,定義動物類型)。

謝謝。

+0

好問題(+1)。查看我的答案獲得完整的解決方案。 – 2010-07-30 05:14:25

回答

0

這種轉變

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:variable name="vrtfXmlFile"> 
    <pets> 
     <animal>cat</animal> 
     <animal>dog</animal> 
    </pets> 
    <working-animals> 
     <animal>elephant</animal> 
    </working-animals> 
    </xsl:variable> 

    <xsl:variable name="vxmlFile" select= 
    "document('')/*/xsl:variable 
         [@name='vrtfXmlFile']"/> 

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

<xsl:template match="b"> 
    <xsl:if test="$vxmlFile/*[name()=current()/@animal-types]"> 
    <xsl:call-template name="identity"/> 
    </xsl:if> 
</xsl:template> 

<xsl:template match="c"> 
    <xsl:if test= 
    "$vxmlFile/*[name()=current()/../@animal-types] 
           /animal[.=current()/@type]"> 
    <xsl:call-template name="identity"/> 
    </xsl:if> 
</xsl:template> 
</xsl:stylesheet> 

當這個XML文檔施加:

<a> 
<b animal-types="pets"> 
    <c type="bird"/> 
    <c type="cat"/> 
    <c type="dog"/> 
</b> 
<b animal-types="working-animals"> 
    <c type="cow"/> 
    <c type="elephant"/> 
</b> 
</a> 

產生想要的,正確的結果

<a> 
    <b animal-types="pets"> 
     <c type="cat"/> 
     <c type="dog"/> 
    </b> 
    <b animal-types="working-animals"> 
     <c type="elephant"/> 
    </b> 
</a> 

  1. 爲了方便起見,將含有允許動物第二文檔,在XSLT樣式表併入。在實踐中,人們會使用document(some-uri)函數來讀取,解析並將其定義爲$vxmlFile變量的內容。

  2. 如果允許的動物列表很長,那麼使用Muenchian分組(鍵)會更有效。