xslt - How to get all preceding nodes regardless of the current position -


structure of xml

<root>     <q id="a">1</q>     <q id="b">2</q>     <q id="c">3</q>       <x>            <q id="d">3.1</q>            <q id="e">3.2</q>           <q id="f">3.3</q>       </x>     <q id="g">4</q>     <q id="h">5</q> </root> 

this xpath expression : //q[@id=$currentqid]/preceding::q

i'm trying list of preceding q nodes regardless of current position (i'm passing current position parameter xslt) when on a,b,c,g,h nodes returns me correct list of preceding nodes. bu when on d,e,f not working right.

example: when current node g returns, a,b,c,d,e,f when current node f returns d,e,f although expect a,b,c,d,e

if 1 can find solution good.

thank you.

use:

($yournode/preceding::q | $yournode/ancestor::q)/@id  

xslt - based verification:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">  <xsl:output omit-xml-declaration="yes" indent="yes"/>   <xsl:variable name="vqg" select="(//q[@id='g'])[1]"/>  <xsl:variable name="vqf" select="(//q[@id='f'])[1]"/>   <xsl:template match="/">   "preceding" $vqg:    <xsl:for-each select=   "($vqg/preceding::q | $vqg/ancestor::q)/@id">    <xsl:value-of select="concat(., ' ')"/>   </xsl:for-each> =====================    "preceding" $vqf:    <xsl:for-each select=   "($vqf/preceding::q | $vqf/ancestor::q)/@id">    <xsl:value-of select="concat(., ' ')"/>   </xsl:for-each>  </xsl:template> </xsl:stylesheet> 

when transformation applied on provided xml document:

<root>     <q id="a">1</q>     <q id="b">2</q>     <q id="c">3</q>       <x>           <q id="d">3.1</q>           <q id="e">3.2</q>           <q id="f">3.3</q>       </x>     <q id="g">4</q>     <q id="h">5</q> </root> 

the xpath expression evaluated twice (for 2 q elements question) , string values of results output separated space:

  "preceding" $vqg:    b c d e f  =====================    "preceding" $vqf:    b c d e  

explanation:

the preceding:: axis doesn't select preceding nodes in document order -- 1 needs use ancestor:: axis.

from w3c xpath 1.0 specification:

"• the preceding axis contains nodes in same document context node before context node in document order, excluding ancestors , excluding attribute nodes , namespace nodes"


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -