需要使用 XSLT 在一个标签中标记特定段落并保留在另一个标签中

Need to tag certain paragraph in one tag and remaining in another tag using XSLT

我想在一个元素中使用第一组段落,在另一个元素中使用第二组段落

我的输入 XML 文件是:

<topic class="- topic/topic " outputclass="TOPIC-MLU-Body">
<title outputclass="MLU-Body">Body</title>
<body class="- topic/body ">
<p class="- topic/p ">Insulin is a medicine</p>
<fig class="- topic/fig ">
<image class="- topic/image "
          href="SBX0139003.jpg"
          outputclass="Fig-Image_Ref" placement="break"/>
<p class="- topic/p " outputclass="Fig-Text">Caption</p>
</fig>
<p class="- topic/p ">So, to try and lower your blood glucose levels</p>
</body>
</topic>

我用作的 XSL:

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

<xsl:template match="topic[@outputclass='TOPIC-MLU-Body']">
<body>
<text>
 <text_top><xsl:value-of select="title|body/p"/></text_top>
 <text_bottom><xsl:value-of select="body/fig|p"/></text_bottom>
</text> 
<xsl:apply-templates/>
</body>
</xsl:template>

</xsl:stylesheet>

我需要图形元素之前的段落为 "text-top",图形元素之后的段落为 "text_bottom"

我得到的输出为:

    <mlu9_body>
    <mlu9_text>
    <text_top>Insulin is a medicine So, to try and lower your blood glucose levels</text_top>
    <text_bottom>Caption</text_bottom>
    </mlu9_text>
    </mlu9_body>

但我的预期输出是:

<mlu9_body>
<mlu9_text>
<text_top>Insulin is a medicine</text_top>
<text_bottom>So, to try and lower your blood glucose levels</text_bottom>
</mlu9_text>
</mlu9_body>

我正在使用 Saxon PE 和 version=2.0 样式表。请给我一些建议。提前致谢。

您所说的当前获得的输出实际上与您提供的 XSLT 并不对应。例如,您的 XSLT 输出一个 <body> 标记,但您的输出显示一个 <mlu9_body> 标记。

此外,XSLT 中的 | 运算符是并集运算符,return 也是两个节点集的并集。比如你在做这个

<xsl:value-of select="body/fig|p"/

在 XSLT 2.0 中,这将 return body/fig 的字符串值和两个 p 元素的字符串值的串联。

无论如何,试试这个 XSLT,它将给出您预期的输出:

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

<xsl:output method="xml" indent="yes" />

<xsl:template match="topic[@outputclass='TOPIC-MLU-Body']">
<mlu9_body>
<mlu9_text>
 <text_top><xsl:value-of select="body/p[1]"/></text_top>
 <text_bottom><xsl:value-of select="body/p[2]"/></text_bottom>
</mlu9_text> 
</mlu9_body>
</xsl:template>

</xsl:stylesheet>

请注意,如果您真的想定位 fig 元素之后出现的 p 元素,您可以这样做...

<text_bottom><xsl:value-of select="body/fig/following-sibling::p"/></text_bottom>