• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Urgent Help needed on XPath

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi folks,
i have the following xml:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href = "Sales.xsl" type = "text/xsl" ?>
<SalesReport>
<Company>XML.com</Company>
<Period>2001-2002</Period>
<Sales Region = "EU">50,000</Sales>
<Sales Region = "NA">150,000</Sales>
<Sales Region = "AV">10,000</Sales>
</SalesReport>
and the following xsl:
-----------------------
<?xml version='1.0'?>
<!-- FILE NAME main.xsl -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'>
<xsl:template match="/child::SalesReport/child::Sales"> <xsl:value-of select = "@Region"/>
<xsl:value-of select = "."/>

</xsl:template>
</xsl:stylesheet>
it gives me the following result:
_______________________________
XML.com2001-2002 EU50,000 NA150,000 AV10,000
this is contrary to what i had anticipated to get.
i thought it would return me the values of Sales element and Region attribute that is,
EU50,000 NA150,000 AV10,000 leaving off the first two elements.
could someone explain me what's going on here? what am i missing as far as my understanding of XPath is concerned?
thanks.
himal
[ October 19, 2002: Message edited by: Himal Chuli ]
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to add a null template to suppress default
processing of text elements:
<xsl:template match="text()"/>
Without this, XSLT will walk down from the root, past any node that doesn't otherwise match a template, and print out the string value of that node.
[ October 19, 2002: Message edited by: Ron Newman ]
 
Himal Chuli
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ron,
it worked but could you please explain how does it (this default things) work in simple terms?
thanks.
himal
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
XSLT comes with the following default template built-in:
<xsl:template match="text()">
<xsl:value-of select="."/>
</xsl:template>
which prints out the value of any text node it encounters.
Often (maybe always), you don't want this, so you need to override it with the null template I posted in my earlier reply.
reply
    Bookmark Topic Watch Topic
  • New Topic