Jump to content

XSLT Urgent Question; Merge 2 XML docs


Guest LG2006

Recommended Posts

Guest LG2006

I have the following two XML documents:The question is how does the XSLT document has to look like, to get them together,to get the data from the second one into the first. Can somebody help me? Must be really easy for pros, but I'm a rookie. :) This is a question for a exam-preparation. :) I hope someone helps me with this.TIA!!!!!!XML-Dokument NUMBER1:<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE Mitarbeiter SYSTEM “mitarbeiter.dtd”><firma><abteilung name = "kredit"><mitarbeiter><name>Mueller</name><vorname>Stefan</vorname><telefon>2415</telefon></mitarbeiter><mitarbeiter><name>Thoma</name><vorname>Ludwig</vorname><telefon>3060</telefon></mitarbeiter><mitarbeiter><name>Strauss</name><vorname>Richard</vorname><telefon>1328</telefon></mitarbeiter></abteilung></firma>XML-Dokument NUMBER2:<!DOCTYPE Mitarbeiterliste SYSTEM “mitarbeiterliste.dtd”><firma> <abteilung name = "produktmanagement"> <mitarbeiter> <name>Einstein</name> <vorname>Albert</vorname> <telefon>08621/601593</telefon></mitarbeiter> <mitarbeiter> <name>Maier</name> <vorname>Heinz</vorname><telefon>08621/700123</telefon></mitarbeiter> </abteilung> </firma>

Link to comment
Share on other sites

Simple enough, scince both XMLs have the same type of structure...

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">	<xsl:template match="/">		<firma>			<xsl:apply-templates/>		</firma>	</xsl:template>	<xsl:template match="/firma">		<xsl:copy-of select="."/>		<xsl:copy-of select="document('xml2.xml')/firma"/>	</xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

Simple enough, scince both XMLs have the same type of structure...
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">	<xsl:template match="/">		<firma>			<xsl:apply-templates/>		</firma>	</xsl:template>	<xsl:template match="/firma">		<xsl:copy-of select="."/>		<xsl:copy-of select="document('xml2.xml')/firma"/>	</xsl:template></xsl:stylesheet>

and if the xml have not the same structure ?... and then for read a single element, what about ?Andrea
Link to comment
Share on other sites

Sorry. Didn't had Internet for quite a while :) ...Anyway, how to merge them if they don't have the same structure... it depends:

  • Which one of the two document's root element is going to be the new XML's root element? Or is it going to be a new root element, defined in the XSLT itself?
  • Does any of the merged document need to be transformed into somekind, or should it be copied directly as with the first example?
  • What type of structure is the new XML expected to hold? Any difference with the original?

Let's me go into the easiest scenario first:

<xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">	<xsl:template match="/*">		<xsl:element name="{name()}>			<xsl:copy-of select="*"/>			<xsl:copy-of select="document('xml2.xml')/*/*"/>		</xsl:element>	</xsl:template></xsl:stylesheet>

This will make a copy of every XML's non-root nodes and place them in a new one, who's name is defined by the name of the root element of the first XML. Change {name()} to some literal string to define the name of a new root element instead. This scenario anticipates that both XML's need to be as they are. Cut the "/*" out of the end of the document() line and you'll have the second XML merged completely with the first, along with it's root element. Cut the "*" out of the template match, and you'll have the first XML copied directly too.The other scenarios I've mentioned are more complicated and methods vary depending on the XML's complexity.

Link to comment
Share on other sites

  • 3 months later...

Hi, I have quite a similar problem right now and couldn't solv it only with the help of the topic so far. I have 2 xml files og unidentical level depthfirst:

<Level1>  <Level2>	<Level3>	</Level3>  </Level2></Level1>

second:

  <Level2>	<Level3>	  <Tag1>x</Tag1>	  <Tag2>x</Tag2>	  <Tag3>x</Tag3>	</Level3>  </Level2>

Now I want to keep the structure and content of the first xml file but replace level3 and add the tag contents from second xml. I don't get it. Tried this as last version:

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">	<xsl:template match="/">		<Level1>			<xsl:apply-templates/>		<Level2> <xsl:copy-of select="."/>		<Level3>		<xsl:copy-of select="document('second.xml')/Level1/Level2/Level3"/>		</Level3>		</Level2>		</Level1>		</xsl:template>	 <xsl:template match="/Level3">   </xsl:template></xsl:stylesheet>

I just get prosessed the first xml. What's wrong? Tipps appreciated!Loriot

Link to comment
Share on other sites

First stop, I don't think you need the

	<xsl:template match="/Level3">   </xsl:template>

And if the first XML is processed properly, this might mean your processor doesn't support the document() function. What is your processor anyway?

Link to comment
Share on other sites

Got a little further now. You were right, I didn't need the match/lavel3. So this is where I am now to get a proper basis:Stylesheet

<?xml version="1.0" encoding="UTF-16"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:apply-templates/> <xsl:copy-of select="."/> <xsl:copy-of select="document('second.xml')"/></xsl:template></xsl:stylesheet>

Result:

<Level1>  <Level2>	<Level3>	</Level3>  </Level2></Level1><!--up til here all from first xml--><!---from here added info from second xml-->  <Level2>	<Level3>	  <Tag1>x</Tag1>	  <Tag2>x</Tag2>	  <Tag3>x</Tag3>	</Level3>  </Level2>

I can filter the second.xml that I just get the Level3 and Tag info.But how do I now tell the stylesheet, that I want to replace the Level3 section with the content from second.xml?Any suggestions?RegardsLoriot

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...