Jump to content

loading XML into another XML file


xander85

Recommended Posts

hey all, picking ya brains again...is it possible to load one xml doc into another, say to split files or apply different style sheets to several files from the same datawhat im using atm is

<?xml version="1.0" encoding="utf-8" ?><?xml-stylesheet type="text/xsl" href="library-album.xsl"?><library>  <!-- Table music -->    <music>        <artist># soundtracks</artist>        <album>2 fast 2 furious</album>        <source>rns</source>        <kind>mp3 audio</kind>        <Added>2007-08-01</Added>    </music></library>

what i would like to do is have the <library>....</library> in a seperate file. this will enable me to just update/change one xml doc instead of multiplesmaybe look something like this

<?xml version="1.0" encoding="utf-8" ?><?xml-stylesheet type="text/xsl" href="library-album.xsl"?>LOAD EXT XML FILE HERE (library.xml)

i have a sneaking suspition i need to do it using Schema??

Link to comment
Share on other sites

In the example you give, it would be pretty pointless to separate the XML, but I see your point.You can do it with XInclude. The sample XML you gave (the one that would include library.xml I mean) would look like:

<?xml version='1.0'?><?xml-stylesheet type="text/xsl" href="library-album.xsl"?><include href="library.xml" xmlns="http://www.w3.org/2001/XInclude" />

but as I said, it's pretty useless doing that. You should instead do something like:

<?xml version='1.0'?><?xml-stylesheet type="text/xsl" href="library-album.xsl"?><library xmlns:xi="http://www.w3.org/2001/XInclude"><xi:include href="2 fast 2 furious.xml" /></library>

if we suppose "2 fast 2 furious.xml" contains the <music/> element and it's contents, the result will be your first XML.In order to perform the actual XInclude, you need an XInclude processor. PHP5's DOMDocument class supports XInclude with the xinclude() method. Other S3Ls use other processors and APIs I'm not aware of. PHP4 doesn't support XInclude as far as I know. And browsers don't either.

Link to comment
Share on other sites

ok so according to that it should look like this correctlibrary-album.xml

<?xml version='1.0'?><?xml-stylesheet type="text/xsl" href="library-album.xsl"?><library xmlns:xi="http://www.w3.org/2001/XInclude"><xi:include href="library.xml"/></library>

library.xml

<?xml version="1.0" encoding="utf-8"?><library>  <!-- Table music -->    <music>        <artist># soundtracks</artist>        <album>2 fast 2 furious</album>        <source>rns</source>        <kind>mp3 audio</kind>        <Added>2007-08-01</Added>    </music>    <music>        <artist># soundtracks</artist>        <album>28 days later</album>        <source>pyt</source>        <kind>mp3 audio</kind>        <Added>2007-08-01</Added>    </music>... and so on</library>

because that is what i made but it wont work :) is it because the host im using doesn't have a Xinclude processor?

Link to comment
Share on other sites

because that is what i made but it wont work :) is it because the host im using doesn't have a Xinclude processor?
Yes. And even if it did, you actually need to do that within PHP itself. It isn't performed automatically. Something like this in PHP5 (again, your host MUST have PHP5 for this):
<?php//View the result as an XML fileheader('Content-type: application/xml');//Initiate the DOMDocument class$dom = new DOMDocument;//Load library-album.xml$dom->load('library-album.xml');//Perform XIncludes$dom->xinclude();//View the result$dom->saveXML();?>

Note that with those files, your output will be:

<?xml version='1.0'?><?xml-stylesheet type="text/xsl" href="library-album.xsl"?><library><library>  <!-- Table music -->	<music>		<artist># soundtracks</artist>		<album>2 fast 2 furious</album>		<source>rns</source>		<kind>mp3 audio</kind>		<Added>2007-08-01</Added>	</music>	<music>		<artist># soundtracks</artist>		<album>28 days later</album>		<source>pyt</source>		<kind>mp3 audio</kind>		<Added>2007-08-01</Added>	</music>... and so on</library></library>

I just wondered if you're sure you want nested <library/> elements.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...