0

このようなノードのコレクションがあります

<node id="1">
  <languaje>c</languaje>
  <os>linux</os> 
</node>
<node id="2">
  <languaje>c++</languaje>
  <os>linux</os> 
</node>
<node id="3">
  <languaje>c#</languaje>
  <os>window</os> 
</node>
<node id="4">
  <languaje>basic</languaje>
  <os>mac</os> 
</node>

そして、このようなすべてのプロパティIDの新しいコレクションを作成したい

<root>
 <token>1</token>
 <token>2</token>
 <token>3</token>
 <token>4</token>
</root>

どうすればそれができますか

4

3 に答える 3

1

あなたに必要なのは

<xsl:output indent="yes"/>

<xsl:template match="*[node]">
  <root>
    <xsl:apply-templates select="node"/>
  </root>
</xsl:template>

<xsl:template match="node">
  <token><xsl:value-of select="@id"/></token>
</xsl:template>

結果を変数に格納したい場合は、XSLT 1.0 で結果ツリー フラグメントを作成できます。

<xsl:variable name="rtf1">
  <xsl:apply-templates select="node()" mode="m1"/>
</xsl:variable>

    <xsl:template match="*[node]" mode="m1">
      <root>
        <xsl:apply-templates select="node" mode="m1"/>
      </root>
    </xsl:template>

    <xsl:template match="node" mode="m1">
      <token><xsl:value-of select="@id"/></token>
    </xsl:template>

次に<xsl:copy-of select="$rtf1"/>、結果ツリーのフラグメントを使用するか、「exsl:node-set」を使用して、作成されたノードを XPath と XSLT で処理できます。

<xsl:apply-templates select="exsl:node-set($rtf1)/root/token"/>

XSLT 2.0 では、結果ツリー フラグメントがなくなるため、拡張関数を必要とせずに、任意の入力と同じように変数を使用できます。

于 2012-07-24T17:05:39.380 に答える
1

XQuery を使用できる場合は、次のように実行できます。

<root>
   { ($document/node/<node>{string(@id)}</node>) }
</root>

これが最も明確な解決策です。

それ以外の場合は、タグと ID を連結することにより、XPath 2 で目的の結果を含む文字列 (ドキュメントではない) を作成できます。

concat("<root>", string-join(for $i in /base/node/@id return concat("<node>",$i,"</node>"), " ") , "</root>")
于 2012-07-24T17:07:51.567 に答える
0

<nodes> のように、すべてのノードをタグでラップすると、次のように機能します。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<root>
  <xsl:apply-templates select="*" />
</root>
</xsl:template>

<!-- templates -->
  <xsl:template match="node">
  <token><xsl:value-of select="@id" /></token>
</xsl:template>
</xsl:stylesheet>

XsltCake でテスト済み

http://www.xsltcake.com/slices/E937yH

于 2012-07-24T17:23:27.897 に答える