Not getting data to transform Xml

user3451965

I need to transform the following xml :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Document>
<CstmrPmtStsRpt>
<GrpHdr>
<MsgId>DATIR0022G12345678100</MsgId>
<CreDtTm>2013-07-18T06:00:01</CreDtTm>
<InitgPty>
<Id>
<OrgId>
<BICorBEI>BICBICMMXXX</BICorBEI>
</OrgId>
</Id>
</InitgPty>
</GrpHdr>
</CstmrPmtStsRpt>
</Document>

To transform the xml file I am using the following code :

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
      <root>
      <xsl:for-each select="//GrpHdr">
        <tblGrpHdr1>
          <xsl:variable name="CurrentHeaderID" select="position()"/>
          <HeaderID>
            <xsl:value-of select="$CurrentHeaderID"/>
          </HeaderID>
          <MsgId>
            <xsl:value-of select="./MsgId"/>
          </MsgId>
          <CreDtTm>
            <xsl:value-of select="./CreDtTm"/>
          </CreDtTm>
          <BICorBEI>
            <xsl:value-of select="./InitgPty/Id/OrgId/BICorBEI"/>
          </BICorBEI>
        </tblGrpHdr1>
      </xsl:for-each>
      </root>
    </xsl:template>
</xsl:stylesheet>

The code works fine when the root-element is <Document>, but it does not work when the root is <Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.002.001.03">

What I can do?

michael.hor257k

You need to declare the namespace in your stylesheet, assign it a prefix, and use the prefix when addressing the document nodes - for example (including some streamlining):

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pain="urn:iso:std:iso:20022:tech:xsd:pain.002.001.03"
exclude-result-prefixes="pain">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <root>
        <xsl:for-each select="pain:Document/pain:CstmrPmtStsRpt/pain:GrpHdr">
            <tblGrpHdr1>
                <HeaderID>
                    <xsl:value-of select="position()"/>
                </HeaderID>
                <MsgId>
                    <xsl:value-of select="pain:MsgId"/>
                </MsgId>
                <CreDtTm>
                    <xsl:value-of select="pain:CreDtTm"/>
                </CreDtTm>
                <BICorBEI>
                    <xsl:value-of select="pain:InitgPty/pain:Id/pain:OrgId/pain:BICorBEI"/>
                </BICorBEI>
            </tblGrpHdr1>
        </xsl:for-each>
    </root>
</xsl:template>

</xsl:stylesheet>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related