我需要转换以下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>
要转换xml文件,我使用以下代码:
<?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>
当根元素为<Document>
时,代码工作正常,但当根为<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.002.001.03">
时,它不起作用
我能做什么?
答案 0 :(得分:2)
您需要在样式表中声明命名空间,为其指定前缀,并在寻址文档节点时使用前缀 - 例如(包括一些精简):
<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>