xmlxsltxsdmsxml6

Extract namespace attribute from elements of xml


There is XML file (shown below). I wonder how can I extract namespaces (xmlns attribute) from AppHdr and Document elements using MSXML6.0 parser. I can express path to namespace attribute of AppHdr as "Messages/:AppHdr/namespace::", but it works in xPath 2.0 and above. If I try send xsl with such path (Messages/:AppHdr/namespace::) to msxml6.0 it returns error.

    <?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" encoding="utf-8">
    <xsl:template match="Messages/*:AppHdr/namespace::*">
        <xsl:copy>
            <xsl:value-of select="."/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

msxsl.exe error

Can anybody suggest the right view of xpath-expression?

XML file:

<?xml version="1.0" encoding="utf-8"?>
<Messages>
    <AppHdr xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.01">
        <Fr>
            <OrgId>
                <Nm>Open Brok Ltd.</Nm>
                <Id>
                    <OrgId>
                        <Othr>
                            <Id>1083732703772</Id>
                            <Issr>OGRN</Issr>
                        </Othr>
                    </OrgId>
                </Id>
            </OrgId>
        </Fr>
        <BizMsgIdr>12345</BizMsgIdr>
        <MsgDefIdr>semt.017.001.09</MsgDefIdr>
        <CreDt>2020-01-24T14:32:23Z</CreDt>
    </AppHdr>
    <Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:semt.017.001.09">
        <SctiesTxPstngRpt>
            <Pgntn>
                <PgNb>1</PgNb>
                <LastPgInd>true</LastPgInd>
            </Pgntn>
            <StmtGnlDtls>
                <RptNb>
                <Shrt>000</Shrt>
              </RptNb>
              <StmtPrd>
                  <FrDtToDt>
                  <FrDt>2017-04-04</FrDt>
                  <ToDt>2017-04-04</ToDt>
                  </FrDtToDt>
              </StmtPrd>
              <Frqcy>
                  <Cd>DAIL</Cd>
              </Frqcy>
              <UpdTp>
                  <Cd>COMP</Cd>
              </UpdTp>
              <StmtBsis>
                  <Cd>SETT</Cd>
              </StmtBsis>
              <ActvtyInd>true</ActvtyInd>
              <SubAcctInd>true</SubAcctInd>
            </StmtGnlDtls>
            <AcctOwnr>
                <Id>
                    <PrtryId>
                        <Id>1067534439317</Id>
                        <Issr>OGRN</Issr>
                    </PrtryId>
                </Id>
            </AcctOwnr>
            <SfkpgAcct>
                <Id>R50938400000</Id>
                <Nm>NSD</Nm>
            </SfkpgAcct>
            <SubAcctDtls>
                <SfkpgAcct>
                    <Id>R50526502570</Id>
                    <Nm>DVP Euroclear/NSD</Nm>
                </SfkpgAcct>
                <ActvtyInd>true</ActvtyInd>
                <FinInstrmDtls>
                    <FinInstrmId>
                        <ISIN>US92719A1060</ISIN>
                        <Desc>VIMPELCOM-2-ADR</Desc>
                    </FinInstrmId>
                    <Tx>
                        <AcctOwnrTxId>OP201704040000491</AcctOwnrTxId>
                        <CorpActnEvtId>AdmPor - VIMPELCOM-2</CorpActnEvtId>
                        <TxDtls>
                            <TxActvty>
                                <Cd>CORP</Cd>
                            </TxActvty>
                            <SctiesMvmntTp>DELI</SctiesMvmntTp>
                            <Pmt>FREE</Pmt>
                            <PstngQty>
                                <Qty>
                                    <Unit>540000</Unit>
                                </Qty>
                            </PstngQty>
                            <FctvSttlmDt>
                                <Dt>2017-04-04</Dt>
                            </FctvSttlmDt>
                            <TxAddtlDtls>AdmPor - VIMPELCOM-2 от 04.04.2017</TxAddtlDtls>
                        </TxDtls>
                    </Tx>
                </FinInstrmDtls>
            </SubAcctDtls>
        </SctiesTxPstngRpt>
    </Document>
</Messages>

Solution

  • If you want to know in what namespace the AppHdr element is, you can use:

    <xsl:value-of select="namespace-uri(/Messages/*[local-name()='AppHdr'])"/>
    

    Note that the expression:

    /Messages/*[local-name()='AppHdr']/namespace::*
    

    will select all namespaces that are in-scope for the AppHdr element - and in XSLT 1.0 the xsl:value-of instruction will return the string-value of only the first node of the selected node-set.