Questions: Merging various xml structures into one

Q

q-rious

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.
 
P

Peter Flynn

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
 
M

Manuel Collado

El 13/03/2011 13:42, Peter Flynn escribió:
There must be some container around this for it to be a single XML document.
Yes!


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


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


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


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
---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.
 

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,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top