用于删除元素内空间的XSLT转换

时间:2022-05-29 23:36:47

I am a bit new to XSLT stuff. The problem is that I need to get rid of whitespaces within certain elements in my input XML. For example

我对XSLT有点陌生。问题是,我需要在输入XML的某些元素中去掉空格。例如

<element id="12">


</element>

should be transformed into

应该转换成

<element id="12"></element>

and also

<element id="12">

something

</element>

into

<element id="12">something</element>

and the rest of the xml should remain the same. Is this kind of transformation possible with xsl?

xml的其余部分应该保持不变。xsl能实现这种转换吗?

2 个解决方案

#1


4  

What you like to do is an Identity transform.

你喜欢做的是身份变换。

Try something like this:

试试这样:

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

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

    <xsl:template match="text()">
        <xsl:value-of select="normalize-space()"/>
    </xsl:template>

</xsl:stylesheet>

#2


1  

Hope, this will help you.

希望这能对你有所帮助。

<xsl:template match="text()">
    <xsl:value-of select="fn:normalize-space()"/> <!-- fn is the prefix bound to xpath functions -->
</xsl:template>

#1


4  

What you like to do is an Identity transform.

你喜欢做的是身份变换。

Try something like this:

试试这样:

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

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

    <xsl:template match="text()">
        <xsl:value-of select="normalize-space()"/>
    </xsl:template>

</xsl:stylesheet>

#2


1  

Hope, this will help you.

希望这能对你有所帮助。

<xsl:template match="text()">
    <xsl:value-of select="fn:normalize-space()"/> <!-- fn is the prefix bound to xpath functions -->
</xsl:template>