This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: namespace and XPath question


Howdy Partner,

> <xsl:template match="/">
> <PO>
>         <BuyerOrderNumber>
>                 <xsl:value-of select="//Order/OrderHeader/OrderNumber/BuyerOrderNumber"/>
>         </BuyerOrderNumber>
> </PO>
> </xsl:template>

When you use the name of an element (or attribute) in an XPath and you
don't give it a prefix, the XPath processor assumes that the element
is in no namespace. The first step in your XPath looks for any Order
elements in no namespace anywhere in your XML document...

> to translate this XML:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <Order xmlns="XCBL30.sox">
>   <OrderHeader >
>     <OrderNumber>
>       <BuyerOrderNumber>PO123</BuyerOrderNumber>
>     </OrderNumber>
>   </OrderHeader>
> </Order>

When you don't use a prefix for an element in XML, then the element is
in the default namespace, if there is one, or no namespace, if there
isn't a default namespace. The xmlns attribute sets the default
namespace for the element that it's on and for that element's
contents. In the above XML, all the elements in the document are in
the default namespace, 'XCBL30.sox'.

So when the XPath processor looks for an Order element in no
namespace, it doesn't find one.

You need to include the 'XCBL30.sox' namespace in your stylesheet, and
give it a prefix, e.g.:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:ord="XCBL30.sox">
...
</xsl:stylesheet>

Then you need to use this prefix in your XPath so that you can pick
elements in the 'XCBL30.sox' namespace:

<xsl:template match="/">
  <PO>
    <BuyerOrderNumber>
      <xsl:value-of select="/ord:Order/ord:OrderHeader
                              /ord:OrderNumber/ord:BuyerOrderNumber" />
    </BuyerOrderNumber>
  </PO>
</xsl:template>

Note that I changed the path so that it now only searches for Order
elements that are the document element rather than searching through
all descendents of the root node.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]