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 で処理して、次のような文を作成することです。 The~cat~sat~on~the~mat

(これは私が最終的にできるようにしたいことの単純化された例です。これは今のところ障害にすぎません)。

私の 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 に対してスタイルシートを実行すると、次のように、各単語が 1 行に表示され、次の行にチルダが表示されます。

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

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

ティルダを削除すると、

Thecatsatonthemat

それから私には見えます(そして私はこのXSLTのことは初めてです)、の新しい行にチルダを含めると、新しい行が強制されます。

では、テンプレートからの出力をすべて 1 行にまとめるにはどうすればよいでしょうか。(私の最後の要件は、要素にさらに書式を設定し、要素を埋めるためのスペースを追加することです。これについては後で説明します)。

よろしくお願いします

4

3 に答える 3

3

に変更~します<xsl:text>~</xsl:text>

于 2013-02-06T10:29:11.347 に答える
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>
于 2013-02-06T10:31:05.000 に答える
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~
于 2013-02-06T13:58:29.207 に答える