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!
XML

Avatar of undefined
Last Comment
abel

8/22/2022 - Mon
Logos077

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
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?
abel

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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
abel

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.