1

I wanted to add <p> tag for every <li> elements, but there is a special situation when it is a nested list. If there is descendant <ol> tag, this adding <p> must be closed before this <ol> tag. Although it is happened, I could not be able to remove the <s>test tag</s><br/>part repetition.

Please help!

Input:

<a>
    <ol>
        <li>
            <s>test tag</s><br/>
            <ol>
                <li>list item1</li>
                <li>list item2</li>
                <li>list item3</li>
            </ol>
        </li>
    </ol>
</a>

Expected output:

<a>
    <ol>
        <li>
            <p><s>test tag</s><br/></p>
            <ol>

                    <p><li>list item1</li></p>
                    <p><li>list item1</li></p>
                    <p><li>list item1</li></p>

            </ol>
        </li>
    </ol>
</a>

XSLT code:

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

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

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

<xsl:template match="node()[parent::li][following-sibling::ol]"/>
4
  • Your expected output has "ol" as a child of "ol". Why would you want to do that? Commented Oct 3, 2015 at 9:25
  • every <li> tag should be within <p> tag..but if any <li> has decendant <ol> tag that <li> tag should be closed before the decendant <ol> tag..I think it is clear for you @MichaelKay..Thank you Commented Oct 3, 2015 at 15:15
  • My comment is no longer relevant, since you have changed the question since I made the comment. But you still seem to be trying to produce invalid HTML: your desired output now has p as a child of ol. I'm not going to help you produce invalid HTML unless you explain why you want to do so. Commented Oct 5, 2015 at 13:18
  • @MichaelKay This is not a actual html page.This is a xml page.Only the concept is list.So I used <li> and <ol> as my input XML file.So I think giving help is not a problem here. Thanks Commented Oct 13, 2015 at 6:05

1 Answer 1

1

In the case where there is no descendant ol, your desired output has a p element outside each li, whereas your stylesheet is putting a p inside each ol. I don't know which you actually want, but I assume you know how to fix it.

In the case where there is a descendant ol, you have only given one possible example of what you want, and one example does not constitute a specification. However, for that example, the following would work:

<xsl:template match="li[ol]">
    <li>
        <p>
            <xsl:apply-templates select="node()[not(self::ol)]"/>
        </p>
        <xsl:apply-templates select="ol"/>
    </li>
</xsl:template>

If this doesn't solve the problem in the general case, that's because you haven't said what's required in the general case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.