Hi.
I'm rather new to XSL and am having some difficulty understanding how to use
the xsl:key function. I'm sorry if this is a FAQ, but I've tried searching
the archives and couldn't find the answer to my problem.
CONTEXT: I'm trying to split up a large XML file (bigFile.xml) into multiple
smaller files. The file will be splitting up at the "<association>" tags
(see below xml example).
PROBLEM: I have to generate the filename using the value of one of the
elements (countyName tag, e.g. Alameda) in the <association> branch of the
tree. I need to reference those county names to lookup and return a
corresponding county number: e.g. Alameda would return number 1, Alpine
would return number 2, etc... I can't simply use the position() function,
since data from some counties may be missing.
I initially tried to use a bunch of xsl:choose & xsl:when statements (ugly
and inelegant as it is), but that wasn't working very well, so I thought I'd
try something else. I've tried to incorporate the xsl:key statement (see
XSLT fragment below) into this but it doesn't seem to be working.
Can anyone out there offer any advice / suggestions on using the xsl:key
statement (or another approach) to accomplish my objective?
Any advice would be MUCH appreciated.
TIA,
Helen Kim
________________________________________________
input XML fragment, bigFile.xml:
.....
<title class="SystemTitle" toc-level="1">TableData</title>
<association>
<countyName>Alameda</countyName>
<output-object type="table" class="Table">
<!--elided table data-->
</output-object>
</association>
<association>
<countyName>Alpine</countyName>
<output-object type="table" class="Table">
<!--elided table data-->
</output-object>
</association>
<association>
<countyName>Amador</countyName>
<output-object type="table" class="Table">
<!--elided table data-->
</output-object>
</association>
<!--(etc... for all 58 counties)-->
.....
_____________________________________________
XSLT fragment:
!--Set up a variable to hold contents of the counties.xml lookup -->
<xsl:variable name="countyLookup-top"
select="document('counties.xml')/counties"/>
<!--define key-->
<xsl:key name="countyLookup" match="name" use="num" />
<xsl:template match="/">
<xsl:apply-templates select="key('countyLookup', 'Alpine')" />
</xsl:template>
<xsl:template match="name">
<xsl:variable name="countyNo">
<xsl:value-of select="." />
</xsl:variable>
</xsl:template>
<xsl:template match="association">
<xsl:variable name="filenameFrag" select="//proc/title" />
<xsl:variable name="file" select="concat($filenameFrag, '_', $countyNo,
'.xml')" />
<!--creating new file for each association element processed-->
<xsl:document href="e:\data\{$file}">
<xsl:copy>
<xsl:element name="association">
<xsl:attribute name="fileFragment" >
<xsl:value-of select="//proc/title" />
</xsl:attribute>
<xsl:attribute name="countyNum" >
<xsl:value-of select="$countyNo" />
</xsl:attribute>
</xsl:element>
<!------etc...-->
___________________________________________________________________________
XML "lookup", counties.xml:
<counties>
<county><name>Alameda</name><num>1</num></county>
<county><name>Alpine</name><num>2</num></county>
<county><name>Amador</name><num>3</num></county>
<county><name>Butte</name><num>4</num></county>
<county><name>Calaveras</name><num>5</num></county>
<!------etc...-->
</counties>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list