xml - 如何在一个 div 中组合不同的元素
问题描述
verse outputclass 元素中的一组元素 div elment 到 verse 元素中,如下格式所示
我的 XML 代码是:
<?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>
需要输出作为输出类中使用的任何“诗句”,我们希望在诗句元素组合完成后作为 div 根元素结束,如下所示:
<!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>
MY Xslt 模板 topic.p 模板的代码:
<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 输出类元素。
请建议。
解决方案
如果您唯一的任务是用元素中的字符串包装相邻p
元素,那么这是一个教科书示例(请参阅https://www.w3.org/TR/xslt20/#grouping-examples中的示例“组合元素的交替序列”例如)使用 XSLT 2/3 的:Verse
outputclass
for-each-group group-adjacent
<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
当然)可以解决这个问题但最好将其分解为不同的问题。
推荐阅读
- ip - 主机部分不为零的子网
- firebase - 错误:向 Firebase 发送 ios 推送时出现 InvalidApnsCredential,添加了身份验证密钥和证书
- javascript - Wordpress Appyn 主题加载时间问题
- scala - 将单行文件读入数据框
- flutter - 如何在颤动中为滚动条提供渐变颜色?
- java - 如何在 kotlin 中为测试创建 ApplicationContext?
- oracle - 将布尔列添加到 oracle 数据库的现有表中
- reactjs - 无法在 nextjs 中传递 slug 值 getStaticpath 方法
- angular - 从 @angular/google-maps 获取 google.maps.Map 实例
- hive - 在配置单元中选择和插入时出错 - KryoException