简体   繁体   English

为什么 //descendant 也在这个 XSLT 模板中评估兄弟姐妹?

[英]Why //descendant is evaluating siblings as well in this XSLT template?

I am curious why this XSLT:我很好奇为什么这个XSLT:

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:apply-templates select="//ca"/> </xsl:template> <xsl:template match="ca"> <xsl:value-of select="."/> <xsl:value-of select="//cd"/> </xsl:template> </xsl:stylesheet>

over this XML document通过这份 XML 文件

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!DOCTYPE a> <a> <b> <c> <ca>CA</ca> <cd>CD</cd> </c> </b> </a>

has the result: CACD有结果:CACD

I'm mostly interested in why CD evaluates properly because I thought current context in a template is defined by match attribute, that is to say, ca in the second template.我最感兴趣的是为什么 CD 可以正确评估,因为我认为模板中的当前上下文是由 match 属性定义的,也就是说,第二个模板中的ca If that was correct, in the context of ca , with //cd, as far as I know, the XSLT processor should be searching by any descendant of ca of any level with name cd .如果那是正确的,在ca的上下文中,使用 //cd,据我所知,XSLT 处理器应该由名称为cd的任何级别的ca的任何后代进行搜索。

cd is a sibling of ca , so I am very confused. cdca的兄弟姐妹,所以我很困惑。

I would appreciate any help which sheds light on this.我将不胜感激任何能阐明这一点的帮助。

Thank you in advance.先感谢您。

Use .//cd to select relative to the context node, a path starting with / always selects starting at the document node/root node, ie //cd is /descendant-or-self::node()/cd .使用.//cd到 select 相对于上下文节点,以/开头的路径总是选择从文档节点/根节点开始,即//cd/descendant-or-self::node()/cd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM