简体   繁体   English

用XSL替换XML

[英]XML replacement with XSL

Hi I am trying to convert an XML file of FpML 4 to FpML 5. 嗨,我正在尝试将FpML 4的XML文件转换为FpML 5。

The only thing I have to change is the FpML header Here follows an example: 我唯一需要更改的是FpML标头,下面是一个示例:

input file FpML 4 输入文件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>

Now the resulting file should look like: 现在生成的文件应如下所示:

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

I tried with XSL tutorials around and nothing really helped. 我尝试了XSL教程,但没有任何帮助。 Any ideas anyone would be welcome. 任何想法,任何人都将受到欢迎。

@Update: @更新:

For now just to see it's working I tried this XSL 现在,只是为了查看它是否有效,我尝试了此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>

Thanks 谢谢

This stylesheet: 此样式表:

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

Output: 输出:

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

Edit : Better with a default namespace... 编辑 :更好的默认名称空间...

Here is a sample stylesheet that does the change of the input sample you asked for: 这是一个示例样式表,它可以更改您要求的输入示例:

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

Whether such a simple transformation is sufficient to satisfy the schema I have not checked at all. 这样简单的转换是否足以满足我从未检查过的模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM