XSLT Concatenate元素值

时间:2017-11-10 10:08:13

标签: xslt-2.0

我的要求是在ref元素中连接运行时的值。我可以为每个元素获取单独的值并将其连接起来,但我仍然会错过它们之间的文本。

我根据Martin提出的建议编辑了我的问题。

这是我的输入XML:

<?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>

我试过跟随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>
    <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>
是的,有人能帮帮我吗?提前谢谢。

2 个答案:

答案 0 :(得分:0)

你正在迭代元素,其中任何一个都有方括号:

进行第一次迭代时,得到元素<year>,其值为1902,而不是[1902]。您的方括号位于元素<noteref>

更改您的XML以将年份存储为[1902]或更改您的for-each以手动打印方括号。

答案 1 :(得分: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在线。

相关问题