What am I missing re: very simple XML->XML transform

D

Duane Morin

Ok, I've got a situation where I have an XML that looks something like
this:
<A>
<B>
<C/>
</B>
<D>
<C/>
</D>
<E>
</E>
</A>
....
basically the relevant descriptor is "There are some instances of <C>
in the file in non-easily predictable spots."

What I want to do is transform this file so that it looks like this:
<A>
<B>
<C/><MyTag/>
</B>
<D>
<C/><MyTag/>
</D>
...
In other words "Find all <C> tags and insert <MyTag> after it."

I am at a loss as to how to do this. I can get the "Find all C's and
do this afterward..." but how to I say "And leave the rest of the file
exactly as it is?" Can I even do that? Do I have to explicitly say
"Find A, write A, find B, write B...find C, write C and then write
MyTag...."?? That can't be right, can it?
 
M

Marten Gaans

Duane Morin wrote:
....
In other words "Find all <C> tags and insert <MyTag> after it."

I am at a loss as to how to do this. I can get the "Find all C's and
do this afterward..." but how to I say "And leave the rest of the file
exactly as it is?" Can I even do that? Do I have to explicitly say
"Find A, write A, find B, write B...find C, write C and then write
MyTag...."?? That can't be right, can it?

start with the 'identity template', which copies your document recursively:

--
<?xml version="1.0" encoding="ISO-8859-1"?>

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

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

</xsl:stylesheet>
--

and add templates for elements, you want modified/deleted... , in your case:

....
<xsl:template match="C">
<xsl:copy-of select=".">
<MyTag/>
</xsl:template>
....


m.
 
D

Duane Morin

Great! Thanks for the help! Unfortunately I get a SAXParseException,
"node in step pattern not implemented." It points at the first
match="node()|@*" line.

Is my SAX parser just out of date, or is this a DOM-only thing? We're
using jclark's, I think circa 1999. It's what was here when I got
here. :)

Duane
 
M

Marten Gaans

Duane said:
Great! Thanks for the help! Unfortunately I get a SAXParseException,
"node in step pattern not implemented." It points at the first
match="node()|@*" line.

try to replace 'node()' with '*', shouldn't make a difference for your example.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top