0

如何使用 XSLT 转换 XML 文档,例如读取输入、对文档中的所有元素应用转换(例如修剪前导和尾随空格)并返回具有完整结构的 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”-Tag 内添加“call-template”-Tag。

在将转换应用于每个元素时,如何将完整结构从源文档复制到目标文档中?

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 回答