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

Remove whitespaces except the ones preceded or followed by specific element

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

输入文件:

<?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 文件(目前似乎无论如何都会插入 spaces):

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

怎么样:

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>