0

入力を読み取り、ドキュメント内のすべての要素に変換 (先頭と末尾の空白を削除するなど) を適用し、XML ドキュメントを元の完全な構造で返すように、XSLT を使用して XML ドキュメントを変換するにはどうすればよいですか? (トリミングの問題については、繰り返し空白を単一のものに置き換えずに XSLT でスペースをトリミングするにはどうすればよいですか?も参照してください)

すべての要素をコピーするために、次のコードから始めました。

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

これは正常に動作します。ここで、いくつかの行を追加して変換を適用したいと思いました。

<xsl:template match="@* | node()">
    <xsl:copy>

      <xsl:apply-templates select="@* | node()">
        <xsl:call-template name="string-trim">
          <xsl:with-param name="string" select="@* | node()" />
        </xsl:call-template>
      </xsl:apply-templates>

    </xsl:copy>
</xsl:template>

ただし、「apply-templates」タグ内に「call-template」タグを追加することは許可されていないようです。

各要素に変換を適用しながら、ソース ドキュメントからターゲット ドキュメントに完全な構造をコピーするにはどうすればよいですか?

4

1 に答える 1

1

text()@*...の個別のテンプレートを作成できます。

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

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

    <xsl:template match="text()">
        <xsl:call-template name="string-trim">
            <xsl:with-param name="string" select="." />
        </xsl:call-template>                
    </xsl:template>

    <xsl:template match="@*">
        <xsl:attribute name="{name()}">
            <xsl:call-template name="string-trim">
                <xsl:with-param name="string" select="." />
            </xsl:call-template>        
        </xsl:attribute>
    </xsl:template>

    <xsl:template name="string-trim">
        <xsl:param name="string"/>
        ?????
    </xsl:template>

</xsl:stylesheet>

「string-trim」という名前のテンプレートを自分のものに置き換えることを忘れないでください。

于 2013-02-11T08:51:09.233 に答える