2017-11-10 21 views
0

私の要件は、実行時にref要素の値を連結することです。私は各要素とそれを連結するための別々の値を得ることができますが、それでも私は間にテキストを逃します。XSLT要素の値を連結する

私はマーティンの示唆に従って質問を編集しました。ここで

私の入力XMLである:私は、次のXSLT

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
exclude-result-prefixes="xs" 
version="2.0"> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 
<xsl:template match="nref"> 
    <xsl:variable name="ref" select="@ref"/> 
    <noteref> 
     <xsl:for-each select="//pnotes/pnote"> 
      <xsl:if test="@id = $ref"> 
       <xsl:value-of select="."/> 
      </xsl:if> 
     </xsl:for-each> 
    </noteref> 
</xsl:template> 
<xsl:template match="//pnotes[not(pnotes)]"/> 
</xsl:stylesheet> 

を試してみました

<?xml version="1.0" encoding="UTF-8"?><rootelement> 
<nref ref="f1"/> <nref ref="f2"/> <nref ref="f3"/> 
<nref ref="f4"/> <nref ref="f5"/> 
<pnotes> 
<pnote id="f1"> 
    <p>Some Text.</p> 
</pnote> 
<pnote id="f2"> 
    <p><ref><year>1812</year>, <vol>3</vol><series>F. &amp; 
      F.</series><pages>22</pages></ref>, at p. 27.</p> 
</pnote> 
<pnote id="f3"> 
    <p><ref>[<year>1914</year>], 
     <vol>2</vol><series>A.B.C.</series><pages>94</pages></ref>.</p> 
</pnote> 
<pnote id="f4"> 
    <p><ref><year>1955</year>, 
     <vol>2</vol><series>A.B.C</series><pages>509</pages></ref>.</p> 
</pnote> 
<pnote id="f5"> 
    <p><ref>[<year>1805</year>], <series>A.C.</series><pages>21</pages>  </ref>.</p> 
</pnote> 
</pnotes></rootelement> 

私が得る結果が正しくありません。

テキストの間にスペースが必要です。

結果:

<?xml version="1.0" encoding="UTF-8"?> 
<rootelement> 
    <noteref> Some Text. </noteref> 
    <noteref> 1812, 3F. &amp; F.22, at p. 27. </noteref> 
    <noteref> [1914], 2A.B.C.94. </noteref> 
    <noteref> 1955, 2A.B.C509. </noteref> 
    <noteref> [1805], A.C.21. </noteref> 
</rootelement> 

必要な結果は次のとおりです。

<?xml version="1.0" encoding="UTF-8"?> 
<rootelement> 
<noteref> Some Text. </noteref> 
<noteref> 1812, 3 F. &amp; F. 22, at p. 27. </noteref> 
<noteref> [1914], 2 A.B.C. 94. </noteref> 
<noteref> 1955, 2 A.B.C 509. </noteref> 
<noteref> [1805], A.C.21. </noteref> 
</rootelement> 

誰かが私を助けてくださいことはできますか?前もって感謝します。あなたはelemntsを反復処理され、そのうちの誰もが角括弧を持って

答えて

0

あなたが最初の反復を行うと、[1902]、ではないあなたは、要素<year>を取得し、その値は1902です。角括弧は要素<noteref>にあります。

年を[1902]に格納するようにXMLを変更するか、for-eachで大括弧を手動で印刷するように変更します。

0

単に出力要素の文字列値あなたが行は、文字列を正規化壊したくない場合は、すべてのテキストを取得する:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="ref"> 
     <noteref> 
     <xsl:value-of select="normalize-space()"/> 
     </noteref> 
    </xsl:template> 

</xsl:transform> 

結果が

<rootelement> 
<pnote> 
    <P> 
     <noteref>[1902], A.C. 12</noteref>.</P> 
</pnote> 
<pnote> 
    <P> 
     <noteref>1902 B.A. 98</noteref>.</P> 
</pnote> 
<pnote> 
    <P> This is a text </P> 
</pnote> 
<pnote> 
    <P> 
     <noteref>[1912], 1 R.B. 160</noteref>, at pp. 165, 169.</P> 
</pnote> 
</rootelement> 

オンラインでありますhttp://xsltransform.hikmatu.com/b4GWV2

+0

サンプル・サンプルでnormalize-space()を使用すると、正常に動作しますが、実際のXMLに対して実行すると、noteref要素からスペースが削除されます。あなたはお勧めできますか? – user23

+0

質問を編集して、サンプルデータと必要な出力を、保存したい空白の説明と削除または崩壊の説明とともに提供してください。 ''の代わりに ''を使用すると、空白は削除されないと確信していますが、改行を維持して、これまでに投稿したサンプルを表示して、折りたたむことを示します。 –

+0

こんにちはマーティン - 私はあなたの提案に従って私の質問を編集しました。助けてくれてありがとう – user23