我一直在尝试使用 XSLT 转换 XML 文件,但由于一些问题,即“xmlns”,它没有转换。我,一辈子都找不到问题所在。
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<restoreCredit fpmlVersion="5-6"
xsi:schemaLocation="http://www.fpml.org/FpML-5/pretrade d:\_Test\_PM\FpML\5.6\pretrade\fpml-main-5-6.xsd"
xmlns="http://www.fpml.org/FpML-5/pretrade"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<header>
<messageId messageIdScheme="http://www.test.com/fpml">4000001</messageId>
<sentBy>Test</sentBy>
<sendTo>SEF1</sendTo>
<creationTimestamp>2012-07-25T08:57:00Z</creationTimestamp>
</header>
<parentCorrId corrIdScheme="http://www.test.com/fpml">RestoreCreditOnSEF</parentCorrId>
<corrId corrIdScheme="http://www.test.com/fpml">4000123</corrId>
<sqNumber>1</sqNumber>
<party id="cb12">
<partyId>CM1</partyId>
</party>
<account id="acc1">
<accountId>account112</accountId>
</account>
</restoreCredit>
这是 XSLT 文件:
<xsl:stylesheet version="1.0"
xmlns="http://www.fpml.org/FpML-5/pretrade"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.fpml.org/FpML-5/pretrade d:\_test\_PM\FpML\5.6\pretrade\fpml-main-5-6.xsd"
>
<xsl:output method ="xml" indent="yes"/>
<!-- NOTE: All tags have been checked if present, if not , the tags will not be shown in the output -->
<xsl:strip-space elements= "*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="rCredit">
<rCredit>
<xsl:if test="header">
<!-- Header -->
<header>
<FpHdMsgID>
<xsl:attribute name = "FpHdMsgIDScheme">
<xsl:value-of select = "header/messageId/@messageIdScheme" />
</xsl:attribute>
<xsl:value-of select = "header/messageId"/>
</FpHdMsgID>
<FpHdSentBy>
<xsl:value-of select = "header/sentBy" />
</FpHdSentBy>
<FpHdSentTo>
<xsl:value-of select="header/sendTo" />
</FpHdSentTo>
<FpHdCreateTime>
<xsl:value-of select= "header/creationTimestamp" />
</FpHdCreateTime>
<xsl:if test = "header/expiryTimeStamp">
<FpHdExpTime>
<xsl:value-of select= "header/expiryTimeStamp" />
</FpHdExpTime>
</xsl:if>
</header>
</xsl:if>
<!-- parentCorrId -->
<xsl:if test="parentCorrId">
<FpParentCorID>
<xsl:attribute name = "FpParentCorIDCorSch">
<xsl:value-of select = "parentCorrId/@corrIdScheme" />
</xsl:attribute>
<xsl:value-of select = "parentCorrId"/>
</FpParentCorID>
</xsl:if>
<!-- corrId -->
<xsl:if test="corrId">
<FpCorID>
<xsl:attribute name = "FpCorIDSch">
<xsl:value-of select = "corrId/@corrIdScheme" />
</xsl:attribute>
<xsl:value-of select = "corrId"/>
</FpCorID>
</xsl:if>
<!-- sqNumber -->
<xsl:if test="sqNumber">
<FpSeqNum>
<xsl:value-of select="sqNumber"/>
</FpSeqNum>
</xsl:if>
<!-- party -->
<xsl:if test="party">
<FpPartyID>
<xsl:value-of select = "party/@id" />
</FpPartyID>
<FpPartyIDValue>
<xsl:value-of select = "party/partyId" />
</FpPartyIDValue>
</xsl:if>
<!-- account -->
<xsl:if test="account">
<FpAccountId>
<xsl:value-of select="account/@id"/>
</FpAccountId>
<FpAccountIdAcc>
<xsl:value-of select="account/accountId"/>
</FpAccountIdAcc>
</xsl:if>
</rCredit>
</xsl:template>
</xsl:stylesheet>
预期输出:
<?xml version="1.0" encoding="UTF-8"?>
<restoreCredit xmlns="http://www.fpml.org/FpML-5/pretrade" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<header>
<FpHdMsgID FpHdMsgIDScheme="http://www.test.com/fpml">4000001</FpHdMsgID>
<FpHdSentBy>Test</FpHdSentBy>
<FpHdSentTo>SEF1</FpHdSentTo>
<FpHdCreateTime>2012-07-25T08:57:00Z</FpHdCreateTime>
</header>
<FpParentCorID FpParentCorIDCorSch="http://www.test.com/fpml">RestoreCreditOnSEF</FpParentCorID>
<FpCorID FpCorIDSch="http://www.test.com/fpml">4000123</FpCorID>
<FpSeqNum>1</FpSeqNum>
<FpPartyID>cb12444</FpPartyID>
<FpPartyIDValue>CM1</FpPartyIDValue>
<FpAccountId>acc1</FpAccountId>
<FpAccountIdAcc>account1</FpAccountIdAcc>
</restoreCredit>
结果输出:
<?xml version="1.0" encoding="UTF-8"?>
<restoreCredit xmlns="http://www.fpml.org/FpML-5/pretrade" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" fpmlVersion="5-6" xsi:schemaLocation="http://www.fpml.org/FpML-5/pretrade d:\_test\_PM\FpML\5.6\pretrade\fpml-main-5-6.xsd">
<header>
<messageId messageIdScheme="http://www.test.com/fpml">4000001</messageId>
<sentBy>test</sentBy>
<sendTo>SEF1</sendTo>
<creationTimestamp>2012-07-25T08:57:00Z</creationTimestamp>
</header>
<parentCorrId corrIdScheme="http://www.test.com/fpml">RestoreCreditOnSEF</parentCorrId>
<corrId corrIdScheme="http://www.test.com/fpml">4000123</corrId>
<sqNumber>1</sqNumber>
<party id="c3321">
<partyId>CM1</partyId>
</party>
<account id="acc1">
<accountId>account12</accountId>
</account>
</restoreCredit>
仅当我删除 xmlns="http://www.fpml.org/FpML-5/pretrade" 时才会出现预期输出
如果在 XML 文件中,我向“xmlns”添加一个标签,例如 xmlns:AAA="http://www.fpml.org/FpML-5/pretrade",这也有效
但我必须在不编辑 XML 文件的情况下对其进行转换.