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 May 10th, 2007, 03:56 AM
Registered User
 
Join Date: May 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Appending child nodes to a RDF/OWL file using xslt

I have an RDF/OWL file where I want to add some new child nodes. These new nodes are in a seperate file with the similar structure but only with new nodes that are to be added to the first RDF/OWL file. I tried to do that using XSLT but it has some problems finding the desiried place to insert the new element (I beleive) caused by the rdf:RDF wrapping. Anyway a simple format of my files are below. I also tried to do the same with XQuery but it, atleast for me, is difficult. Can anybody suggest a way to do that. I have following requirements: first the it should find the matching node (for example <TransferType rdf:ID="xx"> in the file1) and the insert the new nodes in, say,under the node matching node <TransferType rdf:ID="xx"> in the file 2 under it. Can anybody help me?


file1:

<rdf:RDF --goes here>
<Transaction rdf:ID="vi34+vi94">
    <Exchanges rdf:ID="ve107">
           <hasEventTypes>
            <TransferType rdf:ID="vp94">
            <isEventType rdf:datatype="http://www.w3.org/2001/XMLSchema#string">decrement</isEventType>
        </TransferType>
    </hasEventTypes>
    <hasEventTypes>
        <TransferType rdf:ID="vp29">
            <ActorType rdf:resource="rightUsers"/>
            <isEventType rdf:datatype="http://www.w3.org/2001/XMLSchema#string">increment</isEventType>
            </TransferType>
        </hasEventTypes>
    </Exchanges>
</Transaction>
</rdf:RDF>

file2:

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<Transaction rdf:ID="vi34+vi94">
    <Exchanges rdf:ID="ve107">
           <hasEventTypes>
            <TransferType rdf:ID="vp94">
                     <actorType rdf:ID="rightSociety"/>
                     <hasTransferedRight rdf:ID="Ownership"/>
                     </TransferType>
    </Exchanges>
</Transaction>
</rdf:RDF>

 
Old May 10th, 2007, 04:12 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

First define your identity template which copies everything unchanged:

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

Declare a global variable holding your second file:

<xsl:variable name="file2" select="document('file2.xml')"/>

Then add a template rule that modifies the element you want to change:

<xsl:template match="TransferType"
              xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:copy-of select="$file2//TransferType[@rdf:ID = current()/@rdf:ID]/*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>


This kind of thing is much harder in XQuery, which doesn't support this "copy everything exactly except X" coding pattern. To do the same thing in XQuery, you'll have to wait for XQuery updates to be supported.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 10th, 2007, 04:37 AM
Registered User
 
Join Date: May 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael, It works nicely. I have done somethings wrong in my xslt preogram and you have explained it so nicely. Also thanks for your explanation of my XQuery problem as well.






Similar Threads
Thread Thread Starter Forum Replies Last Post
not able to print out the child nodes eruditionist XSLT 7 October 30th, 2008 10:33 AM
Appending set of elements after the similar nodes sesath XSLT 2 May 15th, 2007 10:16 AM
XSLT for Variable Child Nodes jlagedo XSLT 2 September 10th, 2006 03:00 AM
RDF, OWL and XSL-FO NEO1976 XSLT 6 August 23rd, 2006 04:23 AM
Appending new nodes inside tables in IE 6 stephen_c_ Javascript How-To 1 January 26th, 2005 05:06 AM





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