Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: does xpath expressions/matches support variables? >Thread Next - Re: does xpath expressions/matches support variables? Re: does xpath expressions/matches support variables?To: NULL Date: 6/4/2006 3:40:00 PM "stoj" <stoj@d...> wrote in message news:78FAEB31-EEC1-4AF8-BC8E-B2A272187E67@m...... > 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")? > No. The XPath function remains the same it's just an input that is changing. > 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 I'm going to guess that you have that all bearerRef elements refer to name elements that are inside an outer bearer tag and that you have similar references to name elements inside other outer tags. > 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 --> Ivan's post really put the finger the confusion This:- root/bearerTable/bearer/name/ethernet is a path to a node like this:- <root> <bearerTable> <bearer> <name> <ethernet> This doesn't really exist in your xml <!-- closing tags here. I suspect that given the reference 'bearer.ethernet' what you want to create a path that will select the bearer element that has the name 'ethernet' like this:- root/bearerTable/bearer[@name = 'ethernet'] The big question is the set of distinct xxxxxRef element names small and known or do you need to be able to add new xxxxRef elements to the XML without modifying the XSL? If the former is fairly straightforward using normal template techniques that latter will need a dynamic solution which can create anchors for any element with a tag name following the xxxxxRef pattern. An additional question is do you actually need to look up the references in order to create the anchor, i.e., is there some text in the referenced element other than the name that you need.? BTW with reference to the xxxxRef elements:- <bearerRef>bearer.ethernet</bearerRef> is not good XML design IMO. Better would be:- <bearerRef>ethernet</bearerRef> the 'bearer.' prefix is redundant data. A possible alternative would be:- <ref type="bearer">ethernet</ref> but having the element name specify the reference type is probably better design. Also you might find using xsl:key can provide a solution which isn't scanning the document frequently. > <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 > > "Anthony Jones" wrote: > > > > > "stoj" <stoj@d...> wrote in message > > news:AD9BDA51-058A-4006-8CC4-1369D77C1745@m...... > > > I need to use a variable (similar scenario also for a template parameter) > > > that has been assigned with an xpath string as an xpath expression/match > > for > > > xsl elements such as xsl:if, xsl:what, xsl:template, etc. > > > > > > Eg. xsl snippet - intended to invoke execute-code block if the node > > exists. > > > It works perfectly if i replace "$var" with "/root/parent/child". > > > > > > <xsl:variable name="var">/root/parent/child</xsl:variable> > > > <xsl:if test="$var"> > > > ...execute-code > > > </xsl:if> > > > > > > 1. Observed behaviour is that the if statement always fires. Presumably > > > because it is resolving to <xsl:if test="'/root/parent/child'">, which > > > resolves to true. If this is true, how can i change this to <xsl:if > > > test="/root/parent/child">? Is there a function availble to strip away > > the > > > extra quotations? > > > > > > 2. Does anyone know of a *clean* design pattern to solve this? Preferably > > > one that i can call as a generic template/function to allow it to called > > at > > > whim. > > > > > > cheers > > > stoj > > > > What you are trying to do is create Dynamic XSL that is XSL that changes as > > it's processed. This is not possible since the XSL is parsed and compiled > > in to a processing engine which can not be modified on the fly. > > > > We would need to know more about what you are doing to offer any serious > > help. > > > > Some things that might help are xsl:choose, extensive use of templates and > > the mode attribute. > > > > Anthony. > > > > > > | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
