kiran puram
kiran puram

Reputation: 9

Name space in XSLT

Below is the example file

<?xml version='1.0' encoding='UTF-8'?>
<Document xmlns='urn:iso:std:iso:20022:tech:xsd:pain.002.001.02' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>  
<pain.002.001.02>
------------
-------------
-------------

</pain.002.001.02>
</Document> 

Below is the XSLT Transformation code :

<xsl:transform version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:sap="http://www.sap.com/sapxsl"
  xmlns:asx="http://www.sap.com/abapxml"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xsl:strip-space elements="*"/>

<xsl:template match="/">


<asx:abap version="1.0">
<asx:values>

 <xsl:for-each select="Document/pain.002.001.02">
<ACK_HEADER>
<ZTFI_PYSTATUS_HD>
  <xsl:for-each select="GrpHdr[1]">
  <ORGNLMSG_ID><xsl:value-of select="MsgId"/></ORGNLMSG_ID>
<!--  <MSG_CRTD_DATE><xsl:value-of select="CreDtTm"/></MSG_CRTD_DATE>-->
<xsl:variable name="date_time" select="CreDtTm"/>
<MSG_CRTD_DATE><xsl:value-of select="substring-before(@T,',$date_time')"/></MSG_CRTD_DATE>
<!--Payment Acknowledgement Header data-->
</xsl:for-each>

<xsl:for-each select="OrgnlGrpInfAndSts[1]">
<MSGID><xsl:value-of select="OrgnlMsgId"/></MSGID>
<xsl:variable name="msgid" select="OrgnlMsgId"/>
<ORIGNLMSG_NM_ID><xsl:value-of select="OrgnlMsgNmId"/></ORIGNLMSG_NM_ID>
<ORGNL_NO_OF_TRAN><xsl:value-of select="OrgnlNbOfTxs"/></ORGNL_NO_OF_TRAN>
<ORGNL_CNTRL_SUM><xsl:value-of select="OrgnlCtrlSum"/></ORGNL_CNTRL_SUM>
<ORGNL_FILE_STAT><xsl:value-of select="GrpSts"/></ORGNL_FILE_STAT>
<xsl:for-each select="NbOfTxsPerSts">
<xsl:choose>
<xsl:when test="DtldSts='ACSP'">
<ORGNL_NO_OF_ACSP><xsl:value-of select="DtldNbOfTxs"/></ORGNL_NO_OF_ACSP>
<ORGNL_ACSP_SUM><xsl:value-of select="DtldCtrlSum"/></ORGNL_ACSP_SUM>
</xsl:when>
<xsl:when test="DtldSts='RJCT'">
<ORGNL_NO_OF_RJCT><xsl:value-of select="DtldNbOfTxs"/></ORGNL_NO_OF_RJCT>
<ORGNL_RJCT_SUM><xsl:value-of select="DtldCtrlSum"/></ORGNL_RJCT_SUM>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>
</ZTFI_PYSTATUS_HD>
</ACK_HEADER>
<ACK_DETAIL>

<!--Payment Acknowledgement Detail data-->

<xsl:for-each select="TxInfAndSts">
<ZTFI_PYSTATUS_DT>
<MSGID><xsl:value-of select="$msgid"/></MSGID>
<PMT_INFO_IDENT><xsl:value-of select="OrgnlPmtInfId"/></PMT_INFO_IDENT>
<END_2_END_ID><xsl:value-of select="OrgnlEndToEndId"/></END_2_END_ID>
<TRAN_STATUS><xsl:value-of  select="TxSts"/></TRAN_STATUS>
<INSTRU_IDENT><xsl:value-of select="OrgnlInstrId"/></INSTRU_IDENT>
<xsl:for-each select="StsRsnInf[1]">
<STAT_RE_AD_INFO><xsl:value-of select="AddtlStsRsnInf"/></STAT_RE_AD_INFO>
</xsl:for-each>
<xsl:for-each select="OrgnlTxRef[1]">
<xsl:for-each select="Amt[1]">
<xsl:for-each select="InstdAmt[1]">
<INSTRCTD_AMT><xsl:value-of select="string()"/></INSTRCTD_AMT>
<CURRENCY><xsl:value-of select="@Ccy"/></CURRENCY>
</xsl:for-each>
</xsl:for-each>
<REQ_EXEC_DATE><xsl:value-of select="ReqdExctnDt"/></REQ_EXEC_DATE>
<xsl:for-each select="CdtrAcct[1]">
<xsl:for-each select="Id[1]">
<xsl:for-each select="PrtryAcct[1]">
<LIFNR><xsl:value-of select="Id"/></LIFNR>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>


</xsl:for-each>
</ZTFI_PYSTATUS_DT>
</xsl:for-each>
</ACK_DETAIL>
</xsl:for-each>
</asx:values>
</asx:abap>
</xsl:template>
</xsl:transform>

when i exclude the following string(in the Document tag) from the XML file i am able to transform.

'xmlns='urn:iso:std:iso:20022:tech:xsd:pain.002.001.02' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> 

but when i include the same string my XSLT transformation. it is not working.

Please do the needful.

Thanks and Regards, Kiran.

Upvotes: 1

Views: 1745

Answers (2)

Gregoire
Gregoire

Reputation: 24832

Add your xml document namespace in your xsl declaration

<xsl:transform version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:sap="http://www.sap.com/sapxsl"
  xmlns:asx="http://www.sap.com/abapxml"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:doc='urn:iso:std:iso:20022:tech:xsd:pain.002.001.02'
>

and refer to your nodes by the prefix doc:. E.g:

<xsl:for-each select="doc:pain.002.001.02">

EDIT For your problem try:

<xsl:for-each select="doc:Document/doc:pain.002.001.02">

Upvotes: 1

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

This is a FAQ. XPath treats unprefixed element names as belonging to "no namespace".

Whenever an XML document has a default namespace, the only way to refer to elements by name is to refer to them as prefixed, where the prefix is bound to the default namespace.

This means:

  1. Associate a prefix (say "xxx") to the document's default namespace.

  2. In any XPath expression or match pattern replace every element name by the corresponding preffixed name. For example, replace /a/b/c/d with /xxx:a/xxx:b/xxx:c/xxx:d

After following strictly the above two rules the modified stylesheet behaves as wanted: in the same way as the original stylesheet applied on an XML document that has no default namespace.

Upvotes: 0

Related Questions