2011-02-01 38 views
0

添加额外的命名空间/ schemalocations到XML文件,我想转换:使用XSLT

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

我需要一些空间声明及其关联schemaLocations添加到现有的XML文件而不改变该XML中的其他内容。

回答

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,或者如果要预先添加名称空间未知。所以你可能需要解释一下这个问题的更多细节

+0

工作原理:-)我做了一个小改动 - 第二个模板需要是。 – xmllist 2011-02-02 00:52:21