[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 492
  • Last Modified:

preceding-sibling

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
daveamour
Asked:
daveamour
  • 2
  • 2
1 Solution
 
bloodredsunCommented:
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
 
daveamourAuthor Commented:
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
 
bloodredsunCommented:
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
 
daveamourAuthor Commented:
Ok thanks

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

0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now