如何去除标签中的空格?

时间:2022-07-25 20:32:39

In an XSLT stylesheet, how can I remove leading and trailing whitespace inside a <xsl:attribute> tag?

在XSLT样式表中,如何删除 标记中的前导和后导空格?

For example, the following stylesheet:

例如,以下样式表:

<xsl:template match="/">
  <xsl:element name="myelement">
    <xsl:attribute name="myattribute">
      attribute value
    </xsl:attribute>
  </xsl:element>
</xsl:template>

outputs:

输出:

<myelement myattribute="&#10;      attribute value&#10;    "/>

whilst I would like it to output:

同时我希望它输出:

<myelement myattribute="attribute value"/>

Is there any way to accomplish that other than collapsing the <xsl:attribute> start and end tags in a single line?

除了将 的开始和结束标记压缩到一行之外,还有什么方法可以实现这一点?

Because if the attribute value is not a plain line of text but the result of some complex calculation (for example using or tags), then collapsing all the code in one line to avoid the leading and trailing whitespace would result in a horribly ugly stylesheet.

因为如果属性值不是简单的文本行,而是一些复杂计算的结果(例如使用或标记),那么在一行中折叠所有的代码以避免导致和拖尾的空白将导致一个可怕的样式表。

1 个解决方案

#1


7  

You could wrap the text by xsl:text or xsl:value-of:

可以通过xsl:text或xsl:value-of:

<xsl:template match="/">
    <xsl:element name="myelement">
        <xsl:attribute name="myattribute">
            <xsl:text>attribute value</xsl:text>
        </xsl:attribute>
    </xsl:element>
</xsl:template>

or

<xsl:template match="/">
    <xsl:element name="myelement">
        <xsl:attribute name="myattribute">
            <xsl:value-of select="'attribute value'"/>
        </xsl:attribute>
    </xsl:element>
</xsl:template>

Is this useful for you? Otherwise please explain your problem in using a single line.

这对你有用吗?否则请解释你使用单行的问题。

Please take notice of the comment of Michael Kay, it explains the problem to the point!

请注意迈克尔·凯的评论,它把问题解释得一清二楚!

#1


7  

You could wrap the text by xsl:text or xsl:value-of:

可以通过xsl:text或xsl:value-of:

<xsl:template match="/">
    <xsl:element name="myelement">
        <xsl:attribute name="myattribute">
            <xsl:text>attribute value</xsl:text>
        </xsl:attribute>
    </xsl:element>
</xsl:template>

or

<xsl:template match="/">
    <xsl:element name="myelement">
        <xsl:attribute name="myattribute">
            <xsl:value-of select="'attribute value'"/>
        </xsl:attribute>
    </xsl:element>
</xsl:template>

Is this useful for you? Otherwise please explain your problem in using a single line.

这对你有用吗?否则请解释你使用单行的问题。

Please take notice of the comment of Michael Kay, it explains the problem to the point!

请注意迈克尔·凯的评论,它把问题解释得一清二楚!