Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: does xpath expressions/matches support variables?

From: Ivan Peters <ivan@--.----.--->
To: NULL
Date: 6/5/2006 12:44:00 AM

stoj wrote:
> I'm a little confused by your statement that XSL can't generate dynamic XSL.  
> Aren't the following (valid) alternative uses of a variable also examples of 
> dynamic behaviour, whereby the generated XSL/HTML code changes as it is 
> processed?;
> a) assigned element text value to the variable
> b) supply as an argument to an xpath function, eg. concat($var, ".ext")?
> 
> eg. snippet of relevant XML
> <root>
>   <bearerTable>
>     <bearer>
>       <name>ethernet</name>
>       ...other elements
>     </bearer>
>     <bearer>
>       <name>ppp</name>
>       ...other elements
>     </bearer>
>   </bearerTable>
> 
>   <networkTable>
>     <network>
>       <name>home</name>
>       <bearerRef>bearer.ppp</bearerRef>
>       ...other elements
>     </network>
>     <network>
>       <name>work</name>
>       <bearerRef>bearer.ethernet</bearerRef>
>       ...other elements
>     </network>
>   </networkTable>
> </root>      
> 
> What i'm trying to do is translate above xml into html as follows;
> a) marking up all <name> elements as anchors, eg. <a name="bearer.ethernet">
> b) marking up all <xxxxxRef> elements as anchor references, eg. <a 
> href="#bearer.ethernet">
> 
> Easy enough, except that before i create the anchor references, i want to 
> check if the specified xml bearer element actually exists first.  
> eg. xsl with bearer ref as current context
> <!-- using some string xpath fns; concat, substring-before, etc. to create a 
> string variable that holds a valid xpath like; 
> $ref=/root/bearerTable/bearer/name/ethernet -->
> <xsl:if test="$ref">
> ...create anchor reference
> </xsl:if>
> 
> One alternative i've found is to explicitly check each <name> value in a 
> xsl:for-each loop, but in my view this is very messy & slow(?).  Personally, 
> i'd prefer to let XSL do the hardwork and use it's XPath pattern matching... 
> but is this possible when the XPath argument is supplied as a variable?
> 
> cheers
> stoj

Something along these lines, perhaps:

<xsl:template match="network">
  <xsl:variable name="refName" select="substring-after(bearerRef,'.')"/>
  <xsl:if test="/root/bearerTable/bearer/name[. = $refName]">
   <a href="#bearer.{$refName}"><xsl:value-of select="name"/></a>
  </xsl:if>
</xsl:template>



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent