Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

xml - FpML - how to replace a text in the namespace after copy the whole fpml?

I am facing trouble when try to replace the namespace after copy the fpml. I need to replace

<nonpublicExecutionReport xmlns="http://www.fpml.org/FpML-5/transparency" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.fpml.org/FpML-5/transparency file:///C:/APPS/Resources/xmls/SDR/transparency/fpml-main-5-5.xsd" fpmlVersion="5-5">
    <A/>
    <B/>
</nonpublicExecutionReport>

for

<nonpublicExecutionReport fpmlVersion="5-5" xsi:schemaLocation="http://www.fpml.org/FpML-5/recordkeeping /../xmls/SDR/recordkeeping/fpml-main-5-5.xsd" xmlns="http://www.fpml.org/FpML-5/recordkeeping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <A/>
    <B/>
</nonpublicExecutionReport>

Basically is replace 'transparency' by 'recordkeeping'

I have tried follow the previous question like XML replacement with XSL but without success for my case.

What I do is:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" mlns:fpml="http://www.fpml.org/FpML-5/transparency">
    <!-- Copy XML source -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <!-- Remove transparency from the layout-->
    <xsl:template match="fpml:*">
        <!-- Update this tag -->
        <xsl:element name="{local-name()}">
            <nonpublicExecutionReport xmlns="http://www.fpml.org/FpML-5/reportkeeping" fpmlVersion="5-5" xsi:schemaLocation="http://www.fpml.org/FpML-5/recordkeeping/../xmls/SDR/recordkeeping/fpml-main-5-5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <xsl:apply-templates select="node()"/>
            </nonpublicExecutionReport>
        </xsl:element>
    </xsl:template>
    <xsl:template match="fpml:*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>  
</xsl:stylesheet>

The namespace isn't being replaced.

question from:https://stackoverflow.com/questions/65917561/fpml-how-to-replace-a-text-in-the-namespace-after-copy-the-whole-fpml

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have two templates matching the same pattern. This will either produce an error or only the last template will be applied.

To get the result shown, you could do simply:

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

<xsl:template match="fpml:*">
    <xsl:element name="{local-name()}" namespace="http://www.fpml.org/FpML-5/recordkeeping">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="node()"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...