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

Discussion in 'XML' started by Luke Dalessandro, Dec 23, 2004.

  1. 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
    Luke Dalessandro, Dec 23, 2004
    #1
    1. Advertising

  2. 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
    Luke Dalessandro, Dec 23, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank
    Replies:
    1
    Views:
    734
    Dimitre Novatchev
    Sep 12, 2003
  2. Yasaswi Pulavarti

    two xml and one xsl files

    Yasaswi Pulavarti, Sep 17, 2004, in forum: XML
    Replies:
    1
    Views:
    392
    Kenneth Stephen
    Sep 17, 2004
  3. Rolf Kemper
    Replies:
    0
    Views:
    394
    Rolf Kemper
    Dec 23, 2004
  4. Replies:
    1
    Views:
    3,573
    A. Bolmarcich
    May 27, 2005
  5. GenxLogic
    Replies:
    3
    Views:
    1,227
    andrewmcdonagh
    Dec 6, 2006
Loading...

Share This Page