2016-09-26 13 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> 
関連する問題