[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Referencing an element from another XML file

Subject: Re: Referencing an element from another XML file
From: "Joerg Heinicke" <joerg.heinicke@xxxxxx>
Date: Wed, 27 Mar 2002 09:11:00 +0100
author.xml
Of course ...

tutorial.xml (which is transformed)

<tutorial>
    <author idref="1"/>
</tutorial>

author.xml

<authors>
    <author id="1">
        <name>foo</name>
    </author>
    <author id="2">
        <name>bar</name>
    </author>
</authors>

XSL

<xsl:template match="tutorial">
    <xsl:copy>
        <xsl:apply-templates select="author"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="author">
    <xsl:copy>
        <xsl:copy-of select="document('author.xml')/authors/author[@id =
current()/@idref]/name"/>
    </xsl:copy>
</xsl:template>

output should be

<tutorial>
    <author>
        <name>foo</author>
    </author>
</tutorial>

Of course there are some simplifications:

1. You could store the author.xml in a variable, if you have to use it
often:

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

Later:

<xsl:copy-of select="$authors/author[@id = current()/@idref]/name"/>

2. You can create a key for the authors:

<xsl:key name="authors" match="author" use="@id"/>

later:

<xsl:copy-of select="key('authors', @idref)"/>

3. If you have a DTD in author.xml setting @id to type ID, you can use the
simple id(). Then you only have to switch to the other file.

<xsl:for-each select="$author">
    <xsl:copy-of select="id(@idref)"/>
</xsl:for-each>

Hope this helps,

Joerg

> G'Day People !
>
> My Current Structure (Tutorial.xml)
> ===================================
> <Tutorial>
> <LotsOfOtherElements>
> </LotsOfOtherElements>
>
> <AuthorList>
> <Author>
> <NAME>A</NAME>
> <EMAIL>A@xxx</EMAIL>
> </Author>
> <Author>
> <NAME>B</NAME>
> <EMAIL>B@xxx</EMAIL>
> </Author>
> </AuthorList>
> </Tutorial>
> ===================================
>
> What I wish to do:
>
> I want to have two files:
>
> Tutorial.xml, and
> Author.xml
>
> Then, I want to be able to reference an author existing in author.xml
> >from tutorial.xml.
>
> Is it possible to do something like this ?
>
>
> TIA,
>
> Kunal


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.