Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

xml - HOW TO MEARGE THE 'listBulleted' ELEMENT WHILE COMING CONSCUTVELY

The element 'listBulleted' have same parent and here we are trying to mearge only those 'listBulleted' elements which have conscutive/countinued otherwise keep as it is:

NOTE: We are creating specific '<xsl:template match="listBulleted">' template so we can easely moved/add in other xslt.

INPUT XML: enter image description here

<?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>

EXPECTED OUTPUT: enter image description here

<?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 CODE:

<?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>

Reference URL : https://xsltfiddle.liberty-development.net/6q1S8Az/1

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The text book approach is 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

It doesn't match on listBulleted, however, as that is not the container where you need to group.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...