[Home] [By Thread] [By Date] [Recent Entries]
Greetings,
I am having a difficulty creating a XPath element. Tiny Example XML document: <?xml version="1.0" encoding="UTF-8"?> <catalog name="MasterCatalog"> <service name="WCS" base="/opendap/wcs_service/"/> <dataset name="Foo" urlPath="data/foo" >
<serviceName>WCS</serviceName>
</dataset></catalog> This works: <xsl:value-of select="//service[@name='WCS']/@base" /><br /> It returns the value of the attribute "base" for the first "service" element in the document whose "name" attribute evaluates to "WCS" However, in my problem when processing a "dataset" element there is a child element that contains the name of the service: I need to get the "base" attribute for the "service" that matches the text value of the "serviceName" element. I thought that I should be able to do it by calling for the value-of the serviceName element inside the XPath for the select:: <xsl:template match="dataset"> <xsl:value-of select="(//service/[@name='{serviceName}']/ @base" /> </xsl:template-match> But that doesn't work. Question 1): Should that work? Is it just an XPath syntax error on my part, or must the value of the equality test [@name='stuff'] be hard wired? Question 2): Is there another way to get to the same place? I can kludge it using a named template, but I think it's (a) fragile and (b) ugly. Thanks in Advance! Nathan Potter Complete Example: Example source XML: <?xml version="1.0" encoding="UTF-8"?>
<catalog name="MasterCatalog">
<service name="OPeNDAP-Hyrax" base="/opendap/"/>
<service name="WCS" base="/opendap/wcs_service/"/>
<dataset name="Surface Air Temperature"
urlPath="data/TSurfAir" >
<serviceName>OPeNDAP-Hyrax</serviceName>
</dataset>
<dataset name="Foo" urlPath="data/foo" >
<serviceName>WCS</serviceName>
</dataset>
<dataset name="SeaSurfaceTemperature"
urlPath="data/sst">
<serviceName>WCS</serviceName>
</dataset>
</catalog>Example XSLT: <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE xsl:stylesheet []> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>
<xsl:template match="dataset"> <xsl:if test="not(dataset)"><!-- Otherwise, create the access link. --> <!-- This Works, but it's hardwired for the same result every time: --> service: <xsl:value-of select="//service[@name='WCS']/ @base" /><br/> <!-- This does not. Is there a way to get there? --> service: <xsl:value-of select="//service [@name='{serviceName}']/base" /><br/> <!-- And this is a nasty workaround --> <xsl:call-template name="accessURL"> <xsl:with-param name="serviceName"><xsl:value-of select="serviceName" /></xsl:with-param> <xsl:with-param name="urlPath"><xsl:value-of select="@urlPath" /></xsl:with-param> <xsl:with-param name="name"><xsl:value-of select="@name" /></xsl:with-param> <xsl:with-param name="suffix">html</xsl:with-param> </xsl:call-template><br/> </xsl:if> </xsl:template> <xsl:template name="accessURL"> <xsl:param name="serviceName" /> <xsl:param name="urlPath" /> <xsl:param name="name" /> <xsl:param name="suffix" /> <xsl:for-each select="//service"> <xsl:if test="@name = $serviceName"> <a href="{@base}{$urlPath}.{$suffix}" > <xsl:value- of select="$name" /></a> </xsl:if> </xsl:for-each> </xsl:template>
</xsl:stylesheet> ============================================================ Nathan Potter Oregon State University, COAS
|

Cart



