2013-02-06 3 views
0

은 ...줄 바꿈이 같은 몇 가지 간단한 XML을

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <sentence> 
     <word1>The</word1> 
     <word2>cat</word2> 
     <word3>sat</word3> 
     <word4>on</word4> 
     <word5>the</word5> 
     <word6>mat</word6> 
    </sentence> 
    <sentence> 
     <word1>The</word1> 
     <word2>quick</word2> 
     <word3>brown</word3> 
     <word4>fox</word4> 
     <word5>did</word5> 
     <word6>nothing</word6> 
    </sentence> 
</root> 

내가 무엇을 할 수 있기를 원하는 것은 과정이이 ~ 고양이처럼, 문장을 만들 XSLT 함께 ~ ~ ~ ~ ~ mat ~

(이것은 내가 궁극적으로 할 수있는 일의 단순한 예이며, 지금은 단지 걸림돌입니다.)

내 XSLT는 다음과 같습니다. 나는 XML을 통해 스타일 시트를 실행하는 경우는 다음 줄에 자신의 다음, 틸다를 s로 나는 tildas I를 제거하면

<?xml version="1.0" encoding="UTF-8"?> 
    <xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output indent="no" /> 
    <xsl:template match="text()[not(string-length(normalize-space()))]"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/"> 
     <xsl:text>&#xa;</xsl:text> 
     <xsl:apply-templates /> 
    </xsl:template> 

    <xsl:template match="/root/sentence"> 
     <xsl:apply-templates /> 
     <xsl:text>&#xa;</xsl:text> 
    </xsl:template> 

    <xsl:template match="word1"> 
     <xsl:value-of select="text()" /> 
     ~ 
    </xsl:template> 

    <xsl:template match="word2"> 
     <xsl:value-of select="text()" /> 
     ~ 
    </xsl:template> 

    <xsl:template match="word3"> 
     <xsl:value-of select="text()" /> 
     ~ 
    </xsl:template> 

    <xsl:template match="word4"> 
     <xsl:value-of select="text()" /> 
     ~ 
    </xsl:template> 

    <xsl:template match="word5"> 
     <xsl:value-of select="text()" /> 
     ~ 
    </xsl:template> 

    <xsl:template match="word6"> 
     <xsl:value-of select="text()" /> 
     ~ 
    </xsl:template> 
</xsl:stylesheet> 

나는이

<?xml version="1.0" encoding="UTF-8"?> 
The 
     ~ 
    cat 
     ~ 
    sat 
     ~ 
    on 
     ~ 
    the 
     ~ 
    mat 
     ~ 

The 
     ~ 
    quick 
     ~ 
    brown 
     ~ 
    fox 
     ~ 
    did 
     ~ 
    nothing 
     ~ 

같은 라인의 각 단어를 얻을 의 새로운 라인에 틸다의 포함은 새로운 라인을 강요하고 있다고, 그런 다음 나에게 보이는

Thecatsatonthemat 

수 (나는이 XSLT 물건에 새로운 오전).

그래서 템플릿의 출력을 모두 한 줄로 만들 수 있습니까? (필자의 마지막 요구 사항은 엘리먼트에 더 많은 포맷팅을하고, 엘리먼트를 패딩하기위한 공간이다 - 나중에 설명하겠다.) 기대에

감사

답변

3

는 이런 일이 당신의 ~-<xsl:text>~</xsl:text>

+0

확실히 트릭을 수행했습니다. 감사합니다 – Nerdio

1

이유 변경은 그 공백이 아닌 텍스트가 아닌 그 텍스트의 xsl:template과 그 내부 요소, 모든간에 직접 나타날 때 - 공백은 결과 (공백 포함)에 포함됩니다. 이 문제를 방지하려면이 작업을 수행해야합니다 간단하게

<xsl:template match="*[starts-with(name(), 'word')]"> 
    <xsl:value-of select="concat(text(), '~')" /> 
</xsl:template> 
+0

답변 주셔서 감사합니다, 나는 이것을 시도합니다. BTW, word1, word2는 '단순한'예제의 일부일뿐입니다. 내 실제 XML에는 이름과 유형이 다른 요소가 있습니다. – Nerdio

+0

이것은 확실히 작동하며 concat() 함수가 유용합니다. – Nerdio

+0

word1, 2, 3 등등에 대해서도 이해할 수 있습니다. 다른 답변에서 볼 수 있듯이'xsl : text '도 여기에서 작동하지만 이미'xsl : value-of'가있을 때'concat()'를 사용하는 것을 선호합니다 왜냐하면'xsl : text'는 일반적으로 XSLT에서 추가 라인을 차지할 것이고 시작 태그와 끝 태그가 추가로 복잡해지기 때문입니다. – JLRishe

0

:

<xsl:template match="word1"> 
    <xsl:value-of select="concat(text(), '~')" /> 
</xsl:template> 

는 또한 그 단어 1, word2 등 거의 동일 템플릿을 제거하고 하나의 템플릿으로 대체 제안 <xsl:value-of select="text()" />사이에 줄 바꿈을 삭제 ~ 나를 위해 작품

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output indent="no" /> 
    <xsl:template match="text()[not(string-length(normalize-space()))]"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/"> 
    <xsl:text>&#xa;</xsl:text> 
    <xsl:apply-templates /> 
    </xsl:template> 

    <xsl:template match="/root/sentence"> 
    <xsl:apply-templates /> 
    <xsl:text>&#xa;</xsl:text> 
    </xsl:template> 

    <xsl:template match="word1"> 
    <xsl:value-of select="text()" />~</xsl:template> 

    <xsl:template match="word2"> 
    <xsl:value-of select="text()" />~</xsl:template> 

    <xsl:template match="word3"> 
    <xsl:value-of select="text()" />~</xsl:template> 

    <xsl:template match="word4"> 
    <xsl:value-of select="text()" />~</xsl:template> 

    <xsl:template match="word5"> 
    <xsl:value-of select="text()" />~</xsl:template> 

    <xsl:template match="word6"> 
    <xsl:value-of select="text()" />~</xsl:template> 
</xsl:stylesheet> 

결과 :

<?xml version="1.0" encoding="utf-8"?> 
The~cat~sat~on~the~mat~ 
The~quick~brown~fox~did~nothing~