xsl-list
[Top] [All Lists]

Re: [xsl] Fetching the value of a dynamically assigned attribute

2017-07-19 04:29:18

On 19 Jul 2017, at 00:52, Joseph L. Casale 
jcasale(_at_)activenetwerx(_dot_)com 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

While mocking up some examples related to a previous question,
I have an example where I add a namespace to my stylesheet and
add some data to it:

 <myns:data>
   <foo>
     <bar>one</bar>
     <bar>two</bar>
     <bar>three</bar>
   </foo>
 </myns:data>

I also load another XML doc I am using data from:

 <xsl:variable name="documentAppConfig" 
select="document('some_binary.exe.config')" />

While looping over the elements from the data I added to my stylesheet, I
am trying to select the value of an attribute where the attribute name is
dynamically specified:

   <xsl:for-each select="document('')/xsl:stylesheet/myns:data/foo/bar">

       <!-- this works: -->
       <xsl:value-of select="concat('some-text', text())" />

Better is <xsl:value-of select="concat('some-text', .)" /> - because the first 
one will fail if there are multiple text nodes separated by comments. Using "." 
here is equivalent to string(.) which gives you the string-value of the element.


       <!-- this does not work: -->
       <xsl:value-of 
select="$documentAppConfig/configuration/myNode/@*[local-name()=text()]" />

That's because within the predicate the context item is an attribute node, and 
attribute nodes don't have text node children. But you couldn't use "." here 
either, because the context is wrong. You could use "current()" however.

       <!-- this works: -->
       <xsl:variable name="text" select="text()" />
       <xsl:value-of 
select="$documentAppConfig/configuration/myNode/@*[local-name()=$text]" />

   </xsl:for-each>

That result is not surprising, but what is the syntactically correct
way of accomplishing this, or is the intermediate assignment the only
way?

These are all syntactically correct, but the one that doesn't work is 
semantically incorrect, because you didn't take context into account.

Michael Kay
Saxonica
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>