Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 8th, 2005, 01:42 PM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to remote
Default Merging 2 documents, joining on a unique ID

My goal is to merge two xml documents so that a specific node and its children are replaced with the contents of another in the second xml document. *It seems to be working with one problem(see bottom). It's hard for me to explain cause I'm newby:)

Here's the code:



<users>
    <user uid="1">
        <fname>John</fname>
        <lname>Smith</lname>
        <phone>1111111111</phone>
    </user>
    <user uid="2">
        <fname>Dave</fname>
        <lname>Black</lname>
        <phone>2222222222</phone>
    </user>
</users>




<users>
    <user uid="2">
        <fname>Sam</fname>
        <lname>Gold</lname>
        <phone>3333333333</phone>
    </user>
</users>



<users>
    <user uid="1">
        <fname>John</fname>
        <lname>Smith</lname>
        <phone>1111111111</phone>
    </user>
    <user uid="2">
        <fname>Sam</fname>
        <lname>Gold</lname>
        <phone>3333333333</phone>
    </user>
</users>



<!--
The problem is with the John Smith record(the one that doesn't get the update...the <user uid="1"> is missing. Do I have to manually write this out in my <xsl:otherwise> element...maybe because I'm inside the user node already?-->
-->

<users>
    <notupdated>

    <fname>John</fname>

      <lname>Smith</lname>

      <phone>1111111111</phone>

   </notupdated>

   <updated><user uid="2">
        <fname>Sam</fname>

         <lname>Gold</lname>

         <phone>3333333333</phone>

      </user>
   </updated>

</users>


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="doc-file">documentupdate.xml</xsl:variable>


<xsl:template match="* | @*">
   <xsl:copy>
      <xsl:copy-of select="@*"/>
         <xsl:apply-templates/>
      </xsl:copy>
</xsl:template>

<xsl:template match="users/user">
   <xsl:param name="userID">
      <xsl:value-of select="@uid"/>
   </xsl:param>
   <xsl:choose>
      <xsl:when test="document($doc-file)//users/user[@uid=$userID]">
         <updated><xsl:copy-of select="document($doc-file)//users/user"/></updated>
      </xsl:when>
      <xsl:otherwise>
          <notupdated>
         <xsl:apply-templates/>
         </notupdated>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>
</xsl:stylesheet>

 
Old March 8th, 2005, 03:23 PM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to remote
Default

Okay, looks like I can just add the following to <xsl:otherwise>

<xsl:otherwise>
  <notupdated>
  <user uid="{@uid}">
  <xsl:apply-templates/>
  </user>
  </notupdated>
</xsl:otherwise>








Similar Threads
Thread Thread Starter Forum Replies Last Post
generate unique id capri SQL Server 2000 1 April 6th, 2008 10:18 AM
Merging two XML documents with XSL DWalker XSLT 1 February 3rd, 2006 06:58 PM
Create Unique Id langer123 Classic ASP Basics 0 April 6th, 2005 01:27 PM
Getting Unique ID from Database Nicky_uk Classic ASP Databases 9 January 26th, 2005 04:45 PM
merging 2 xml documents espider4u XSLT 0 August 31st, 2004 11:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.