XSLT transformation to remove space within element

[亡魂溺海] 提交于 2019-12-02 08:27:04

问题


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

<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?


回答1:


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:


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>


来源:https://stackoverflow.com/questions/16933351/xslt-transformation-to-remove-space-within-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!