如何将相邻相同元素的所有部分放入输出中的单个内容中,使用 xslt 按 ID 分组,其中部分由 ID 标识?



INPUT XML:

<?xml version="1.0"?>
<he>
<p>...some text 111.</p>
<figure id="c01-fig-001">
<section id="c01-sec-1418">
<title type="main">A Journey</title>
<section id="c01-sec-1218">
<p>My para.</p>
</section>
</section>
<caption>My Fig 1.</caption>
</figure>
<p>...some text 222.</p>
<figure id="c01-fig-002" role="noart">
<section id="c01-sec-1518">
<title type="main">An Abstract 2</title>
<section id="c01-sec-3218">
<p>My para1 2.</p>
</section>
<section id="c01-sec-5218">
<p>My para2 2.</p>
</section>
</section>
<caption>My Fig 2.</caption>
</figure>
<figure id="c01-fig-002a" role="noart">
<section id="c01-sec-1418">
<section id="c01-sec-1318">
<p>My para3 2a.</p>
</section>
<section id="c01-sec-1218">
<p>My para4 2a.</p>
</section>
</section>
</figure>
<figure id="c01-fig-002b" role="noart">
<section id="c01-sec-1418">
<title type="main">An Abstract 2b</title>
<section id="c01-sec-1318">
<p>My para5 2b.</p>
</section>
</section>
</figure>
<figure id="c01-fig-002c" role="noart">
<section id="c01-sec-1208">
<title type="main">An Abstract 2c</title>
<section id="c01-sec-1158">
<title>Last para of last part 2c</title>
<p>My para6 2c.</p>
</section>
</section>
</figure>
<p>...some text 333.</p>
<figure id="c01-fig-003" role="noart">
<section id="c01-sec-1818">
<title type="main">Pre Production 3</title>
<section id="c01-sec-3518">
<p>My para1 3.</p>
</section>
<section id="c01-sec-1218">
<p>My para2 3.</p>
</section>
</section>
<caption>My Fig 3.</caption>
</figure>
<p>...some text 333.</p>
<figure id="c01-fig-003a" role="noart">
<section id="c01-sec-1718">
<title type="main">Pre Production 3a</title>
<section id="c01-sec-3158">
<p>My para3 3a.</p>
</section>
<section id="c01-sec-1628">
<p>My para4 3a.</p>
</section>
</section>
</figure>
<p>...some text 444.</p>
<figure id="c01-fig-004">
<section id="c01-sec-1128">
<title type="main">Some Text 4</title>
<section id="c01-sec-3118">
<p>My para1 4.</p>
</section>
<section id="c01-sec-1328">
<p>My para2 4.</p>
</section>
</section>
<caption>My Fig 4.</caption>
</figure>

XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="p">
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="section/p">
<h3><xsl:apply-templates/></h3>
</xsl:template>
<xsl:template match="figure/section/title">
<h1><xsl:apply-templates/></h1>
</xsl:template>
<xsl:template match="section/section/title">
<p><i><xsl:apply-templates/></i></p>
</xsl:template>
<xsl:template match="caption">
<caption><xsl:apply-templates/></caption>
</xsl:template>
<xsl:template match="figure">
<xsl:if test="number(substring-after(@id, '-fig-'))">
<aside type="figure">
<div id="{substring-after(@id, '-fig-')}">
<xsl:apply-templates/>
<xsl:if test="contains(following-sibling::figure[1]/@id, @id)">
<PART><xsl:apply-templates select="following-sibling::figure" mode="noart"/></PART>
</xsl:if>
</div>
</aside>
</xsl:if>
</xsl:template>
<xsl:template match="figure" mode="noart">
<xsl:if test="contains(preceding-sibling::figure[1]/@id, substring(@id, 1, string-length(@id) - 1))">
<xsl:if test="matches(@id, substring(preceding-sibling::figure[1]/@id, 1, string-length(preceding-sibling::figure[1]/@id) - 1))">
<xsl:apply-templates/>
</xsl:if>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

电流输出:

<p>...some text 111.</p>
<aside type="figure">
<div id="001">
<h1>A Journey</h1>
<h3>My para.</h3>
<caption>My Fig 1.</caption>
</div>
</aside>
<p>...some text 222.</p>
<aside type="figure">
<div id="002">
<h1>An Abstract 2</h1>
<h3>My para1 2.</h3>
<h3>My para2 2.</h3>
<caption>My Fig 2.</caption>
<PART>
<h3>My para3 2a.</h3>
<h3>My para4 2a.</h3>
<h1>An Abstract 2b</h1>
<h3>My para5 2b.</h3>
<h1>An Abstract 2c</h1>
<p>
<i>Last para of last part 2c</i>
</p>
<h3>My para6 2c.</h3>
<h1>Pre Production 3a</h1>  -------SHOULD NOT BE HERE(as part of fig-3)
<h3>My para3 3a.</h3>    -------SHOULD NOT BE HERE(as part of fig-3)
<h3>My para4 3a.</h3>    -------SHOULD NOT BE HERE(as part of fig-3)
</PART>
</div>
</aside>
<p>...some text 333.</p>
<aside type="figure">
<div id="003">
<h1>Pre Production 3</h1>
<h3>My para1 3.</h3>
<h3>My para2 3.</h3>
<caption>My Fig 3.</caption>
<PART>
<h1>Pre Production 3a</h1>
<h3>My para3 3a.</h3>
<h3>My para4 3a.</h3>
</PART>
</div>
</aside>
<p>...some text 333.</p>
<p>...some text 444.</p>
<aside type="figure">
<div id="004">
<h1>Some Text 4</h1>
<h3>My para1 4.</h3>
<h3>My para2 4.</h3>
<caption>My Fig 4.</caption>
</div>
</aside>

注意:要匹配的 [-fig-] 后面的 ID 数字部分,即 [001, 002, 003, ...]。

例如,ID 为c01-fig-002的图形应与c01-fig-002a | c01-fig-002b ...匹配,其中最后一个字符a|b|c...表示这些是同一捆绑包的部分,并且要保存在输出中的单个捆绑包中。

提前感谢您的任何支持。

这是答案:

  1. 添加变量
<xsl:variable name="ID" select="./@id"/>
    <
  1. xsl:apply-templates select="follow-sibling::figure" mode="noart"/>应该是这样的
<PART>
<xsl:for-each select="following-sibling::figure[matches(@id,$ID)]">
<xsl:apply-templates select="." mode="noart"/>
</xsl:for-each>
</PART>
  1. 模板应读作
<xsl:template match="figure" mode="noart">
<xsl:text>&#xA;</xsl:text>
<xsl:apply-templates/>
<xsl:text>&#xA;</xsl:text>
</xsl:template>

最新更新