【问题标题】:Get element values in xsl:variable获取 xsl:variable 中的元素值
【发布时间】:2014-02-12 18:26:42
【问题描述】:

请原谅我对 XSLT 的无知,我对它还很陌生。

使用 saxon xslt 2.0:我试图从 xsl:variable 中获取单个元素,在应用 <xsl:copy-of select="$type"> 时看起来像这样:

  <type>
     <label>Book</label>
     <id>book</id>
  </type>

尝试仅访问 id 元素 - 我尝试过:

<xsl:copy-of select="$type/id">
<xsl:copy-of select="$type[2]">
<xsl:value-of select="$type/id">
<xsl:value-of select="$type[2]">

也试过这个和一些变种

<xsl:value-of select="$type[name()='id']"/>

并尝试更改数据类型

<xsl:variable name="type" as="element"> 

XSLT 2.0 node-set() 操作似乎不适用。

我寻求有关如何正确访问 xsl:variable 元素的详细说明,并且很高兴发现我使用这一切都错了,有更好的方法。感谢您的见解和努力。

@martin-honnen 添加时:

<xsl:variable name="test1">
  <type>
     <label>Book</label>
     <id>book</id>
  </type>
</xsl:variable>

<TEST1><xsl:copy-of select="$test1/type/id"/></TEST1>

<xsl:variable name="test2" as="element()">
  <type>
     <label>Book</label>
     <id>book</id>
  </type>
</xsl:variable>

<TEST2><xsl:copy-of select="$test2/id"/></TEST2>

我得到了结果:

   <TEST1/>
   <TEST2/>

【问题讨论】:

    标签: xslt xslt-2.0 saxon xsl-variable


    【解决方案1】:

    如果你有

    <xsl:variable name="type">
      <type>
         <label>Book</label>
         <id>book</id>
      </type>
    </xsl:variable>
    

    那么你需要例如&lt;xsl:copy-of select="$type/type/id"/&gt; 复制id 元素,因为type 变量绑定到包含type 元素节点和id 子元素节点的临时文档节点。

    或者使用

    <xsl:variable name="type" as="element()">
      <type>
         <label>Book</label>
         <id>book</id>
      </type>
    </xsl:variable>
    

    然后&lt;xsl:copy-of select="$type/id"/&gt; 有效,因为现在变量绑定到type 元素节点。

    以下是我的建议的完整示例:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    
    <xsl:output indent="yes"/>
    
    <xsl:template match="/">
    
    <xsl:variable name="test1">
      <type>
         <label>Book</label>
         <id>book</id>
      </type>
    </xsl:variable>
    
    <TEST1><xsl:copy-of select="$test1/type/id"/></TEST1>
    
    <xsl:variable name="test2" as="element()">
      <type>
         <label>Book</label>
         <id>book</id>
      </type>
    </xsl:variable>
    
    <TEST2><xsl:copy-of select="$test2/id"/></TEST2>
    
    </xsl:template>
    
    </xsl:stylesheet>
    

    输出是

    <TEST1>
       <id>book</id>
    </TEST1>
    <TEST2>
       <id>book</id>
    </TEST2>
    

    【讨论】:

    • 第一个样本你没有听从我的建议。第二个应该对我有用,我已经用一个完整的示例编辑了我的答案,显示了两种方式以及我使用 Saxon 9 获得的输出。如果您仍然有问题,请发布一个最小但完整的示例,让我们重现问题。
    • 谢谢马丁。我已经更新了上述内容并接受了答案,因为它应该按照您所展示的方式工作。
    • 我的完整示例应该可以工作。如果您的路径没有选择任何内容,那么可能是因为您的样式表有xmlns="..."。您可以使用&lt;xsl:variable name="test" xmlns=""&gt;&lt;type&gt;&lt;id&gt;1&lt;/id&gt;&lt;/type&gt;&lt;/xsl:variable&gt; 确保变量中的元素不在命名空间中。
    • 谢谢 - 看起来像是命名空间问题。一旦我有 15 个代表,我会接受作为答案。
    【解决方案2】:

    要访问元素值,只需正确指定 XPath,即 type/id

    <xsl:value-of select="type/id" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 2020-08-22
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      相关资源
      最近更新 更多