2013-02-06 20 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创建一个句子,这样 的〜猫(这是我最终希望能够做的一个简单的例子,这只是现在的一个绊脚石)。

我的XSLT看起来像这样;

<?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样式表我得到一条线的每个字的它自己的,然后在下一行蒂尔达,这样

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

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

如果我删除tildas我得到

Thecatsatonthemat 

它看起来对我,然后(我是新来这个XSLT东西),一个蒂尔达对在新线纳入正迫使新线。

那么,我该如何强制模板的输出全部在一行? (我最后的要求是对元素进行更多的格式化,并使用空格来填充元素 - 稍后我会介绍)。

由于在期待

回答

3

更改~<xsl:text>~</xsl:text>

+0

这当然有诀窍。谢谢 – Nerdio

1

出现这种情况的原因是,当非空白文本直接出现一个xsl:template和内部元素,所有文本非之间 - 空格属于包含在结果中(包括任何空格)。为了避免这种情况,你应该这样做:

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

我还建议消除这些字词1,单词2等是几乎相同的模板,并与一个模板替换他们:

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

谢谢你的回答,我会试试这个。顺便说一下,word1,word2只是“简单”例子的一部分。我真正的XML具有不同名称和类型的元素。 – Nerdio

+0

这当然有效,并且concat()函数将会很有用。 – Nerdio

+0

了解word1,2,3等。正如其他答案所示,'xsl:text'也可以在这里使用,但是当已经有'xsl:value-of'参数时,我更喜欢使用'concat()因为'xsl:text'通常会在XSLT中占据一个额外的行,并且会通过其开始和结束标记创建额外的混乱。 – JLRishe

0

简单<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~