easy(?) question about cross referencing in XSLT

J

Jeff Calico

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
 
S

Soren Kuula

Hi,
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??

Absolutely ;) Keys are the answer.

Soren
 
G

George Bina

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:eek:utput 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
 
J

Jeff Calico

George said:
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:eek:utput 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



Thanks, George! That example was very helpful!

--Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top