Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Home » Tip Bank » XML » XSLT
Language: XSLT
Expertise: Beginner
Nov 10, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Variable Length Ordered Lists Using XSL


I'm attempting to create an ordered list every time the Sequence attribute of a Para element has been set to 'Begin'. The list should continue until it reaches a paragraph with the Sequence attribute set to 'End'. The problem arises because the

are not being properly matched within the XSL document. I have tried various combinations of
    , CData, and CDATA sections.

  1. Answer:

    This can be a real pain—I've run into similar situations, where you need to be able to wrap a tag around an expression but the tag start and tag end are generated programmatically (and hence appear "unbalanced" to the XSL document). The simplest solution (and it isn't terribly simple), is to recognize that you need to abstract your structure out one level. What you have is this:

    First Paragraph
    Second Paragraph
    Third Paragraph
    Fourth Paragraph
    Fifth Paragraph
    Sixth Paragraph

    The problem with this code is that you're actually subverting the intrinsic structure of the code, flattening the hierarchy too much. There is no clean way to write an XPath query that will satisfy this. However, if you recognized that each of these items exist in groups, then you could revamp your structure a bit:

       First Paragraph
       Second Paragraph
       Third Paragraph
       Fourth Paragraph
       Fifth Paragraph
       Sixth Paragraph

    Then the XSL transform becomes much simpler:

  2. This actually brings up a good point to remember about XSL—in general, whenever you have an order issue involved (or, as in this example, you seem to have unbalanced elements), this is typically an indication that your hierarchy is too flat. By deepening the hierarchy, encapsulating it if you will, you can usually solve such problems.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date