由于名称空间的原因,无法使用XSLT转换XML;xmlns";
我一直在尝试使用XSLT转换XML文件,但由于一些问题,即“xmlns”,它无法转换。一、 为了我的生命,我找不到什么是错的 XML文件:由于名称空间的原因,无法使用XSLT转换XML;xmlns";,xml,xslt,Xml,Xslt,我一直在尝试使用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
<?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>
4000001
试验
SEF1
2012-07-25T08:57:00Z
恢复性反应
4000123
1.
CM1
会计112
这是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>
4000001
试验
SEF1
2012-07-25T08:57:00Z
恢复性反应
4000123
1.
cb12444
CM1
acc1
帐户1
结果输出:
<?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>
4000001
测试
SEF1
2012-07-25T08:57:00Z
恢复性反应
4000123
1.
CM1
会计12
只有在删除xmlns=”时,才会出现预期的输出http://www.fpml.org/FpML-5/pretrade"
如果在XML文件中,我向“xmlns”添加了一个标记,例如xmlns:AAA=,那么这种方法同样有效http://www.fpml.org/FpML-5/pretrade"
但是我必须在不编辑XML文件的情况下对其进行转换您的XML源中的元素位于
xmlns=”http://www.fpml.org/FpML-5/pretrade“
名称空间。您必须在样式表中声明此名称空间,为其指定前缀,并在选择或匹配XML中的元素时使用该前缀。下面是一个最小化的示例:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fmpl="http://www.fpml.org/FpML-5/pretrade"
xmlns="http://www.fpml.org/FpML-5/pretrade"
exclude-result-prefixes="fmpl">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/fmpl:restoreCredit">
<restoreCredit>
<header>
<FpHdMsgID>
<xsl:attribute name="FpHdMsgIDScheme">
<xsl:value-of select="fmpl:header/fmpl:messageId/@messageIdScheme" />
</xsl:attribute>
<xsl:value-of select="fmpl:header/fmpl:messageId"/>
</FpHdMsgID>
</header>
</restoreCredit>
</xsl:template>
</xsl:stylesheet>
- 第一个声明是指定
fpml前缀的声明 旨在使您能够处理输入XML中的元素
- 第二个声明声明了
样式表本身:您写入样式表的任何文字元素(例如示例中的
)都将被放入默认名称空间
两个名称空间相同(即具有相同的URI)这一事实可以说是巧合 您必须在xslt文件中注册名称空间,并在所有XPath中引用该名称空间。例如
xmlns:abc=”http://www.fpml.org/FpML-5/pretrade“
和
是否有人在不编辑XML的情况下执行此操作?另外,我真的很抱歉,在XSLT文件中,“xmlns:t”应该只有“xmlns”,这里有数百篇帖子问同样的问题,我标记为重复的是我第一次在Google上点击“XSLT默认名称空间”。您在样式表中声明了xmlns:t
,但实际上您需要在XPath表达式中使用前缀,如t:header/t:messageId
,我回滚了您的编辑,因为没有示例输入,问题和答案就没有意义,输出和当前XSLT。请记住,所有SO帖子的完整修订历史都是公开的,因此,如果您试图删除“机密”信息,那么简单地编辑这样的问题无论如何都不会有帮助。如果您在问题中错误地包含了敏感信息,那么您需要将其标记为版主注意,并要求删除整个问题。谢谢您的回答michael.hor257k!!但是,在根本不编辑XML文件的情况下,是否仍可以执行此操作?@Shn根本不需要编辑XML文件;所有这些都是在XSLT样式表中完成的。
<?xml version="1.0" encoding="utf-8"?>
<restoreCredit xmlns="http://www.fpml.org/FpML-5/pretrade">
<header>
<FpHdMsgID FpHdMsgIDScheme="http://www.traiana.com/fpml">4000001</FpHdMsgID>
</header>
</restoreCredit>
xmlns:fmpl="http://www.fpml.org/FpML-5/pretrade"
xmlns="http://www.fpml.org/FpML-5/pretrade"