473,320 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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:otherwise>
<!--================================
Don't know what to put here...?
================================ -->
</xsl:otherwise>
</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
Jul 20 '05 #1
1 2863
Luke Dalessandro wrote:
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
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jeremylim2000 | last post by:
sorry, maybe the way I wrote a little bit confussing. It question how can encript the asp files. It can run on server but the source code is encript. Jeff Cochran wrote: > *On Mon, 6 Sep 2004...
6
by: Sylvain Thenault | last post by:
Hi there ! I've some questions regarding pth files (which btw are undocumented in the python reference, is this intentional ?) I thought that I could use a .pth file to be able to import zope...
4
by: ScoobyDoo | last post by:
Anyone know how I can create and edit batch files from MS Access 2002?
6
by: j0mbolar | last post by:
out of curiosity, what conventions do you guys use for header files? include guards type definitions prototypes #defines
3
by: juli jul | last post by:
Hello, How exactly can I open a directory and search in all text files in it ,for a specific string? Thank you very much! *** Sent via Developersdex http://www.developersdex.com *** Don't...
2
by: Philip Wagenaar | last post by:
How do I search for a directory on a windows xp machine and delete the directory including all subfolders and files in them?
3
by: puja patel | last post by:
hi all, could anyone please help in priting aspx page to .pdf format ? I have on form which is designed in asp.net 2.0 using c#. Now after user has filled in the form and click on submit, I want...
2
by: John | last post by:
Hi How can I do this programmatically; 1. Read the first file in a folder. 2. Search for its path+name in a field in a table. 3. If not found, delete the file in the folder.
9
by: outstretchedarm | last post by:
How exactly does HTML/Javascript handle playing midi files? Does it have a player imbedded in it? Or does it borrow from the computer's midi player? How could you make a webpage play certain...
1
by: Roshawn | last post by:
Hi, I'm an affiliate of several online merchants. These merchants all offer data feeds to their affiliates and I'm desirous to work with them. However, these feeds are gzipped (.gz file...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.