• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XSLT Sorting Problem

 
Ranch Hand
Posts: 511
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello...
I am trying to sort mailing addresses from states that start with the letter "M"... I am using the Xalan-J 2.5.2 XSLT parser...
Here's what I did:
1) Created a names.xml file:
<?xml version="1.0"?>
<addressbook>
<address>
<name>
<title>Mr.</title>
<first-name>Chester Hasbrouck</first-name>
<last-name>Frisby</last-name>
</name>
<street>1234 Main Street</street>
<city>Sheboygan</city>
<state>WI</state>
<zip>48392</zip>
</address>
<address>
<name>
<first-name>Mary</first-name>
<last-name>Backstayge</last-name>
</name>
<street>283 First Avenue</street>
<city>Skunk Haven</city>
<state>MA</state>
<zip>02718</zip>
</address>
<address>
<name>
<title>Ms.</title>
<first-name>Natalie</first-name>
<last-name>Attired</last-name>
</name>
<street>707 Breitling Way</street>
<city>Winter Harbor</city>
<state>ME</state>
<zip>00218</zip>
</address>
<address>
<name>
<first-name>Harry</first-name>
<last-name>Backstayge</last-name>
</name>
<street>283 First Avenue</street>
<city>Skunk Haven</city>
<state>MA</state>
<zip>02718</zip>
</address>
<address>
<name>
<first-name>Mary</first-name>
<last-name>McGoon</last-name>
</name>
<street>103 Bryant Street</street>
<city>Boylston</city>
<state>VA</state>
<zip>27318</zip>
</address>
<address>
<name>
<title>Ms.</title>
<first-name>Amanda</first-name>
<last-name>Reckonwith</last-name>
</name>
<street>930-A Chestnut Street</street>
<city>Lynn</city>
<state>MA</state>
<zip>02930</zip>
</address>
</addressbook>
2) Created a namessorter.xsl file:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl utput method="text" indent="no"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>
<xsl:template match="/">
<xsl:for-each select="addressbook/address/[starts-with(state, 'M')]">
<xsl:sort select="name/last-name"/>
<xsl:sort select="name/first-name"/>
<xsl:if test="name/title">
<xsl:value-of select="name/title"/>
<xsl:text> </xsl:text>
</xsl:if>
<xsl:value-of select="name/first-name"/>
<xsl:text> </xsl:text>
<xsl:value-of select="name/last-name"/>
<xsl:value-of select="$newline"/>
<xsl:value-of select="street"/>
<xsl:value-of select="$newline"/>
<xsl:value-of select="city"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="state"/>
<xsl:text> </xsl:text>
<xsl:value-of select="zip"/>
<xsl:value-of select="$newline"/>
<xsl:value-of select="$newline"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
3) When I went into the command line (on Windows 2000 Professional), I typed:
java org.apache.xalan.xslt.Process -in names.xml -xsl namessorter.xsl -out list.text
I get this error:
SystemId Unknown; Line #15; Column #-1; XSLT Error (javax.xml.transform.TransformerConfigurationException): javax.xml.transform.TransformerException: javax.xml.
transform.TransformerException: A location step was expected following the '/' or '//' token.
Line#15, is of course:
<xsl:for-each select="addressbook/address/[starts-with(state, 'M')]">
If anyone can help me with this problem, I will be most grateful....
All the best,
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl utput method="text" indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>
<xsl:template match="/">
<xsl:for-each select="addressbook/address/state[starts-with(., 'M')]/..">
<xsl:sort select="name/last-name"/>
<xsl:sort select="name/first-name"/>
<xsl:if test="name/title">
<xsl:value-of select="name/title"/>
<xsl:text> </xsl:text>
</xsl:if>
<xsl:value-of select="name/first-name"/>
<xsl:text> </xsl:text>
<xsl:value-of select="name/last-name"/>
<xsl:value-of select="$newline"/>
<xsl:value-of select="street"/>
<xsl:value-of select="$newline"/>
<xsl:value-of select="city"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="state"/>
<xsl:text> </xsl:text>
<xsl:value-of select="zip"/>
<xsl:value-of select="$newline"/>
<xsl:value-of select="$newline"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

--Nimchi
 
Unnsse Khan
Ranch Hand
Posts: 511
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!
Can you explain why my code didn't work, and why yours did? I am very impressed by the workaround.
Kindest regards,
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your problem is with the node selection in xsl:for-each, not a sort problem per se.
<xsl:for-each select="addressbook/address/[starts-with(state, 'M')]">
doesn't select anything. Use of [] is to limit certain nodes. If you use
<xsl:for-each select="addressbook/address[starts-with(state, 'M')]">
should work also.
 
Greenhorn
Posts: 14
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Iam getting this error, previously it was working fine and i don't know why is this throwing this error and it is not pointing to specific line.

Can anyone help.


javax.xml.transform.TransformerConfigurationException: javax.xml.transform.TransformerConfigurationException: javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: A location step was expected following the '/' or '//' token.
 
I'm full of tinier men! And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic