0
votes

I have a xml file like this,

<content>
    <ol>
        <li> List<span style="color: rgb(255,0,255);">The scopeeeee of</span>this project is
            <ol>
                <li>nested list1</li>
                <li>nested <span style="color: rgb(255,0,255);">The scope of this project is</span> list1</li>
                <li>nested list1</li>
                <li>nested list1</li>
                <li>nested list1</li>
            </ol>
        </li>
        <li>
            <p>List<span style="color: rgb(255,0,255);">The scope of this project is to:</span></p>
        </li>
        <li>
            <p>List<span style="color: rgb(255,0,255);">The scope of this project is to:</span></p>
        </li>
    </ol>
</content>

I need to transform above xml to following xml using XSLT

expected output,

<content>
    <orderedList>
        <liItem>
            <para>List<c type="color: rgb(255,0,255);">The scopeeeee of this project is to:</c>jkhsdlkjfh</para>
        </liItem>
        <orderedList>
            <liItem>
                <para>nested list1</para>
            </liItem>
            <liItem>
                <para>nested <c type="color: rgb(255,0,255);">The scope of this project is</c>list1</para>
            </liItem>
            <liItem>
                <para>nested list1</para>
            </liItem>
            <liItem>
                <para>nested list1</para>
            </liItem>
            <liItem>
                <para>nested list1</para>
            </liItem>
        </orderedList>

        <liItem>
            <para> List<c type="color: rgb(255,0,255);">The scope of this project is to:</c></para>
        </liItem>
        <liItem>
            <para> List<c type="color: rgb(255,0,255);">The scope of this project is to:</c></para>
        </liItem>
    </orderedList>
</content>

As you can see, the text content and span nodes within li items has to cover with <para> node in output.

I have written following xsl to get this output,

<xsl:template match="ol">
        <orderedList>
            <xsl:apply-templates/>
        </orderedList>
    </xsl:template>

    <xsl:template match="li[not(descendant::ol)]" >
        <liItem>
            <para>
                <xsl:apply-templates />
            </para>
        </liItem>
    </xsl:template>

    <xsl:template match="li[descendant::ol]" >
        <liItem>
            <para>
                <xsl:apply-templates select="node()[parent::li][following-sibling::ol]"/>
            </para>
        </liItem>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="span">
        <c type="{@style}">
            <xsl:apply-templates/>
        </c>
    </xsl:template>

    <xsl:template match="li/p">
        <xsl:apply-templates />
    </xsl:template> 

The only problem of above xsl is comes when it has <ol> inside another <ol> item. I'm struggling to find a way add <para> node to the text content which placed between <li> and <ol> nodes.

current output is follows,

<content>
    <orderedList>
        <liItem>
            <para> List<c type="color: rgb(255,0,255);">The scopeeeee of</c>this project is</para>
        </liItem> List<c type="color: rgb(255,0,255);">The scopeeeee of</c>this project is
        <orderedList>
            <liItem><para>nested list1</para></liItem>
            <liItem><para>nested <c type="color: rgb(255,0,255);">The scope of this project is</c>list1</para></liItem>
            <liItem><para>nested list1</para></liItem>
            <liItem><para>nested list1</para></liItem>
            <liItem><para>nested list1</para></liItem>
        </orderedList>
        <liItem><para> List<c type="color: rgb(255,0,255);">The scope of this project is to:</c></para></liItem>
        <liItem><para> List<c type="color: rgb(255,0,255);">The scope of this project is to:</c></para></liItem>
    </orderedList>
</content>

can anyone suggest me a way how can I modify my code to get the expected output..

1

1 Answers

4
votes

If the ol element is always going to come after the text in the li element, you could just change the template to this

<xsl:template match="li[descendant::ol]" >
    <liItem>
        <para>
            <xsl:apply-templates select="node()[following-sibling::ol]"/>
        </para>
    </liItem>
    <xsl:apply-templates select="ol"/>
</xsl:template>

Or maybe this, if you actually wanted the ol element inside the liItem but just not in the para

<xsl:template match="li[descendant::ol]" >
    <liItem>
        <para>
            <xsl:apply-templates select="node()[following-sibling::ol]"/>
        </para>
        <xsl:apply-templates select="ol"/>
    </liItem>
</xsl:template>

In either case, you could combine the two templates matching li into a single template; like so:

<xsl:template match="li" >
    <liItem>
        <para>
            <xsl:apply-templates select="node() except ol"/>
        </para>
        <xsl:apply-templates select="ol"/>
    </liItem>
</xsl:template>

Alternatively, if you wanted to cope with multiple ol elements within a single list item, or if you had text after the ol element too, you could make use of xsl:for-each-group

<xsl:template match="li" >
    <liItem>
        <xsl:for-each-group select="node()" group-adjacent="boolean(self::ol)">
            <xsl:choose>
                <xsl:when test="current-grouping-key() or not(normalize-space())">
                    <xsl:apply-templates select="current-group() "/>
                </xsl:when>
                <xsl:otherwise>
                    <para>
                        <xsl:apply-templates select="current-group() "/>
                    </para>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </liItem>
</xsl:template>