Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what does preceding:: do in xsl 1

Status
Not open for further replies.

harmmeijer

Programmer
Mar 1, 2001
869
CN
This is the xml page:

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<?xml-stylesheet type='text/xsl' href='test.xsl'?>
<root>
<software>
<name>software1</name>
</software>
<software>
<name>software2</name>
</software>
<software>
<name>software3</name>
</software>
</root>


This is the xsl page:


<?xml version='1.0'?>
<xsl:stylesheet version=&quot;1.0&quot;
xmlns:xsl=&quot;

<xsl:template match=&quot;root/software&quot; mode=&quot;grp-grp&quot;>
current software: <xsl:value-of select=&quot;name&quot; />
preceding software: <xsl:value-of select=&quot;preceding::software/name&quot; /><br></br>
</xsl:template>

<xsl:template match=&quot;/&quot;>
<xsl:apply-templates select=&quot;root/software&quot; mode=&quot;grp-grp&quot;/>
</xsl:template>

</xsl:stylesheet>


This is what I see when I open the xml page in IE5:

current software: software1 preceding software:
current software: software2 preceding software: software1
current software: software3 preceding software: software1


I think I understand the first two lines but why is preceding::software/name still software1 the third time (or the hundreth time for that matter)?
What is the use of preceding?
How can I get the previous value of software?
 
preceding::software/name[1]

This will show the pref name.

 
Sorry, just posted this in another thread...

preceding::
represents all nodes before the currently selected node, excluding ancestor nodes, in reverse document order. Which, in english, mean it goes backwards thru all nodes (including text nodes) between the currently selected node and its immediate parent.

Hope this helps.
 
The preceding shows the nodes before current node in physical order.
If I do a sort it gives me values I cannot use.
For example
<root>
<item>
<name>bbb</name>
</item>
<item>
<name>aaa</name>
</item>
.... a lot more items
</root>

in xsl
<xsl:key name=&quot;grp_key&quot; match=&quot;item&quot; use=&quot;name&quot;/>
....
<xsl:template match=&quot;root&quot; mode=&quot;grp&quot;>
<xsl:for-each select=&quot;key('grp_key', name)&quot;>
<xsl:sort select=&quot;name&quot;/>

preceding::name will give me bbb and then empty because physically bbb is the preceding of aaa and nothing is the preceding of bbb.
I want to know how I can get the preceding of the key('grp_key', name) (in this case first time nothing and second time aaa.
For now grouping works because I set the order by clause in the sql statement so the nodes are physically alphbetical (first main group then sub group etc etc.).
If I need to regroup I have to run the sql statement again and re connect to the database.

Thanks for your help uura.
 
I still want to know how to get the previous value of the key(... and not the physical previous value(s) of the current node.
Of course the above example will not work, here is how it should be:
xsl:
<xsl:stylesheet version=&quot;1.0&quot;
xmlns:xsl=&quot;<xsl:key name=&quot;grp_key&quot; match=&quot;item&quot; use=&quot;name&quot;/>

<xsl:template match=&quot;/&quot;>
<xsl:apply-templates select=&quot;root&quot; mode=&quot;grp&quot;/>
</xsl:template>

<xsl:template match=&quot;root&quot; mode=&quot;grp&quot;>
<xsl:for-each select=&quot;key('grp_key', item/name)&quot;>
<xsl:sort select=&quot;name&quot;/>
<xsl:value-of select=&quot;name&quot;/> preceding: <xsl:value-of select=&quot;preceding::name&quot;/><br/>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>

xml:
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<?xml-stylesheet type='text/xsl' href='test.xsl'?>
<root>
<item>
<name>bbb</name>
</item>
<item>
<name>aaa</name>
</item>
</root>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top