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

Discussion in 'XML' started by Duane Morin, Jan 26, 2004.

  1. Duane Morin

    Duane Morin Guest

    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?
    Duane Morin, Jan 26, 2004
    #1
    1. Advertising

  2. Duane Morin

    Marten Gaans Guest

    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.
    Marten Gaans, Jan 26, 2004
    #2
    1. Advertising

  3. Duane Morin

    Duane Morin Guest

    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



    Marten Gaans <> wrote in message news:<-ip.de>...
    > <?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.
    Duane Morin, Jan 26, 2004
    #3
  4. Duane Morin

    Marten Gaans Guest

    Duane Morin wrote:
    > 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.
    Marten Gaans, Jan 26, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    451
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,592
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    409
    jacob navia
    Oct 17, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    498
    Dave Thompson
    Feb 14, 2005
  5. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,246
Loading...

Share This Page