0

I am facing issue while trying to get the node value of "streetNumber" which is inside namespaced XML. Below is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702" xmlns:tns="http://xsd.hometrack.com.au/pih/" xmlns:vms="http://www.sandstone-vms.com.au/schema/vms/1.0" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xs="http://wsdl.hometrack.com.au/pih/">
   <soap:Body>
      <xs:getPropertyResponse xmlns="http://wsdl.hometrack.com.au/pih/" xmlns:xs="http://wsdl.hometrack.com.au/pih/">
         <pih xmlns="http://xsd.hometrack.com.au/pih/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <propertyResponse>
               <property>
                  <address>
                     <streetNumber>32</streetNumber>
                     <streetName>SPARKLE</streetName>
                  </address>
               </property>
            </propertyResponse>
         </pih>
      </xs:getPropertyResponse>
   </soap:Body>
</soap:Envelope>

And my XSLT for this is:

<xsl:template match="/soapenv:Envelope/soapenv:Body">
    <xsl:element name="response">       

        <xsl:element name="output">             
            <xsl:attribute name="streetNumber"><xsl:value-of select="xs:getPropertyResponse/pih/propertyResponse/property/address/streetNumber" /></xsl:attribute>              
        </xsl:element>

    </xsl:element>
</xsl:template>

Below is the result where I am not getting value for "StreetNumber"

<?xml version="1.0" encoding="UTF-8"?>
    <response><output streetNumber=""/>
</response>

Any help on this will be helpful for me. Many thanks in advance!

Sahil
  • 3
  • 2
  • Closing as a duplicate; you can find thousands of other people who have asked essentially the same question by searching for "XSLT default namespace". No criticism intended: a lot of people make this mistake. – Michael Kay Feb 06 '20 at 11:13

1 Answers1

1

In XSLT, needs to define those namespaces, which are in XML.

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xs="http://wsdl.hometrack.com.au/pih/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xsl:template match="soap:Envelope/soap:Body">
    <xsl:element name="response">       
        <xsl:element name="output">          
            <xsl:attribute name="streetNumber"><xsl:value-of select="xs:getPropertyResponse/*:pih/*:propertyResponse/*:property/*:address/*:streetNumber" /></xsl:attribute>
        </xsl:element>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

Reult:

<response><output streetNumber="32"/></response>
Rudramuni TP
  • 1,268
  • 2
  • 16
  • 26
  • Thanks a lot. It is working great. – Sahil Feb 06 '20 at 22:42
  • Hi Rudramuni, My above xml got extended with below two records and I want to sort that based on it's date which is a attribute. Can you please help. – Sahil May 12 '20 at 06:02
  • https://stackoverflow.com/questions/61745059/apply-xslt-on-xml-to-sort-the-records-by-date-which-is-an-attribute – Sahil May 12 '20 at 06:34