How-To: Use XSL to "search and replace" between two XML files?

L

Luke Dalessandro

I have an application where there is a primary XML data file. I'll use
the following as an example:

<data>
<item id="a">
<name>A</name>
<price>$10</price>
</item>
<item id="b">
<name>B</name>
<price>$5</price>
</item>
</data>

Simple enough so far. I have a layout XSL that takes a file conforming
to this schema and outputs it in html. This is simple too. The problem
that I am having is that there is a second XML file, in the same schema
as above, that contains data overrides for items. Basically, some users
see different data for an item. For example:

<data>
<item id="b">
<name>B</name>
<price>$25</price>
</item>
</data>

I am trying to build an XSL that will go through the primary data file
and replace any "items" that are overriden by the secondary file.
Furthermore, the path to the secondary file is passed in as a parameter.

This is what I have so far:

<xsl:stylesheet ...>

<!-- Secondary data file -->
<xsl:param name="mergeFile" />

<!-- Convert the merge file into a node list -->
<xsl:variable name="mergedItems" select="document($mergeFile)"/>

<!-- Pulls through all non-specified tags -->
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>

<!-- THIS IS THE SEARCH AND REPLACE TEMPLATE -->
<xsl:template match="item">
<xsl:variable name="localId" select="@id" />
<xsl:variable name="override"
select="$mergedItems//material[@id=$localId]" />

<xsl:choose>
<!-- If no override for this item, just copy it -->
<xsl:when test="count($override) = 0">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:when>
<!-- Otherwise, replace it with the data from $override -->
<xsl:eek:therwise>
<!--================================
Don't know what to put here...?
================================ -->
</xsl:eek:therwise>
</xsl:choose>

</xsl:template>

</xsl:stylesheet>

Basically, my probelm comes down to the fact that I'm not sure how to
copy the data from a node stored as a variable... if I try to call a
template using the node, it's just going to loop back into this same
template because the node has the same name. Then, it will keep finding
an overriding node, and looping.

As a side note, I should explain that the actaul application has much
more complicated data than above... each "replace" might replace a large
subtree of data. My platform is C#, ASP.NET 2.0 CTP release
(XslCompiledTransform), not that it matters really...

Sorry for the long post, and thanks in advance,
Luke
 
L

Luke Dalessandro

Luke said:
Basically, my probelm comes down to the fact that I'm not sure how to
copy the data from a node stored as a variable...

And voila, the <xsl:copy-of> tag... sigh*

Luke
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top