删除除特定元素之前或之后的空格

时间:2016-09-26 12:42:12

标签: xml xslt removing-whitespace

我想删除xml文件中所有不必要的空格。但是如果在arg元素之前或之后只有空格,我希望在arg元素周围留一个空格(因为我不希望将参数与周围文本连接起来,如果从一开始就不是这样的话)。

输入文件:

<?xml version="1.0" encoding="utf-8"?>
<Data>
  <Text number="1">
    <Title>Lazy dog jumper</Title>
    <Description> The quick brown         fox jumps over the lazy dog <arg format="z" />. 
The quick brown fox jumps over the lazy dog <arg format="y" />. The quick brown fox jumps over the lazy dog <arg format="x" />. </Description>
  </Text>
  <Text number="2">
    <Title>                        Lazy foxer</Title>
    <Description>The                 quick brown <arg format="a" />fox <arg format="x" /><p />jumps over the lazy dog.         </Description>
  </Text>
</Data>

xsl文件(目前似乎无论如何都插入空格):

<?xml version="1.0" encoding="UTF-8"?>
<!-- Remove spaces. Keep spaces around arg tags. -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" 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:template match="node()[local-name()='arg']">

        <xsl:if test="preceding-sibling::node()[1][self::text()[not(normalize-space()) = '']]">
            <xsl:text>&#160;</xsl:text>
        </xsl:if>

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

        <xsl:if test="following-sibling::node()[1][self::text()[not(normalize-space()) = '']]">
            <xsl:text>&#160;</xsl:text>
        </xsl:if>
    </xsl:template>  
</xsl:stylesheet>

所需的输出:

<?xml version="1.0" encoding="utf-8"?>
<Data>
  <Text number="1">
    <Title>Lazy dog jumper</Title>
    <Description>The quick brown fox jumps over the lazy dog <arg format="z" />. The quick brown fox jumps over the lazy dog <arg format="y" />. The quick brown fox jumps over the lazy dog <arg format="x" />.</Description>
  </Text>
  <Text number="2">
    <Title>Lazy foxer</Title>
    <Description>The quick brown <arg format="a" />fox <arg format="x" /><p />jumps over the lazy dog.</Description>
  </Text>
</Data>

1 个答案:

答案 0 :(得分: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="*"/>

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

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

<xsl:template match="arg"> 
    <xsl:variable name="text-before" select="preceding-sibling::node()[1][self::text()]" />
    <xsl:variable name="text-after" select="following-sibling::node()[1][self::text()]" />
    <xsl:if test="substring($text-before, string-length($text-before)) = ' '">
        <xsl:text> </xsl:text>
    </xsl:if>
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    <xsl:if test="substring($text-after, 1, 1) = ' '">
        <xsl:text> </xsl:text>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>