user1164713
user1164713

Reputation: 51

namespaces not giving expected results

I am trying to read the following XML using XSLT, but can not get the expected results.

If i remove the "xmlns:a="http://schemas.datacontract.org/2004/07/CoreModels" namespace from the txnDetail node, then the xslt below works fine ???

What am i doing wrong ?

The input XML:

<TransactionRsp xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
   <avlBal>848.35</avlBal>
   <blkAmt>0</blkAmt>
   <txnDetail xmlns:a="http://schemas.datacontract.org/2004/07/CoreModels">
      <a:txnDetail>
         <a:billAmount>400</a:billAmount>
         <a:txnDateTime>2012-02-23T14:35:45</a:txnDateTime>
      </a:txnDetail>
      <a:txnDetail>
         <a:billAmount>10</a:billAmount>
         <a:txnDateTime>2012-07-30T12:22:14</a:txnDateTime>
      </a:txnDetail>
   </txnDetail>
</TransactionRsp>

The XSLT stylesheet:

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
 <root>
     <xsl:for-each select="TransactionRsp/txnDetail/txnDetail">
      <row>
        <col name="billAmount"><xsl:value-of select="billAmount"/></col>
        <col name="itemID"><xsl:value-of select="itemID"/></col>
      </row>
      </xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>

Upvotes: 1

Views: 73

Answers (1)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51721

You need to qualify your XPath expressions with namespace prefix:

  <xsl:for-each select="TransactionRsp/txnDetail/a:txnDetail">
  <row>
    <col name="billAmount"><xsl:value-of select="a:billAmount"/></col>
    <col name="itemID"><xsl:value-of select="a:itemID"/></col>
  </row>
  </xsl:for-each>


EDIT: The namespace must be declared for your XSLT file too. Thanks to @hcayless's comments below.

<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:a="http://schemas.datacontract.org/2004/07/CoreModels" >

Upvotes: 2

Related Questions