XSLT模板中发生换行符

时间:2013-02-06 10:23:06

标签: xml xslt

我有一些像这样简单的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上运行样式表,我会将每个单词放在它自己的一行上,然后在下一行上显示一个tilda,就像这样

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

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

如果我删除了tildas

Thecatsatonthemat

然后我看起来(我是这个XSLT的新手),在新行中包含一个tilda正在逼迫新行。

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

感谢您的期待

3 个答案:

答案 0 :(得分:3)

将您的~更改为<xsl:text>~</xsl:text>

答案 1 :(得分:1)

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

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

我还建议删除那些几乎相同的word1,word2等模板,并用一个模板替换它们:

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

答案 2 :(得分: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~