?
Solved

preceding-sibling

Posted on 2005-03-16
4
Medium Priority
?
490 Views
Last Modified: 2012-05-05
I have a probkem using preceding-sibling

Here is my xml and xslt.  I am trying to output the Age and previous age.  This is just a contrived example to illustrate an example.

Any ideas?

Thanks

Dave

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="C:\Documents and Settings\armoud\Desktop\EEPeople.xslt"?>
<TestData>
      <item>
            <column name="Firstname" value="Fred"/>
            <column name="Surname" value="Green"/>
            <column name="Age" value="28"/>
            <column name="Sex" value="M"/>
            <column name="MemberNumber" value="58"/>
      </item>
      <item>
            <column name="Firstname" value="Dave"/>
            <column name="Surname" value="Bloggs"/>
            <column name="Age" value="38"/>
            <column name="Sex" value="M"/>
            <column name="MemberNumber" value="28"/>
      </item>
      <item>
            <column name="Firstname" value="Tiffany"/>
            <column name="Surname" value="Brown"/>
            <column name="Age" value="24"/>
            <column name="Sex" value="F"/>
            <column name="MemberNumber" value="15"/>
      </item>
</TestData>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
      
      <xsl:template match="/TestData/item">
            <xsl:variable name="PreviousItem" select="preceding-sibling::item/column[@name=Age]/@value"></xsl:variable>
            
            Current Age: <xsl:value-of select="column[@name='Age']/@value"></xsl:value-of><br />
            Previous Age: <xsl:value-of select="$PreviousItem"></xsl:value-of><br />
            <hr />
      
      </xsl:template>
</xsl:stylesheet>
0
Comment
Question by:daveamour
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
bloodredsun earned 200 total points
ID: 13555491
You missed the '' around age and the position comparison so that you only return the closest preceding sibling item



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
      <xsl:template match="/">
            <xsl:for-each select="/TestData/item">
             Current Age: <xsl:value-of select="child::column[@name='Age']/@value"/>
                  <br/>
            Previous Age: <xsl:value-of select="preceding-sibling::item[position()=1]/column[@name='Age']/@value"/>
                  <hr/>
            </xsl:for-each>
      </xsl:template>
</xsl:stylesheet>
0
 
LVL 19

Author Comment

by:daveamour
ID: 13555726
Thanks man, your a star!

How do you learn all this stuff, I find it very unintuitive and books I have tried reading seem difficult too.

Thanks

Dave
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13556133
O'Reillys XSLT book and 5 years experience. The only way comes from doing it, and it's much easier if you can do it in a decent editor like XMLSPy that gives you XPath functionality in an xml doc so you can test your expressions.

Cheers for the points and the grade.
0
 
LVL 19

Author Comment

by:daveamour
ID: 13556175
Ok thanks

I'm using Xml Spy so at least I'm on the right path...

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question