使用 xsl 摆脱 xml 中的空格

Get rid off the spaces from the xml using xsl

我有一个 xml,我正在使用 xslt 对其进行转换。 xml 看起来像这样

<Function name="Add" returnType="Integer">
      <Description>
        // Returns the sum of the input parameters

        // param [out]    result
        // param [in]     input 1
        // param [in]     input 2
        // return         error        Ok=0, Warning=1, Error=2
      </Description>
</Function>

我想去掉描述标签下每行之前的空格。

当前输出:

    // Returns the sum of the input parameters

    // param [out]    result
    // param [in]     input 1
    // param [in]     input 2
    // return         error        Ok=0, Warning=1, Error=2

预期输出:

// Returns the sum of the input parameters

// param [out]    result
// param [in]     input 1
// param [in]     input 2
// return         error        Ok=0, Warning=1, Error=2

我有一个 xslt,我尝试在其中翻译输入

<xsl:value-of select="translate(Description, '    ','')" />

不幸的是,它删除了描述中的所有空格。

我也尝试了 <xsl:strip-space elements="*"/> 和正常化

<xsl:variable name="description" select="Description"/>
        <varoutput>
            <xsl:value-of select= "normalize-space($description)"/>
        </varoutput>

但运气不好。如果有人可以帮助我解决问题。

这是 XSl 1.0,因为我正在使用 schemas-microsoft-com:xslt

要删除每行开头的空格,您必须将文本拆分为单独的行并依次处理每一行。要在纯 XSLT 1.0 中执行此操作,您需要使用递归命名模板:

XSLT 1.0

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

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

<xsl:template match="Description">
    <xsl:copy>
        <xsl:call-template name="process-lines">
            <xsl:with-param name="text" select="."/>
        </xsl:call-template>
    </xsl:copy> 
</xsl:template>

<xsl:template name="process-lines">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="'&#10;'"/>
    <xsl:variable name="line" select="substring-before(concat($text, $delimiter), $delimiter)" />
    <xsl:variable name="first-char" select="substring(normalize-space($line), 1, 1)" />
    <!-- output -->
    <xsl:value-of select="$first-char"/>
    <xsl:value-of select="substring-after($line, $first-char)"/>
    <!-- recursive call -->
    <xsl:if test="contains($text, $delimiter)">
        <xsl:value-of select="$delimiter"/>
        <xsl:call-template name="process-lines">
            <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

当应用于您的输入示例时,这将产生:

结果

<?xml version="1.0" encoding="UTF-8"?>
<Function name="Add" returnType="Integer">
  <Description>
// Returns the sum of the input parameters

// param [out]    result
// param [in]     input 1
// param [in]     input 2
// return         error        Ok=0, Warning=1, Error=2
      </Description>
</Function>