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]

Re: xsl:key question


Hi Kerin,

>hi everyone,
>
>I'm something of an XSL newbie, and I can't seem to find the answer to this
>problem - I'm sure it's very simple, but I just can't work it out.
>
>With this XML:
>
>...
>				<Company CDLID="18767">
>					<CompanyCitation>
>						<CompanyName>
>							Johnson and Johnson
>						</CompanyName>
>					</CompanyCitation>
>				</Company>
>...
>
>I need to have an xsl:key which will return the text within <CompanyName>
>when a CLID="" value of the <Company> element is passed to it (The value of
>the CDLID attribute is taken from the <CompanyRef> element).
>
>How do I do this? All of the xsl:key examples I've come across only concern
>retrieving values from the same element that is queried. I'm sure this is a
>simple XPath expression, but, as I say, I just can't seem to work it out
>today.
>
First, the match needs to identify the nodes you are looking for.
Then the 'use' has to find the key value with the selected node as the
context.  So you *could* use something like

<xsl:key name="CoName"
    select="Company//CompanyName/text()"
    use="ancestor::Company/@CDLID"
/>

Notice I am using XPaths which are a bit more general than your XML
demands - this is usually good practice as it means less to change
when someone tinkers with the XML structure.  It may slow things down
a bit depending on how complicated the XML really is and how clever
your XSLT processor.

BUT...

If I were you I would simply say

<xsl:key name="Company" select="Company" use="@CDLID"/>

and then

<xsl:value-of select="key('Company', @CDLID)//CompanyName" />

This is easier to read and more useful if in future you
a) need something other than the company name
b) find you want to put more structure into the company name, rather
than just simple text

Regards,
Trevor Nash
--
Traditional training & distance learning,
Consultancy by email

Melvaig Software Engineering Limited
voice:     +44 (0) 1445 771 271 
email:     tcn@melvaig.co.uk

 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]