2
<root>
<tag>
  <form>
   some html form will be here
  </form>

</tag>
<tag>
  some visible data
</tag>

xslt

<xsl:template match="tag">
    <div id="page-base">
      <xsl:apply-templates />
    </div>
  </xsl:template>

生産

<div id="page-base">

</div>
<div id="page-base">
 some visible data
</div>

必要な出力

<div id="page-base">
 <form>
   some html form will be here
  </form>
</div>
<div id="page-base">
 some visible data
</div>

編集:

tagテンプレートルールが適用される要素にネストされている場合はtag、タグがテンプレートに置き換えられ、テンプレートが一致しない他の要素がコピーされます。例をご覧ください。tagaribtraryネストされる可能性があります

<root>
        <tag>
          <form>
           some html form will be here
          </form>
          <tag>
            arbitrary nested tags
          </tag>    
        </tag>
        <tag>
          some visible data
        </tag>
</root>

期待される結果

 <div id="page-base">
     <form>
       some html form will be here
      </form>
      <div id="page-base">
       arbitrary nested tags  
      </div>
  </div>
  <div id="page-base">
     some visible data
  </div>
4

2 に答える 2

2

この変換

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="tag">
  <div id="page-base">
    <xsl:copy-of select="node()"/>
  </div>
 </xsl:template>
</xsl:stylesheet>

提供されたXMLドキュメントに適用した場合:

<root>
    <tag>
        <form>
   some html form will be here
        </form>
    </tag>
    <tag>
  some visible data
    </tag>
</root>

必要な正しい結果を生成します。

<div id="page-base">
   <form>
   some html form will be here
        </form>
</div>
<div id="page-base">
  some visible data
    </div>

説明

tagあなたのコードには、一致した要素の本体をコピーするものがありませんでした。

この「何か」がxsl:copy-of指示です。


更新

OPは彼の質問を変更しました、そしてこれは別の解決策を必要とします:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="/*"><xsl:apply-templates/></xsl:template>

 <xsl:template match="tag">
  <div id="page-base">
    <xsl:apply-templates/>
  </div>
 </xsl:template>
</xsl:stylesheet>

この変換が新しく提供されたXMLドキュメントに適用される場合:

<div id="page-base">
   <form>
           some html form will be here
          </form>
   <div id="page-base">
            arbitrary nested tags
          </div>
</div>
<div id="page-base">
          some visible data
        </div>
于 2012-09-13T14:05:13.287 に答える
1

コピールールを追加します:

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

これにより、より説明的なテンプレートに一致しないノードがコピーされます。一致するルールのキャッチオールの性質により、優先順位を微調整しない限り、他のルールが優先されます。微調整する場合は、このルールに他のルールよりも低い優先順位を割り当てる必要があります。

ツリーの一部のみをコピーする場合は、別のモードを追加する必要があります。このモードは、applyを呼び出すたびに指定する必要があります。

<xsl:template match="@*|node()" mode="copy">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()" mode="copy"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="tag">
  <div id="page-base">
    <xsl:apply-templates mode="copy"/>
  </div>
</xsl:template>

XSLT 2.0を使用している場合は、を使用してディープコピーを実行することもできますcopy-of。ただし、上記のアプローチでは、コピーから特定のノードを省略または変換できるため、さらに柔軟性があります。

于 2012-09-13T14:04:47.677 に答える