This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Referencing an element from another XML file


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@A.A</EMAIL>
> </Author>
> <Author>
> <NAME>B</NAME>
> <EMAIL>B@B.B</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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]