2016-09-26 188 views
0

我想删除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> 

回答

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>