简体   繁体   English

<xsl:when test=“//…”>句法

[英]<xsl:when test=“//…”> syntax

I'm trying to debug an existing XSLT file and have the following elements 我正在尝试调试现有的XSLT文件并具有以下元素

    <xsl:choose>
        <xsl:when test="//j:Node_A/j:Node_B/j:Node_c">
            <xsl:apply-templates select="//j:Node_A/j:Node_B/j:Node_c />
        </xsl:when>
        <xsl:otherwise>
            <xsl:call-template name="NoMatch" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

As I understand the test statement it is looking for a structure like the following in the XML and when the structure is found applies the template. 据我了解,该测试语句正在XML中寻找一种类似于以下内容的结构,并在找到该结构时应用该模板。

  <node_a>
    <node_b>
      <node_c>
          DATA
      </node_c>
    </node_b>
  </node_a>

What I don't understand is the meaning of the // . 我不了解//的含义。

The precise definition of // in XPath is that it is shorthand for XPath中//的确切定义是它的简写

/descendant-or-self::node()/

including the leading and trailing slashes. 包括开头和结尾的斜线。 The path a//b is effectively the union of a/b , a/*/b , a/*/*/b , etc. for any number of repetitions of /* 路径a//b实际上是a/ba/*/ba/*/*/b等的并集,适用于/*任意重复

In practice this means you can use it when you want to look for descendant nodes at any level rather than just immediate children, eg /node_a//node_c . 实际上,这意味着您可以在想要查找任何级别的后代节点而不是直接子节点(例如/node_a//node_c

This is actually an XPath question. 这实际上是一个XPath问题。

The "//" indicates "not starting at root" “ //”表示“不是从根开始”

/blam/hoot/kapow only matches if the root node is blam. /blam/hoot/kapow仅在根节点为/blam/hoot/kapow匹配。

//blam/hoot/kapow matches if there is a node pattern of blam containing a hoot containing a kapow. //blam/hoot/kapow如果存在//blam/hoot/kapow的节点模式包含包含kapow的hoot的节点模式,则匹配。

The second test will match the blam/hoot/kapow in this: 第二个测试将与此匹配blam / hoot / kapow:

<schmar>
   <blam>
     <hoot>
       <kapow>
       </kapow>
     </hoot>
   </blam>
</schmar>

The first test will not match in the xml above. 上面的xml中的第一个测试将不匹配。

// represents the descendant:: or descendant-or-self:: axis in XPath. //表示descendant::descendant-or-self:: XPath中轴线。 It should be followed by a node test which can be: 随后应进行节点测试 ,可以是:

  • qualified name of an element. 元素的限定名称。 Ex: //j:node_a (matches all descendant node_a elements in the namespace represented by the prexif j - same as //descendant-or-self::j:node_a ) 例如:// j:node_a(匹配由prexif j表示的命名空间中的所有后代node_a元素-与//descendant-or-self::j:node_a
  • an * which represents any element. *表示任何元素。 Ex: //* (matches all descendant elements - same as //descendant-or-self::* ) 例如:// *(匹配所有后代元素 -与//descendant-or-self::*
  • text(), processing-instruction() or comment(). text(),processing-instruction()或comment()。 Ex: //text() (matches all descendant text nodes - same as //descendant-or-self::text() ) 例如:// text()(匹配所有后代文本节点 -与//descendant-or-self::text()
  • another axis. 另一个轴。 Ex: //@* (matches all attributes - same as //attribute::* ) 例如: //@* (匹配所有属性-与//attribute::*相同)

You can use it anywhere inside the XPath expression to skip levels in a tree. 您可以在XPath表达式内的任何位置使用它来跳过树中的级别。 For example: 例如:

//j:Node_A//j:Node_c

will match all Node_c elements which have a Node_A ancestor. 将匹配所有具有Node_A祖先的Node_c元素。

Note that XPath expressions are case sensitive: Node_a != Node_A != node_a . 请注意,XPath表达式区分大小写: Node_a != Node_A != node_a

Your XSLT selects elements which are prefixed. 您的XSLT选择带前缀的元素。 It should also have a declaration somewhere (normally in the <xsl:stylesheet> element) which associates that prefix with a namespace for your expressions to work. 它还应该在某处(通常在<xsl:stylesheet>元素中)有一个声明,该声明将该前缀与命名空间相关联,以使表达式起作用。

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

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