【问题标题】:Get rid off the spaces from the xml using xsl使用 xsl 消除 xml 中的空格
【发布时间】:2021-05-31 01:25:45
【问题描述】:

我有一个使用 xslt 转换的 xml。 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, '    ','')" />

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

我也尝试了&lt;xsl:strip-space elements="*"/&gt; 并正常化

<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(这可以使用某些扩展函数的帮助)。
  • @michael.hor257k 感谢您的建议。更新了我的问题
  • 您想要仅输出描述的文本吗?
  • @michael.hor257k 是的。基本上我正在自动生成一些代码,这将是函数的块注释
  • 恐怕这对我没有任何意义。我发布了一个修改 Description 元素的答案。如果您需要其他东西,则必须自己进行调整。

标签: xml xslt-1.0


【解决方案1】:

要删除每行开头的空格,您必须将文本拆分为单独的行并依次处理每一行。要在纯 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-30
    • 2013-04-06
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 2011-05-30
    相关资源
    最近更新 更多