Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   Q: Out of order document (http://www.velocityreviews.com/forums/t591682-q-out-of-order-document.html)

G. Ralph Kuntz, MD, MS 02-14-2008 01:07 PM

Q: Out of order document
 
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

Johannes Koch 02-14-2008 01:08 PM

Re: Q: Out of order document
 
G. Ralph Kuntz, MD, MS schrieb:
> 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?
--
Johannes Koch
In te domine speravi; non confundar in aeternum.
(Te Deum, 4th cent.)

Martin Honnen 02-14-2008 02:16 PM

Re: Q: Out of order document
 
G. Ralph Kuntz, MD, MS wrote:
> 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:output 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.



--

Martin Honnen
http://JavaScript.FAQTs.com/

Joseph Kesselman 02-14-2008 02:51 PM

Re: Q: Out of order document
 
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.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden

G. Ralph Kuntz, MD, MS 02-14-2008 04:50 PM

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

Thanks.


All times are GMT. The time now is 12:32 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.