由于命名空间“xmlns",无法使用 XSLT 转换 XML [英] Cannot Transform XML using XSLT due to Namespaces "xmlns"

查看:28
本文介绍了由于命名空间“xmlns",无法使用 XSLT 转换 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 XSLT 转换 XML 文件,但由于一些问题,即xmlns",它没有转换.我,这辈子都查不出哪里出了问题.

I have been trying to transform an XML file using XSLT but due to some problem, namely "xmlns", It is not transforming. I, for the life of me can't find what is wrong.

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 文件:

This is the XSLT File:

<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" 时才会出现预期的输出

The expected output only comes when I remove xmlns="http://www.fpml.org/FpML-5/pretrade"

如果在 XML 文件中,我将标签添加到xmlns",例如 xmlns:AAA="http://www.fpml.org/FpML-5/pretrade",这也有效但是我必须在不编辑 XML 文件的情况下进行转换.

This also works if, In the XML file, I add a tag to "xmlns" Eg xmlns:AAA="http://www.fpml.org/FpML-5/pretrade" But I have to transform this without editing the XML file.

推荐答案

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

The elements in your XML source are in the xmlns="http://www.fpml.org/FpML-5/pretrade" namespace. You must declare this namespace in your stylesheet, assign it a prefix, and use that prefix when selecting or matching the elements in your XML. Here's a minimized example:

XSLT 1.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>

当这应用于您的输入示例时,结果将是:

When this is applied to your input example, the result will be:

<?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>

重要提示:

注意相同命名空间的双重声明:

Note the double declaration of the same namespace:

xmlns:fmpl="http://www.fpml.org/FpML-5/pretrade"
xmlns="http://www.fpml.org/FpML-5/pretrade"

  • 第一个声明,即分配fpml前缀的声明是旨在使您能够处理输入 XML 中的元素.

    • The first declaration, the one that assigns the fpml prefix, is intended to enable you to address the elements in the input XML.

      第二个声明为样式表本身:您写入样式表的任何文字元素 - 例如示例中的

      - 将被放置到默认命名空间中.

      The second declaration declares a default namespace for the stylesheet itself: any literal element you write into the stylesheet - such as the <header> in the example - will be placed into the default namespace.

      两个命名空间相同(即具有相同的 URI)的事实可以说是巧合.

      The fact that two namespaces are the same (i.e. have the same URI) could be said to be a coincidence.

      这篇关于由于命名空间“xmlns",无法使用 XSLT 转换 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆