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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Luke Dalessandro

    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:styleshe et ...>

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

    <!-- Convert the merge file into a node list -->
    <xsl:variable name="mergedIte ms" select="documen t($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="$merged Items//material[@id=$localId]" />

    <xsl:choose>
    <!-- If no override for this item, just copy it -->
    <xsl:when test="count($ov erride) = 0">
    <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:apply-templates />
    </xsl:copy>
    </xsl:when>
    <!-- Otherwise, replace it with the data from $override -->
    <xsl:otherwis e>
    <!--=============== =============== ==
    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
    (XslCompiledTra nsform), not that it matters really...

    Sorry for the long post, and thanks in advance,
    Luke
  • Luke Dalessandro

    #2
    Re: How-To: Use XSL to &quot;search and replace&quot; between two XML files?

    Luke Dalessandro wrote:[color=blue]
    > 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...[/color]

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

    Luke

    Comment

    Working...