下面是关于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 //消息或者可能会更方便//消息来获得你需要的东西。
我希望这会有所帮助。
这些都是很好的答案 - 很难选择哪一个才是答案。非常感谢。 –