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 15th, 2007, 06:13 AM
Registered User
 
Join Date: May 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Appending set of elements at the end

hi,

I have two RDF files (say file1 and file2). I want to insert set of elements in file2 into file1. The requirement is to append a set of elements that I want to insert into file2 after the last element. Given below is a sample of two files. If I explain using them what I want is to insert the set of elements, <Conversiontypes rdf:ID="x"> in file2 after the set of <Transaction rdf:ID="xx"></Transaction> elements in file1. How can I do this?

Thanks,

/Sesath


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>
<Transaction rdf:ID="vi345+vi945">
    <Exchanges rdf:ID="ve1075">
           <hasEventTypes>
            <TransferType rdf:ID="vp945">
            <isEventType rdf:datatype="http://www.w3.org/2001/XMLSchema#string">decrement</isEventType>
        </TransferType>
    </hasEventTypes>
    <hasEventTypes>
        <TransferType rdf:ID="vp295">
            <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#">
<ConversionType rdf:ID="TransmittingMusicStream">
  <hasTransferedOrConversedResourceType rdf:resource="#MusicStream"/>
  <isEventType rdf:datatype="http://www.w3.org/2001/XMLSchema#string">decrement</isEventType>
  <hasConversedFeature rdf:resource="OnAir"/>
  <hasActorType rdf:resource="#RightUsers"/>
</ConversionType>
<ConversionType rdf:ID="GettingListenerAttention">
  <hasTransferedOrConversedResourceType rdf:resource="#Attention"/>
  <isEventType rdf:datatype="http://www.w3.org/2001/XMLSchema#string">increment</isEventType>
  <hasConversedFeature rdf:resource="BeingPopularRadioStation"/>
  <hasActorType rdf:resource="#RightUsers"/>
</ConversionType>
</rdf:RDF>

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


With file1.xml as the principal input, do

<xsl:template match="/*">
<xsl:copy>
  <xsl:copy-of select="*"/>
  <xsl:copy-of select="document('file2.xml')/*/*"/>
</xsl:copy>
</xsl:template>

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

Sorry, I should have told you at the first place that the file2 has elements other than <ConversionType> (lets say some other elements such as <TransferType/>,<Transformation/, etc.> ) and I at the same time copy the contents of those elements to the relevant positions in file1. My xslt (actually it is based on the solution you gave me to my previous problem) is below.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" version="1.0">
    <xsl:output method="xml"/>

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







    <xsl:variable name="file2" select="document('E:\Research\PhL Work\Xquery\Mapping\Finals\CompletedMappings\Revis edMappings\Finals_tobe_in_Thesis\Output_OWL_Files\ SecondMapping\TransferTypeDetails2.rdf')"/>



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

    <xsl:template match="Exchange//TransferType" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                                        xmlns:owl="http://www.w3.org/2002/07/owl#"
                                        xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">

    <xsl:param name="e_resource_id" select="current()/@rdf:resource"/>
    <xsl:param name="e_transfer_id" select="substring-after($e_resource_id,'#')"/>
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:copy-of select="$file2//TransferType[@rdf:ID = $e_transfer_id]/*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Interface//TransferType" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                                        xmlns:owl="http://www.w3.org/2002/07/owl#"
                                        xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">

    <xsl:param name="i_resource_id" select="current()/@rdf:resource"/>
    <xsl:param name="i_transfer_id" select="substring-after($i_resource_id,'#')"/>
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:copy-of select="$file2//TransferType[@rdf:ID = $i_transfer_id]/*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>


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

</xsl:stylesheet>

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

In that case, it surely just needs one more template rule:

<xsl:template match="rdf:RDF">
  <xsl:apply-templates/>
  <xsl:copy-of select="document('file2.xml')/*/ConversionType"/>
</xsl:template>

Given the code you've already got, I can't quite see why this causes you problems.

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

Thanks. It works now and I had problems of copying everything under the element <ConversionType> and also pasting it into the proper place (it pasted the child nodes of <ConversionType> at the top of the document). That is first I have put <xsl:apply-templates/> in the wrong place and also selected child elements of the element that I wanted to insert. Thanks once again for the answer.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Appending set of elements after the similar nodes sesath XSLT 2 May 15th, 2007 10:16 AM
Selecting unique set of elements Chamkaur XSLT 1 March 15th, 2007 05:43 AM
Oracle Back End - MS Access Front End - Multi User ckaliveas Oracle 1 February 1st, 2007 06:00 AM
How to create end tag on empty elements rjonk XSLT 1 July 21st, 2006 01:18 PM





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