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!

Accessing the previous node from the current element 1

Status
Not open for further replies.

Sarky78

Programmer
Oct 19, 2000
878
GB
Hi,

I have got an xml document that now looks something like this:

Code:
<a>
  <b>
    <c>1</c>
    <d>2</d>
    <e>3</e>
    <f>4</f>
  </b>
  <b>
    <c>2</c>
    <d>3</d>
    <e>4</e>
    <f>5</f>
  </b>
  <b>
    <c>3</c>
    <d>4</d>
    <e>5</e>
    <f>6</f>
  </b>
</a>

When i am looping over the document using xslt I need to be able to compare the current <f> element with the previous <f> element, if they are different then i need to change the style that is applied to the output.

We are using .net to generate/transform the document and this has got problems with preceeding-sibling apparently that puts the server into a 100% cpu consumption which is nice., so that's not an option.

I thought that I would be able to do something like this:

<xsl:if test="f != f[position()-1]">

but this doesn't work.

Anyone got any ideas?

Tony

P.S thanks to all for your help so far
 
Tony,

The normal XPath expression (assuming you are in the context of a b element would be:
Code:
f != preceding-sibling::f[1]
Have you tried that?

Tom Morrison
 

there is an article on msdn about preceding-sibling crashing .net with 100% cpu usage, so that's not going to be an option until ms fix .net with sp2 or they release v2
 
Tony,

That's really interesting. I cannot imagine how something as important as the preceding-sibling axis crashing a released MS product sneaked by the QA folks.

Don't suppose you can sort these? If so then you could revert to using Muench...

Tom Morrison
 
Tom,

We were using the Muench method that you kindly helped with a while back, and that caused another problem with .net. it caused the transformation to take something in the region of 10 minutes to process. It has something to do with badly formed xml documents, and .net. the document is something that is coming back to us as a webservice from a third party so we have to live with it. there is a patch but it is not on general release and is not recommended for production environments!

We tried it in ColdFusion and that works fine, put it into .net and kaboooooom!

I'll brave it tomorrow when our server guy is out (he's very protective of his 'babies') with the preceeding-sibling and see if i crash the server!

Tony
 
What context are you trying to do this from?
Code:
<xsl:if test="f != f[position()-1]">
This suggests you are trying from the <b> node. In which case, you could do:
Code:
<xsl:variable name="pos" select="position()"/>
<xsl:if test="f != ../b[$pos - 1]/f">

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top