0

请原谅我对 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/>
4

2 回答 2

1

如果你有

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

那么你需要例如<xsl:copy-of select="$type/type/id"/>复制id元素,因为type变量绑定到一个临时文档节点,其中包含一个type带有id子元素节点的元素节点。

或使用

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

然后<xsl:copy-of select="$type/id"/>工作,因为现在变量绑定到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>
于 2014-02-12T18:31:38.237 回答
0

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

<xsl:value-of select="type/id" />
于 2014-02-12T18:30:14.813 回答