XSLT 中的命名空间
下面是示例文件
<?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>
下面是 XSLT 转换代码:
<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>
当我从 XML 文件中排除以下字符串(在文档标记中)时,我就可以进行转换。
'xmlns='urn:iso:std:iso:20022:tech:xsd:pain.002.001.02' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
但是当我包含相同的字符串时,我的 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 xsl 声明中添加 xml 文档命名空间
,并通过前缀
doc:
引用您的节点。例如:编辑
对于您的问题尝试:
Add your xml document namespace in your xsl declaration
and refer to your nodes by the prefix
doc:
. E.g:EDIT
For your problem try:
这是一个常见问题解答。 XPath 将无前缀的元素名称视为属于“无命名空间”。
每当 XML 文档具有默认命名空间时,按名称引用元素的唯一方法就是按前缀引用它们,其中前缀绑定到默认命名空间。
这意味着:
将前缀(例如“xxx”)关联到文档的默认命名空间。
在任何 XPath 表达式或匹配模式中,将每个元素名称替换为相应的前缀名称。例如,
严格遵循根据以上两条规则,修改后的样式表的行为如下:与应用于没有默认命名空间的 XML 文档的原始样式表相同。
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:
Associate a prefix (say "xxx") to the document's default namespace.
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.