• 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
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

having problem with XPath expression

 
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,
here is my xml file order.xml
<?xml version="1.0"?>
<?xml:stylesheet type="text/xsl" href="node.xsl"?>
<order number="1234">
<date>2001/1/1/</date>
<customer id="8888">Company A</customer>
<item>
<part-number warehouse="Warehouse">
E16-25A</part-number>
<description>Production-Class</description>
<quantity>16</quantity>
</item>
</order>
here is my stylesheet order.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl: output method="text"/>
<xsl:template match="/order/item">
<xsl:value-of select="part-number"/>
</xsl:template>
</xsl:stylesheet>
and i am getting the following output:
2001/1/1/ Company A E16-25A
my qestion is why am i getting the date and the customer here?I am expecting to get only the part-number i.e, E16-25A. what am i not understanding here?i would appreciate if anyone could clarify me it. thanks
[Code edited by Val]
[ August 06, 2002: Message edited by: Valentin Crettaz ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this instead:

[ August 06, 2002: Message edited by: Valentin Crettaz ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get such output because there are default template rules exists in XSLT and you didn't redefine this rules. The default rule for elements is:
<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>
The default rule for text and attributes is:
<xsl:template match="text()|@*">
<xsl:value-of select="."/>
</xsl:template>

You didn't define rule for root node "/" therefore default rule uses.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Himal,
You probably got the point by now ;-)
But here is another version that will produce what you want.
I would go with Valentin's version, since it's more explicit.


Cheers,
Dan
 
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

Originally posted by Valentin Crettaz:
Try this instead:

[ August 06, 2002: Message edited by: Valentin Crettaz ]


thanks val.
 
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

Originally posted by Sergey Gribok:
You get such output because there are default template rules exists in XSLT and you didn't redefine this rules. The default rule for elements is:
<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>
The default rule for text and attributes is:
<xsl:template match="text()|@*">
<xsl:value-of select="."/>
</xsl:template>

You didn't define rule for root node "/" therefore default rule uses.


thanks. appreciate it.
 
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also a little addition to this ......
<xsl:template match="text()|@*">
<xsl:value-of select="."/>
</xsl:template>
You have to keep in mind that the statement -
<xsl:apply-templates />
will not take the recursion to the attributes ( i guess attributes are not treated as nodes) and hence you donot see the text included in the attributes getting printed along with the text() nodes' content; You have to force the thing to happen for attributes (if you really want to) by something like -
<xsl:apply-templates select="@*"/> for a given element in context;
 
Do you pee on your compost? Does this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic