2016-09-26 3 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>