首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在单个div中对不同元素进行分组

如何在单个div中对不同元素进行分组
EN

Stack Overflow用户
提问于 2018-09-03 18:56:29
回答 1查看 42关注 0票数 0

verse输出类元素到一组元素中的div元素到verse元素,如下面的格式所示

我的XML代码是:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
  PUBLIC>
<topic id="topic_31" xml:lang="en-US" outputclass="Back-Matter">
  <title>Back-Matter</title>
  <body>
    <p outputclass="E-Head">E-Head</p>
    <p outputclass="F-Head">F-Head</p>
    <p outputclass="Body-Text">Body-Text (Tx)</p>
    <p outputclass="Body-Text-ContinuedTxc">Body-Text-Continued</p>
    <p outputclass="Verse1">Verse1 (Vrs1)</p>
    <p outputclass="Verse1-indent1">Verse1-indent1 (Vrs1-i1)</p>
    <p outputclass="Verse1-indent2">Verse1-indent2 (Vrs1-i2)</p>
    <p outputclass="Verse1-indent3">Verse1-indent3 (Vrs1-i3)</p>
    <p outputclass="Verse2Vrs2">Verse2 (Vrs2)</p>
    <p outputclass="Verse2-indent1Vrs2-i1">Verse2-indent1 (Vrs2-i1)</p>
    <p outputclass="Verse2-indent2Vrs2-i2">Verse2-indent2 (Vrs2-i2)</p>
    <p outputclass="Verse2-indent3Vrs2-i3">Verse2-indent3 (Vrs2-i3)</p>
    <p outputclass="QuotationQuo">Quotation (Quo)</p>
    <p outputclass="Extract1Ext1">Extract1 (Ext1)</p>
    <p outputclass="Extract2Ext2">Extract2 (Ext2)</p>
    <p outputclass="E-Head">E-Head</p>
    <p outputclass="F-Head">F-Head</p>
    <p outputclass="Body-Text">Body-Text (Tx)</p>
    <p outputclass="Body-Text-ContinuedTxc">Body-Text-Continued</p>
    <p outputclass="Verse1">Verse1 (Vrs1)</p>
    <p outputclass="Verse1-indent1">Verse1-indent1 (Vrs1-i1)</p>
    <p outputclass="Verse1-indent2">Verse1-indent2 (Vrs1-i2)</p>
    <p outputclass="Verse1-indent3">Verse1-indent3 (Vrs1-i3)</p>
    <p outputclass="Verse2Vrs2">Verse2 (Vrs2)</p>
    <p outputclass="Verse2-indent1Vrs2-i1">Verse2-indent1 (Vrs2-i1)</p>
    <p outputclass="Verse2-indent2Vrs2-i2">Verse2-indent2 (Vrs2-i2)</p>
    <p outputclass="Verse2-indent3Vrs2-i3">Verse2-indent3 (Vrs2-i3)</p>
    <p outputclass="QuotationQuo">Quotation (Quo)</p>
    <p outputclass="Extract1Ext1">Extract1 (Ext1)</p>
    <p outputclass="Extract2Ext2">Extract2 (Ext2)</p>
    </body>
</topic>

需要输出为任意位置的" verse“,在output类中使用,我们希望在完成verse元素的组合后作为div根元素结束,如下所示:

代码语言:javascript
复制
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en-us" lang="en">
<head>
<title>Styles</title>
</head>
<body class="Back-Matter" id="topic_31">
<section>
<h6 class="E-Head">E-Head</h6>
<section>
<h6 class="F-Head">F-Head</h6>
<section>
<p class="Body-Text">Body-Text</p>
<p class="Body-Text-Continued">Body-Text-Continued</p>
<div class="verse_group">
<p class="Verse1">Verse1</p>
<p class="Verse1-indent1">Verse1-indent1</p>
<p class="Verse1-indent2">Verse1-indent2</p>
<p class="Verse1-indent3">Verse1-indent3</p>
<p class="Verse2">Verse2</p>
<p class="Verse2-indent1">Verse2-indent1</p>
<p class="Verse2-indent2">Verse2-indent2</p>
<p class="Verse2-indent3">Verse2-indent3</p>
</div>
<p class="Quotation">Quotation</p>
<p class="Extract1">Extract1</p>
<p class="Extract2">Extract2</p>
</section>
</section>
<h6 class="E-Head">E-Head</h6>
<section>
<h6 class="F-Head">F-Head</h6>
<section>
<p class="Body-Text">Body-Text</p>
<p class="Body-Text-Continued">Body-Text-Continued</p>
<div class="verse_group">
<p class="Verse1">Verse1</p>
<p class="Verse1-indent1">Verse1-indent1</p>
<p class="Verse1-indent2">Verse1-indent2</p>
<p class="Verse1-indent3">Verse1-indent3</p>
<p class="Verse2">Verse2</p>
<p class="Verse2-indent1">Verse2-indent1</p>
<p class="Verse2-indent2">Verse2-indent2</p>
<p class="Verse2-indent3">Verse2-indent3</p>
</div>
<p class="Quotation">Quotation</p>
<p class="Extract1">Extract1</p>
<p class="Extract2">Extract2</p>
</section>
</section>
</section>
</body>
</html>

topic.p模板的我的Xslt模板代码:

代码语言:javascript
复制
<xsl:template match="*[contains(@class, ' topic/p ')]" name="topic.p">
<xsl:variable name="topicatt"><xsl:value-of select="@outputclass" /></xsl:variable>
<xsl:choose>
      <xsl:when test="descendant::*[contains(@class, ' topic/pre ')] or
        descendant::*[contains(@class, ' topic/ul ')] or
        descendant::*[contains(@class, ' topic/sl ')] or
        descendant::*[contains(@class, ' topic/ol ')] or
        descendant::*[contains(@class, ' topic/lq ')] or
        descendant::*[contains(@class, ' topic/dl ')] or
        descendant::*[contains(@class, ' topic/note ')] or
        descendant::*[contains(@class, ' topic/lines ')] or
        descendant::*[contains(@class, ' topic/fig ')] or
        descendant::*[contains(@class, ' topic/table ')] or
        descendant::*[contains(@class, ' topic/simpletable ')]">
        <div class="p">
          <xsl:call-template name="commonattributes"/>
          <xsl:call-template name="setid"/>
          <xsl:apply-templates/>
        </div>
      </xsl:when>
      <xsl:when test="$topicatt = 'E-Head'">
        <h6 class="E-Head"><xsl:apply-templates/></h6>
      </xsl:when>
      <xsl:when test="$topicatt = 'F-Head'">
        <h6 class="F-Head"><xsl:apply-templates/></h6>
      </xsl:when>
     <xsl:otherwise>
        <xsl:call-template name="output_p_element"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:value-of select="$newline"/>
  </xsl:template>

  <xsl:template name="output_p_element">
    <p>
      <xsl:call-template name="commonattributes"/>
      <xsl:call-template name="setid"/>
      <xsl:if test="contains(@outputclass, 'EntryIdx')">
        <xsl:call-template name="set_index_entry_class"/>
      </xsl:if>
      <xsl:apply-templates/>
    </p>
  </xsl:template>

在div元素中添加verse输出类元素。

请提个建议。

EN

回答 1

Stack Overflow用户

发布于 2018-09-03 21:05:03

如果您唯一的任务是使用outputclass元素中的Verse字符串包装相邻的p元素,那么这是一个使用XSLT2/3的for-each-group group-adjacent的教科书示例(例如,参见示例“在https://www.w3.org/TR/xslt20/#grouping-examples中对元素的交替序列进行分组”)

代码语言:javascript
复制
  <xsl:template match="body">
      <xsl:copy>
          <xsl:for-each-group select="*" group-adjacent="starts-with(@outputclass, 'Verse')">
              <xsl:choose>
                  <xsl:when test="current-grouping-key()">
                      <div class="verse_group">
                          <xsl:apply-templates select="current-group()"/>
                      </div>
                  </xsl:when>
                  <xsl:otherwise>
                      <xsl:apply-templates select="current-group()"/>
                  </xsl:otherwise>
              </xsl:choose>
          </xsl:for-each-group>
      </xsl:copy> 
  </xsl:template>

完整示例请访问https://xsltfiddle.liberty-development.net/jyH9rMK

你似乎也想嵌套内容,我怀疑用for-each-group group-starting-with="*[@outputclass = 'E-Head']"嵌套另一个F-Head,然后嵌套在上面的group-adjacent (其中所有嵌套的分组都在current-group()上工作,而不是完全选择*p )可以解决这个问题,但将它们分成不同的问题可能会更好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52148338

复制
相关文章

相似问题

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