Questions: Merging various xml structures into one

Discussion in 'XML' started by q-rious, Mar 13, 2011.

  1. q-rious

    q-rious Guest

    I have the following in a single file (though if a solution to this
    problem requires it, I can divide into multiple files).

    <a>
    <b></b>
    <c>
    <b></b>
    </c>
    </a>

    <b>
    <x>
    <y></y>
    </x>
    </b>

    I want this:

    <a>
    <x>
    <y></y>
    </x>
    <c>
    <x>
    <y></y>
    </x>
    </c>
    </a>

    I would like to do this using Perl, but I am open to other methods as
    well.

    Thanks in advance.
     
    q-rious, Mar 13, 2011
    #1
    1. Advertising

  2. q-rious

    Peter Flynn Guest

    On 13/03/11 01:11, q-rious wrote:
    > I have the following in a single file (though if a solution to this
    > problem requires it, I can divide into multiple files).
    >
    > <a>
    > <b></b>
    > <c>
    > <b></b>
    > </c>
    > </a>
    >
    > <b>
    > <x>
    > <y></y>
    > </x>
    > </b>


    There must be some container around this for it to be a single XML document.

    > I want this:
    >
    > <a>
    > <x>
    > <y></y>
    > </x>


    That is presumably the x element from within the original third b
    element, yes?

    > <c>
    > <x>
    > <y></y>
    > </x>


    And that is presumably the same x element repeated a second time?

    > </c>
    > </a>


    I'm sure there is a reason for such a redundant structure...

    > I would like to do this using Perl, but I am open to other methods as
    > well.


    When all you have is a hammer, everything looks like a nail.

    Using XSLT, and assuming a container called doc:

    ---test.xml---
    <doc><a><b/><c><b/></c></a><b><x><y/></x></b></doc>

    ---test.xsl---
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:eek:utput method="xml"/>

    <xsl:template match="/">
    <a>
    <xsl:copy-of select="doc/b/x"/>
    <c>
    <xsl:copy-of select="doc/b/x"/>
    </c>
    </a>
    </xsl:template>

    </xsl:stylesheet>

    ---output---
    <a><x><y/></x><c><x><y/></x></c></a>

    ///Peter
     
    Peter Flynn, Mar 13, 2011
    #2
    1. Advertising

  3. El 13/03/2011 13:42, Peter Flynn escribió:
    > On 13/03/11 01:11, q-rious wrote:
    >> I have the following in a single file (though if a solution to this
    >> problem requires it, I can divide into multiple files).
    >>
    >> <a>
    >> <b></b>
    >> <c>
    >> <b></b>
    >> </c>
    >> </a>
    >>
    >> <b>
    >> <x>
    >> <y></y>
    >> </x>
    >> </b>

    >
    > There must be some container around this for it to be a single XML document.


    Yes!

    >
    >> I want this:
    >>
    >> <a>
    >> <x>
    >> <y></y>
    >> </x>

    >
    > That is presumably the x element from within the original third b
    > element, yes?
    >
    >> <c>
    >> <x>
    >> <y></y>
    >> </x>

    >
    > And that is presumably the same x element repeated a second time?
    >
    >> </c>
    >> </a>

    >
    > I'm sure there is a reason for such a redundant structure...
    >
    >> I would like to do this using Perl, but I am open to other methods as
    >> well.

    >
    > When all you have is a hammer, everything looks like a nail.
    >
    > Using XSLT, and assuming a container called doc:
    >
    > ---test.xml---
    > <doc><a><b/><c><b/></c></a><b><x><y/></x></b></doc>
    >
    > ---test.xsl---
    > <?xml version="1.0" encoding="utf-8"?>
    > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > version="1.0">
    > <xsl:eek:utput method="xml"/>
    >
    > <xsl:template match="/">
    > <a>
    > <xsl:copy-of select="doc/b/x"/>
    > <c>
    > <xsl:copy-of select="doc/b/x"/>
    > </c>
    > </a>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    >
    > ---output---
    > <a><x><y/></x><c><x><y/></x></c></a>


    Assuming that each <b> inside <a> is just a placeholder to be replaced
    by the "real" <b>, the XSLT code should be:

    > ---test.xsl---

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:eek:utput method="xml"/>

    <xsl:template match="/"> <!-- process just the first element -->
    <xsl:apply-templates select="doc/a"/>
    </xsl:template>

    <xsl:template match="b"> <!-- expand "b" placeholders -->
    <xsl:copy-of select="/doc/b/*"/>
    </xsl:template>

    <xsl:template match="*"> <!-- copy everything else unchanged -->
    <xsl:copy select=".">
    <xsl:apply-templates/>
    </xsl:copy>
    </xsl:template>

    </xsl:stylesheet>

    This gives the same output.
    --
    Manuel Collado - http://lml.ls.fi.upm.es/~mcollado
     
    Manuel Collado, Mar 13, 2011
    #3
    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. JNU
    Replies:
    3
    Views:
    371
    Roedy Green
    Sep 3, 2003
  2. Mark
    Replies:
    3
    Views:
    706
    Oliver Wong
    Mar 31, 2006
  3. Replies:
    2
    Views:
    17,425
    Philippe Poulard
    Aug 24, 2006
  4. Alfonso Morra
    Replies:
    11
    Views:
    754
    Emmanuel Delahaye
    Sep 24, 2005
  5. Fred
    Replies:
    12
    Views:
    627
    Jack Klein
    May 4, 2007
Loading...

Share This Page