Question

I need help to read into a xml file with the this definition

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CstmrCdtTrfInitn>
        <GrpHdr>
            <NbOfTxs>3</NbOfTxs>

The problem is that I can't read the nodes when the xml has Document xmlns... (I tested removing this line and I can read the nodes)

My xsl is this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="no" omit-xml-declaration="yes"/>
       <xsl:template match='GrpHdr'>
           <NbOfTxs><xsl:value-of select="NbOfTxs"/></NbOfTxs>
       </xsl:template>
</xsl:stylesheet>
Was it helpful?

Solution

The elements in your input XML have a default namespace. You need to declare this namespace in your XSLT stylesheet, too and prefix any input elements you want to match.

You do not need to match the GrpHdr elements if you'd like to output the NbOfTxs element and its content anyway.

Input

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <CstmrCdtTrfInitn>
        <GrpHdr>
            <NbOfTxs>3</NbOfTxs>
        </GrpHdr>
    </CstmrCdtTrfInitn>
</Document>

Stylesheet

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:nsa="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">

   <xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="no" omit-xml-declaration="yes"/>

   <xsl:template match='nsa:NbOfTxs'>
      <xsl:copy>
         <xsl:value-of select="."/>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

Output

Note that the NbOfTxs element still has its namespace in the output (you did not say whether you want to keep it or not).

<NbOfTxs xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">3</NbOfTxs>

OTHER TIPS

Declare a namespace prefix for the namespace in your XSLT and then select using that prefix: see similar question here

How to 'select' from XML with namespaces?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top