Re: [xsl] Newbie - trying to extract specific values from XML file.

Subject: Re: [xsl] Newbie - trying to extract specific values from XML file.
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Fri, 19 Mar 2004 16:59:29 +0000
Hi Julian,

> I have a long html file (see http://hq.tvw.net/template.html)
> that includes a mass of merge codes from an old program.

What I'd suggest is that you set up a key that retrieves the <Answer>
element with a particular name. The key would look like:

<xsl:key name="answers" match="Answer" use="@name" />

Then anywhere in the template where you have something like:

  &laquo;SETstd&raquo;

you can use, instead:

  <xsl:apply-templates select="key('answers', 'SETstd')" />

The call to the key() function retrieves the <Answer> element whose
name attribute has the value 'SETstd'. The <xsl:apply-templates>
instruction applies templates to it. You then need templates that look
like:

<xsl:template match="Answer[@name = 'SETstd']">
  SETstd is <xsl:value-of select="TFValue" />
</xsl:template>

to get the appropriate text for the given answer.

You can similarly use the key to retrieve the <Answer> elements where
they're tested in IF statements within the current template. For
example:

  &laquo;IF Setstd = FALSE&raquo;

can be replaced by:

  <xsl:if test="key('answers', 'SETstd')/TFValue = 'false'">

Note that you have to do a bit of work to identify what descendant of
the <Answer> element you need to look at in these tests, since it
depends on the structure of the XML that you're using (whether the
<Answer> element contains a <RptValue> element, a <TFValue> element,
a <TextValue> element or something else.
  
The key is a very efficient way to get at the <Answer> elements based
on their name. Given that you already have the HTML template written
in a 'pull' way, I think that this approach is going to be easier to
use than a more 'push' method of applying templates to all the
<Answer> elements and just having templates to match them.
  
Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread