1


したがって、このXMLを検討します。

<root>
    <table>
      <tr>
        <td>asdf</td>
        <td>qwerty</td>
        <td>1234 <p>lorem ipsum</p> 5678</td>
      </tr>
    </table>
<root>


...どうすればこのように変換できますか?

<root>
    <table>
      <tr>
        <td><BLAH>asdf</BLAH></td>
        <td><BLAH>qwerty</BLAH></td>
        <td><BLAH>1234 <p>lorem ipsum</p> 5678</BLAH></td>
      </tr>
    </table>
</root>


その場合、のすべてのインスタンスに  要素<td>  が含まれ、  <BLAH> それぞれのコンテンツが  <td>  新しいノード内に含まれます。



...これまでのところ、各  <td>  要素を新しいノードでラップしているこのXSLがありますが、内側ではなく外側にあります:

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

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

    <xsl:template match="table//td">
        <BLAH>
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
                <xsl:apply-templates select="node()"/>
            </xsl:copy>
        </BLAH>
    </xsl:template>
</xsl:stylesheet>


...これはこの望ましくない結果 を生み出しています:

<root>
  <table>
    <tr>
      <BLAH><td>asdf</td></BLAH>
      <BLAH><td>qwerty</td></BLAH>
      <BLAH><td>1234 <p>lorem ipsum</p> 5678</td></BLAH>
    </tr>
  </table>
</root>


http://xslt.online-toolz.com/tools/xslt-transformation.php でテスト済み

4

2 に答える 2

2

<BLAH>中を移動するだけxsl:copyです:

<xsl:template match="td">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <BLAH>
            <xsl:apply-templates select="node()"/>
        </BLAH>
    </xsl:copy>
</xsl:template>
于 2012-08-07T05:26:39.760 に答える
1

これを試して...

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

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="td">
        <xsl:copy>
            <BLAH>
                <xsl:apply-templates select="@*|node()" />
            </BLAH>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
于 2012-08-07T05:27:37.173 に答える