0

私は XSLT にかなり慣れていないので、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"/>をコピーする必要があります。idtypetypeid

または使用

<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 に答える