使用XSL替换XML

时间:2010-12-06 15:36:13

标签: xml string xslt replace fpml

您好我正在尝试将FpML 4的XML文件转换为FpML 5。

我唯一要改变的是FpML标头 以下是一个例子:

输入文件FpML 4

     <FpML version="4-0" xsi:type="DataDocument" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.fpml.org/2003/FpML-4-0 ../fpml-main-4-0.xsd" xmlns="http://www.fpml.org/2003/FpML-4-0">
            <trade>...</trade>
            <party id="partyA">...</party>
            <party id="partyB">...</party>
     </FpML>

现在生成的文件应该如下所示:

     <dataDocument xmlns="http://www.fpml.org/FpML-5/confirmation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" fpmlVersion="5-0" xsi:schemaLocation="http://www.fpml.org/FpML-5/confirmation ../../fpml-main-5-0.xsd">
            <trade>...</trade>
            <party id="partyA">...</party>
            <party id="partyB">...</party>
     </dataDocument>

我尝试过使用XSL教程,但没有任何帮助。 任何想法都会受到欢迎。

@Update:

现在只是看它正在运行我试过这个XSL

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="node() | @*">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="FpML">
  <xsl:element name="test">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

由于

2 个答案:

答案 0 :(得分:2)

此样式表:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:fpml4="http://www.fpml.org/2003/FpML-4-0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.fpml.org/FpML-5/confirmation"
 exclude-result-prefixes="fpml4">
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="fpml4:FpML">
        <dataDocument fpmlVersion="5-0"
                      xsi:schemaLocation=
         "http://www.fpml.org/FpML-5/confirmation ../../fpml-main-5-0.xsd">
            <xsl:apply-templates select="node()"/>
        </dataDocument>
    </xsl:template>
    <xsl:template match="fpml4:*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

输出:

<dataDocument fpmlVersion="5-0" 
 xsi:schemaLocation="http://www.fpml.org/FpML-5/confirmation ../../fpml-main-5-0.xsd" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns="http://www.fpml.org/FpML-5/confirmation">
    <trade>...</trade>
    <party id="partyA">...</party>
    <party id="partyB">...</party>
</dataDocument>

修改:使用默认命名空间更好......

答案 1 :(得分:1)

以下是一个示例样式表,用于更改您要求的输入样本:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fpml4="http://www.fpml.org/2003/FpML-4-0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.fpml.org/FpML-5/confirmation"
  exclude-result-prefixes="fpml4"
  version="1.0">

  <xsl:template match="fpml4:*">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="fpml4:FpML">
    <dataDocument fpmlVersion="5-0" xsi:schemaLocation="http://www.fpml.org/FpML-5/confirmation ../../fpml-main-5-0.xsd">
      <xsl:apply-templates/>
    </dataDocument>
  </xsl:template>

  <xsl:template match="@* | text() | comment() | processing-instruction()">
    <xsl:copy/>
  </xsl:template>

</xsl:stylesheet>

这种简单的转换是否足以满足我根本没有检查的模式。