下面是關於XPath該/ X/Y // z是什麼樣的信息的良好來源,http://www.w3.org/TR/xpath/#location-paths
從縮寫語法部分: //短的/後裔或自身::節點()/。例如,// para是/ descendant-or-self :: node()/ child :: para的縮寫,因此將選擇文檔中的任何para元素(即使是作爲文檔元素的para元素也將被//para,因爲文檔元素節點是根節點的子節點); div // para是div/descendant-or-self :: node()/ child :: para的縮寫,因此將選擇div子元素的所有para子元素。
//是否在xpath的開始或中間,它的含義是相同的。
至於學習這個東西,對我來說,我不得不構建一個小的人工xml或簡化的xml shell,我試圖轉換並運行xslt。在Visual Studio中,2005年左右有一個相當方便的方法可以做到這一點,但我認爲它仍然存在,儘管我沒有在一段時間內搞混它。我發現MSDN或w3.org是很好的資源,儘管w3上的語言有時可能會消化。
使用此XML:
<?xml version="1.0" encoding="utf-8"?>
<root>
<a id="a1">
<d id="1" />
<d id="2" />
</a>
<a id="a2">
<d id="3" />
<d id="4" />
</a>
<b>
<c id="1" />
<c id="2" />
</b>
</root>
有了這個XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<test>
<xsl:apply-templates/>
</test>
</xsl:template>
<xsl:template match="b">
<z>
<select>//a</select>
<xsl:copy-of select="//a"/>
</z>
<z>
<select>.//a</select>
<xsl:copy-of select=".//a"/>
</z>
<z>
<select>.//c</select>
<xsl:copy-of select=".//c"/>
</z>
<z>
<select>/root/a/d</select>
<xsl:copy-of select="/root/a/d"/>
</z>
<z>
<select>/root/a</select>
<xsl:copy-of select="/root/a"/>
</z>
<z>
<question>so what is the node?</question>
<period>
<xsl:copy-of select="."/>
</period>
<slash>
<xsl:copy-of select="/"/>
</slash>
</z>
</xsl:template>
</xsl:stylesheet>
得到這樣的結果:構建一個XPath時
<?xml version="1.0" encoding="utf-8"?>
<test>
<z>
<select>//a</select>
<a id="a1">
<d id="1" />
<d id="2" />
</a>
<a id="a2">
<d id="3" />
<d id="4" />
</a>
</z>
<z>
<select>.//a</select>
</z>
<z>
<select>.//c</select>
<c id="1" />
<c id="2" />
</z>
<z>
<select>/root/a/d</select>
<d id="1" />
<d id="2" />
<d id="3" />
<d id="4" />
</z>
<z>
<select>/root/a</select>
<a id="a1">
<d id="1" />
<d id="2" />
</a>
<a id="a2">
<d id="3" />
<d id="4" />
</a>
</z>
<z>
<question>so what is the node?</question>
<period>
<b>
<c id="1" />
<c id="2" />
</b>
</period>
<slash>
<root>
<a id="a1">
<d id="1" />
<d id="2" />
</a>
<a id="a2">
<d id="3" />
<d id="4" />
</a>
<b>
<c id="1" />
<c id="2" />
</b>
</root>
</slash>
</z>
</test>
所以,//將獲得的後代那個節點類型,如果你從一個xpath開始//你將要去看文檔。
如果你開始。那麼你正在用當前節點啓動你的xpath(如果你使用xsl:apply-templates,當前節點就是在xsl:template匹配後的任何事情,但是如果你使用xsl:call-template,那麼當前節點是相同的作爲您創建xsl:call-template的當前節點)。
如果你用/來啓動xpath,那麼你引用了你的文檔的根目錄。
推測$ docId是一個節點集,它指向一個文檔或另一個文檔,它設置了xpath的起始點,所以$ docId // message表示獲取節點集中所有$ docId的消息元素。
最後你需要//的原因是你沒有完全指定路徑。在我提供的示例中,我構建了一個/ root/a/d,它是d元素的完整路徑,並將其全部4個拉出。 $ docId //消息只是允許您查找$ docId節點集根目錄下的任何元素'消息'。有時你可能沒有一個乾淨的對稱來獲取記錄:例如,如果你有/ messages/critical/message和/ messages/warning/message,使用xpath of/messages //消息或者可能會更方便//消息來獲得你需要的東西。
我希望這會有所幫助。
這些都是很好的答案 - 很難選擇哪一個纔是答案。非常感謝。 –