Connecting Tech Pros Worldwide Forums | Help | Site Map

easy(?) question about cross referencing in XSLT

Jeff Calico
Guest
 
Posts: n/a
#1: Jan 20 '06
Hi

I have what I think is a easy question about how to pull cross
reference data
when I transform my XML file. I can't seem to find a good example of
how to do
this on the web, though.

Here is the XML file I am supposed to process:

<property address="1235 Wildwood">
<ownerRef ownerKey="100"/>
<ownerRef ownerKey="130"/>
</property>

<owner ownerId="100" name="J. Smith"/>
<owner ownerId="130" name="E. Rock"/>


When I process the property element, I want to look up the owner names
and print
them also. I'm guessing something with xsl:key maybe??

thanks,
Jeff


Soren Kuula
Guest
 
Posts: n/a
#2: Jan 21 '06

re: easy(?) question about cross referencing in XSLT


Hi,[color=blue]
>
> When I process the property element, I want to look up the owner names
> and print
> them also. I'm guessing something with xsl:key maybe??[/color]

Absolutely ;) Keys are the answer.

Soren
George Bina
Guest
 
Posts: n/a
#3: Jan 23 '06

re: easy(?) question about cross referencing in XSLT


Hi Jeff,

Here it is a sample:

<test>
<property address="1235 Wildwood">
<ownerRef ownerKey="100"/>
<ownerRef ownerKey="130"/>
</property>
<owner ownerId="100" name="J. Smith"/>
<owner ownerId="130" name="E. Rock"/>
</test>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="ownerById" match="owner" use="@ownerId"/>
<xsl:template match="property">
<xsl:for-each select="ownerRef">
<xsl:value-of select="key('ownerById', @ownerKey)/@name"/>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text></xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

gives

J. Smith, E. Rock

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Jeff Calico
Guest
 
Posts: n/a
#4: Jan 23 '06

re: easy(?) question about cross referencing in XSLT



George Bina wrote:[color=blue]
> Hi Jeff,
>
> Here it is a sample:
>
> <test>
> <property address="1235 Wildwood">
> <ownerRef ownerKey="100"/>
> <ownerRef ownerKey="130"/>
> </property>
> <owner ownerId="100" name="J. Smith"/>
> <owner ownerId="130" name="E. Rock"/>
> </test>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> <xsl:output method="text"/>
> <xsl:key name="ownerById" match="owner" use="@ownerId"/>
> <xsl:template match="property">
> <xsl:for-each select="ownerRef">
> <xsl:value-of select="key('ownerById', @ownerKey)/@name"/>
> <xsl:if test="position()!=last()">
> <xsl:text>, </xsl:text></xsl:if>
> </xsl:for-each>
> </xsl:template>
> </xsl:stylesheet>
>
> gives
>
> J. Smith, E. Rock
>
> Best Regards,
> George
> ---------------------------------------------------------------------
> George Cristian Bina
> <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
> http://www.oxygenxml.com[/color]



Thanks, George! That example was very helpful!

--Jeff

Closed Thread