首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由于名称空间"xmlns“,无法使用XSLT转换XML

由于名称空间"xmlns“,无法使用XSLT转换XML
EN

Stack Overflow用户
提问于 2015-03-23 09:22:18
回答 1查看 2.8K关注 0票数 4

我一直试图使用XSLT转换XML文件,但由于一些问题,即"xmlns",它没有转换。我,为了我的生命找不到什么不对。

XML文件:

代码语言:javascript
复制
<?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文件:

代码语言:javascript
复制
<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>

预期输出:

代码语言:javascript
复制
<?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>

结果输出:

代码语言:javascript
复制
<?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文件的情况下对其进行转换,这同样有效。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-23 09:59:08

XML源中的元素位于xmlns="http://www.fpml.org/FpML-5/pretrade"命名空间中。必须在样式表中声明此命名空间,为其分配前缀,并在选择或匹配XML中的元素时使用该前缀。下面是一个最小化的示例:

XSLT1.0

代码语言:javascript
复制
<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>

当将此应用于输入示例时,结果将是:

代码语言:javascript
复制
<?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>

重要:

请注意同一名称空间的双重声明:

代码语言:javascript
复制
xmlns:fmpl="http://www.fpml.org/FpML-5/pretrade"
xmlns="http://www.fpml.org/FpML-5/pretrade"
  • 第一个声明,即分配fpml prefix的声明,旨在使您能够处理输入XML中的元素。
  • 第二个声明声明样式表本身的默认命名空间:写入样式表的任何文字元素(如示例中的<header> )都将放置在默认名称空间中。

两个名称空间是相同的(即具有相同的URI)这一事实可以说是巧合。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29206690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档