【问题标题】:Add additional namespace/schemalocations to XML file using XSLT使用 XSLT 向 XML 文件添加额外的命名空间/模式位置
【发布时间】:2011-02-01 22:29:39
【问题描述】:

我要转换:

<ppx xmlns="http://www.p.com/ppx/1" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.p.com/ppx/1 http://www.p.com/ppx/1/ppx.xsd">
<p></p></ppx>

进入:

<ppx xmlns="http://www.p.com/ppx/1" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:ppxx="http://www.m.com/mExt/v1" 
xmlns:ppxtpx="http://www.m.com/mExt/v3" 
xsi:schemaLocation="http://www.p.com/ppx/1 http://www.p.com/ppx/1/ppx.xsd 
http://www.m.com/mExt/v1 http://www.m.com/mExt/v1/ppxv1.xsd 
http://www.m.com/mExt/v3 http://www.m.com/mExt/v3/ppxv3.xsd">
<p></p></ppx>

我需要在现有 XML 文件中添加一些命名空间声明及其关联的 schemaLocations,而不更改该 XML 中的任何其他内容。

【问题讨论】:

    标签: xslt xslt-2.0 xslt-1.0


    【解决方案1】:

    原则上很简单:只需要​​一个标准的“修改后的身份模板”模式:

    <xsl:template match="*">
      <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="ppx">
    <ppx xmlns="http://www.p.com/ppx/1" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:ppxx="http://www.m.com/mExt/v1" 
      xmlns:ppxtpx="http://www.m.com/mExt/v3" 
      xsi:schemaLocation="http://www.p.com/ppx/1 http://www.p.com/ppx/1/ppx.xsd 
        http://www.m.com/mExt/v1 http://www.m.com/mExt/v1/ppxv1.xsd 
        http://www.m.com/mExt/v3 http://www.m.com/mExt/v3/ppxv3.xsd">
        <xsl:apply-templates/>
      </ppx>
    </xsl:template> 
    

    但是,根据输入与您向我们展示的示例的差异程度,它可能会变得更加复杂。例如,如果根元素并不总是被命名为ppx,或者如果要添加的命名空间事先不知道。所以你可能需要解释问题的更多细节

    【讨论】:

    • 那行得通 :-) 我做了一个小改动——第二个模板需要是
    猜你喜欢
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 2010-12-22
    • 2016-05-05
    • 2019-07-14
    相关资源
    最近更新 更多