首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将相邻的相同元素的所有部分放入输出中的单个部分,按照ID与xslt进行分组,其中部分是从ID中识别的?

如何将相邻的相同元素的所有部分放入输出中的单个部分,按照ID与xslt进行分组,其中部分是从ID中识别的?
EN

Stack Overflow用户
提问于 2018-04-03 15:35:03
回答 1查看 52关注 0票数 0

输入XML:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<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>

当前产出:

代码语言:javascript
复制
<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>

注:图后的ID数字部分-将匹配,即001,002,003,.

例如,与id类似的c01-fig-002的数字应该与c01-fig-002a | c01-fig-002b ...匹配,其中最后一个字符a|b|c...表示这些是同一个包的一部分,并在输出中保持在一个包中。

提前感谢您的支持。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-23 14:37:56

这就是答案:

  1. 添加一个变量
代码语言:javascript
复制
<xsl:variable name="ID" select="./@id"/>
  1. 行应该类似
代码语言:javascript
复制
<PART>
    <xsl:for-each select="following-sibling::figure[matches(@id,$ID)]">
        <xsl:apply-templates select="." mode="noart"/>
    </xsl:for-each>
</PART>
  1. 模板应读为
代码语言:javascript
复制
<xsl:template match="figure" mode="noart">
    <xsl:text>&#xA;</xsl:text>
        <xsl:apply-templates/>
    <xsl:text>&#xA;</xsl:text>
</xsl:template>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49633950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档