如何在连续出现时合并 'listBulleted' 元素
HOW TO MEARGE THE 'listBulleted' ELEMENT WHILE COMING CONSCUTVELY
元素 'listBulleted' 具有相同的父元素,这里我们试图仅合并那些具有 conscutive/countinued 的 'listBulleted' 元素,否则保持原样:
注意:我们正在创建特定的“”模板,以便我们可以在其他 xslt 中轻松 moved/add。
输入XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<listBulleted>
<listItem><para flow="new"><emphasis bold="yes">Design.</emphasis> Approval of the specific design of the tiebacks or underpinning.</para>
<listBulleted>
<listItem><para flow="new">The location of the tiebacks.</para></listItem>
<listItem><para flow="new">The design of underpinning will be critical.</para></listItem>
</listBulleted></listItem>
<listItem><para flow="new"><emphasis bold="yes">Design Professionals and Contractors.</emphasis>.</para></listItem>
</listBulleted>
<listBulleted>
<listItem><para flow="new"><emphasis bold="yes">Means of Installation.</emphasis> Approval of the timing.</para></listItem>
</listBulleted>
<listBulleted>
<listItem><para flow="new"><emphasis bold="yes">Liability for Damages.</emphasis> The constructing party’s.</para></listItem>
</listBulleted>
<listBulleted>
<listItem><para flow="new"><emphasis bold="yes">Adjacent Property Owner’s Rights</emphasis></para>
<listBulleted>
<listItem><para flow="new">An acknowledgment by the constructing party.</para></listItem>
<listItem><para flow="new">Assurances for the constructing party.</para></listItem>
<listItem><para flow="new">The assurances described in the preceding recorded.</para></listItem>
</listBulleted></listItem>
</listBulleted>
<listBulleted>
<listItem><para flow="new"><emphasis bold="yes">Insurance.</emphasis> Description.</para></listItem>
</listBulleted>
</root>
预期输出:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<listBulleted>
<listItem><para flow="new"><emphasis bold="yes">Design.</emphasis> Approval of the specific design of the tiebacks or underpinning.</para>
<listBulleted>
<listItem><para flow="new">The location of the tiebacks.</para></listItem>
<listItem><para flow="new">The design of underpinning will be critical.</para></listItem>
</listBulleted></listItem>
<listItem><para flow="new"><emphasis bold="yes">Design Professionals and Contractors.</emphasis>.</para></listItem>
<listItem><para flow="new"><emphasis bold="yes">Means of Installation.</emphasis> Approval of the timing.</para></listItem>
<listItem><para flow="new"><emphasis bold="yes">Liability for Damages.</emphasis> The constructing party’s.</para></listItem>
<listItem><para flow="new"><emphasis bold="yes">Adjacent Property Owner’s Rights</emphasis></para>
<listBulleted>
<listItem><para flow="new">An acknowledgment by the constructing party.</para></listItem>
<listItem><para flow="new">Assurances for the constructing party.</para></listItem>
<listItem><para flow="new">The assurances described in the preceding recorded.</para></listItem>
</listBulleted></listItem>
<listItem><para flow="new"><emphasis bold="yes">Insurance.</emphasis> Description.</para></listItem>
</listBulleted>
</root>
XSLT 代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="listBulleted">
<listBulleted>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
<xsl:if test="following-sibling::node()[1][self::listBulleted]">
<xsl:apply-templates select="following-sibling::node()[1][self::listBulleted]/node()"/>
</xsl:if>
</listBulleted>
</xsl:template>
<xsl:template match="listBulleted[preceding-sibling::node()[1][self::listBulleted]]"/>
</xsl:stylesheet>
教科书方法是group-adjacent
:
<xsl:template match="*[listBulleted]">
<xsl:copy>
<xsl:for-each-group select="*" group-adjacent=". instance of element(listBulleted)">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:copy>
<xsl:apply-templates select="current-group()/node()"/>
</xsl:copy>
</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/6q1S8Az/2
它与 listBulleted
不匹配,因为那不是您需要分组的容器。
元素 'listBulleted' 具有相同的父元素,这里我们试图仅合并那些具有 conscutive/countinued 的 'listBulleted' 元素,否则保持原样:
注意:我们正在创建特定的“
输入XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<listBulleted>
<listItem><para flow="new"><emphasis bold="yes">Design.</emphasis> Approval of the specific design of the tiebacks or underpinning.</para>
<listBulleted>
<listItem><para flow="new">The location of the tiebacks.</para></listItem>
<listItem><para flow="new">The design of underpinning will be critical.</para></listItem>
</listBulleted></listItem>
<listItem><para flow="new"><emphasis bold="yes">Design Professionals and Contractors.</emphasis>.</para></listItem>
</listBulleted>
<listBulleted>
<listItem><para flow="new"><emphasis bold="yes">Means of Installation.</emphasis> Approval of the timing.</para></listItem>
</listBulleted>
<listBulleted>
<listItem><para flow="new"><emphasis bold="yes">Liability for Damages.</emphasis> The constructing party’s.</para></listItem>
</listBulleted>
<listBulleted>
<listItem><para flow="new"><emphasis bold="yes">Adjacent Property Owner’s Rights</emphasis></para>
<listBulleted>
<listItem><para flow="new">An acknowledgment by the constructing party.</para></listItem>
<listItem><para flow="new">Assurances for the constructing party.</para></listItem>
<listItem><para flow="new">The assurances described in the preceding recorded.</para></listItem>
</listBulleted></listItem>
</listBulleted>
<listBulleted>
<listItem><para flow="new"><emphasis bold="yes">Insurance.</emphasis> Description.</para></listItem>
</listBulleted>
</root>
预期输出:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<listBulleted>
<listItem><para flow="new"><emphasis bold="yes">Design.</emphasis> Approval of the specific design of the tiebacks or underpinning.</para>
<listBulleted>
<listItem><para flow="new">The location of the tiebacks.</para></listItem>
<listItem><para flow="new">The design of underpinning will be critical.</para></listItem>
</listBulleted></listItem>
<listItem><para flow="new"><emphasis bold="yes">Design Professionals and Contractors.</emphasis>.</para></listItem>
<listItem><para flow="new"><emphasis bold="yes">Means of Installation.</emphasis> Approval of the timing.</para></listItem>
<listItem><para flow="new"><emphasis bold="yes">Liability for Damages.</emphasis> The constructing party’s.</para></listItem>
<listItem><para flow="new"><emphasis bold="yes">Adjacent Property Owner’s Rights</emphasis></para>
<listBulleted>
<listItem><para flow="new">An acknowledgment by the constructing party.</para></listItem>
<listItem><para flow="new">Assurances for the constructing party.</para></listItem>
<listItem><para flow="new">The assurances described in the preceding recorded.</para></listItem>
</listBulleted></listItem>
<listItem><para flow="new"><emphasis bold="yes">Insurance.</emphasis> Description.</para></listItem>
</listBulleted>
</root>
XSLT 代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="listBulleted">
<listBulleted>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
<xsl:if test="following-sibling::node()[1][self::listBulleted]">
<xsl:apply-templates select="following-sibling::node()[1][self::listBulleted]/node()"/>
</xsl:if>
</listBulleted>
</xsl:template>
<xsl:template match="listBulleted[preceding-sibling::node()[1][self::listBulleted]]"/>
</xsl:stylesheet>
教科书方法是group-adjacent
:
<xsl:template match="*[listBulleted]">
<xsl:copy>
<xsl:for-each-group select="*" group-adjacent=". instance of element(listBulleted)">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:copy>
<xsl:apply-templates select="current-group()/node()"/>
</xsl:copy>
</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/6q1S8Az/2
它与 listBulleted
不匹配,因为那不是您需要分组的容器。