Using XSLT to Order Nodes

N

Newbie

The answer to this is probably obvious, but I'm somewhat new to XSLT and can't
find it.

I need to sort a set of nodes within a document, e.g.

<A>
<B/>
<C>
<D>3</D>
<D>2</D>
<D>4</D>
<D>1</D>
</C>
<F/>
</A>

How should I construct an XSLT to output

<A>
<B/>
<C>
<D>1</D>
<D>2</D>
<D>3</D>
<D>4</D>
</C>
<F/>
</A>

where everything outside C is arbitrary? I know about sort, but I don't know how
to output everything around C.

Thanks for any help.
 
J

Joe Kesselman

Newbie said:
where everything outside C is arbitrary? I know about sort, but I don't
know how to output everything around C.

Start with the "identity stylesheet", then add a template which will do
the special handling needed for match="C".
 
N

Newbie

Joe said:
Start with the "identity stylesheet", then add a template which will do
the special handling needed for match="C".

Found a reference to "identity stylesheet" in
http://hacks.oreilly.com/pub/h/2070, but a comment warns about problems with
CDATA. What is the problem here (I can't find any more info), and is there an
easy way around it (I can't control the input)?
 
M

Martin Honnen

Newbie said:
Found a reference to "identity stylesheet" in
http://hacks.oreilly.com/pub/h/2070, but a comment warns about problems
with CDATA. What is the problem here (I can't find any more info), and
is there an easy way around it (I can't control the input)?

The XPath/XSLT data model does not know CDATA sections, it only knows
text nodes. If you transform XML to XML with a simple identity
transformation then CDATA sections might not be preserved but in terms
of well-formedness everything will be fine, only instead of having a
CDATA section to escape things character/entity references will be used
to escape things.
You can however define
<xsl:eek:utput cdata-section-elements="put element names here" />
to have the XSLT processor create CDATA sections as element contents
when serializing that result tree.
That way you can determine which elements in the result should have
CDATA section content. Only it does not help if you want to write a
generic stylesheet processing abritary elements you don't know the names of.
 
J

Joe Kesselman

Of course no modern XML application should care whether you've used
CDATA sections or ordinary text with character-by-character escaping
when needed.

Unfortunately some tools carried over from the HTML world are broken in
that regard.
 
N

Newbie

Martin said:
The XPath/XSLT data model does not know CDATA sections, it only knows
text nodes. If you transform XML to XML with a simple identity
transformation then CDATA sections might not be preserved but in terms
of well-formedness everything will be fine, only instead of having a
CDATA section to escape things character/entity references will be used
to escape things.

Thank you, hopefully this will suffice.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top