0

I want to convert:

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

into:

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

I need to add a few namespace declarations and their associated schemaLocations to an existing XML file without changing anything else in that XML.

4

1 回答 1

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,或者如果要添加的命名空间事先不知道。所以你可能需要解释问题的更多细节

于 2011-02-01T23:57:27.963 回答