Q: Out of order document

  • Thread starter G. Ralph Kuntz, MD, MS
  • Start date
G

G. Ralph Kuntz, MD, MS

How do people deal with out-of-order document? Let me clarify.

Let's say we have an input document having the following format:

<document>
section 1
section 2
section 3
</document>

we want the resulting output document to have the structure

<document>
section 3
section 2
section 1
</document>

Since the xsl template matching will match section 1, then section 2,
etc. how do we get the output into the format we want?

Do you use param(s) to store intermediate values?

Thanks, Ralph
 
J

Johannes Koch

How do people deal with out-of-order document? Let me clarify.

Let's say we have an input document having the following format:

<document>
section 1
section 2
section 3
</document>

we want the resulting output document to have the structure

<document>
section 3
section 2
section 1
</document>

Since the xsl template matching will match section 1, then section 2,
etc. how do we get the output into the format we want?

xsl:sort?
 
M

Martin Honnen

How do people deal with out-of-order document? Let me clarify.

Let's say we have an input document having the following format:

<document>
section 1
section 2
section 3
</document>

we want the resulting output document to have the structure

<document>
section 3
section 2
section 1
</document>

Since the xsl template matching will match section 1, then section 2,
etc. how do we get the output into the format we want?

Here is an example, assuming the document element has section child
elements like this

<document>
<section>section 1</section>
<section>section 2</section>
<section>section 3</section>
</document>

then you can process them and sort in descending order

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:eek:utput method="xml" indent="yes"/>

<xsl:template match="document">
<xsl:copy>
<xsl:apply-templates select="section">
<xsl:sort select="position()" order="descending"
data-type="number"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

to reverse the original order.
 
J

Joseph Kesselman

Do you really mean the "section" lines to be raw text, as you've shown
them? If so, you need to do that with string processing, which is
possible but somewhat awkward in XSLT.

If they have XML structure, then as others have said you can use
sorting... or, if you know the expected sections in advance, you can
simply have the template for <document> explicitly process them in a
specific order by using more specific apply-template selections.
 
G

G. Ralph Kuntz, MD, MS

I meant xml sub-sections. I found the answer. I did not know that
<xsl:apply-templates> could take an argument.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top