Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

having problem with XPath expression

 
Himal Chuli
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • 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 ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this instead:

[ August 06, 2002: Message edited by: Valentin Crettaz ]
 
Sergey Gribok
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • 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;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic