Link to home
Start Free TrialLog in
Avatar of vyomu
vyomu

asked on

How to add new elements to XML from a input file using XSLT.

I am trying to address the following problem:
Using a input xml file, source.xml, which has several elements:
<stuff>
<videos>
</videos>
<books>
<book name="book1"/>
</books>
</stuff>

I am trying to take the book name attribute for each book defined, and then add it as a media to a target file donations.xml, which is formatted:
<donations>
<clothes>
</clothes>
<media>
<media name="media1" type="media1" reference="donation">
</media>
</donations>

So, using this input file source.xml, the output file donations.xml should now look like: Note that media type always matches media name, and reference is always donation for my purposes:
<donations>
<clothes>
</clothes>
<media>
<media name="media1" type="media1" reference="donation">
<media name="book1" type="book1" reference="donation">
</media>
</donations>

Any ideas how to do this via XSLT? I think I have to create a template based on the input source.xml using XSL, and then use another XSLT using this template and donations.xml to add the elements.. thoughts? I tagged ant to see if XSLT wasn't a good way to accomplish this..

A bit urgent so would appreciate any ideas!
Avatar of Logos077
Logos077
Flag of United States of America image

You could do this with XSLT but  Your original document may need to be restructured. You might also consider writing a schema for this document that will require the user to input the data correctly. W3schools has a lot of great information and  with tutorials and examples.

<stuff>
<videos>
</videos>
<book name="book1"/>
</books>
</stuff>


http://www.w3schools.com/xml/default.asp
Avatar of vyomu
vyomu

ASKER

But how can I reference another XML file as an input while doing my XSLT transformation?
Is using the document tag the right step?
This can be done quite simply in XSLT, either version 1.0 or version 2.0. You can use the document() function to get the extra document(s). In fact, the below XSLT can be called with any input document, it will not be processed. Instead, it will process the source.xml and the donations.xml.



<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
    <xsl:variable name="source" select="document('twodocs_source_Q24191177.xml')/*" />
    <xsl:variable name="donations" select="document('twodocs_donations_Q24191177.xml')/*" />
 
    <xsl:output indent="yes"/>
    
    <xsl:template match="/">
        <xsl:apply-templates select="$donations" mode="donations" />
    </xsl:template>
    
    <!-- at end of -->
    <xsl:template match="media/media[not(following-sibling::media)]" mode="donations">
        <!-- copy children -->
        <xsl:copy-of select="." />
        
        <!-- process books -->
        <xsl:apply-templates select="$source/books/book/@name" mode="books" />
    </xsl:template>
 
    <xsl:template match="* | @*" mode="donations">
        <xsl:copy>
            <xsl:apply-templates select="* | @*" mode="donations" />
        </xsl:copy>
    </xsl:template>
 
 
    <xsl:template match="@name" mode="books">
        <media name="{.}" type="{.}" reference="donation" />
    </xsl:template>
</xsl:stylesheet>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial