我有下面的XML文檔根元素更換多個命名空間XSLT
<a:rootElement xmlns:a="http://a/1" xmlns:b="http://b/1" xmlns:c="http://c/1">
<child1 type="b:type"/>
<child2 type="c:type"/>
</a:rootElement>
現在我想改變命名空間的URIs,所以我得到以下結果
<a:rootElement xmlns:a="http://a/2" xmlns:b="http://b/2" xmlns:c="http://c/2">
<child1 type="b:type"/>
<child2 type="c:type"/>
</a:rootElement>
不應該有其他更改。我用下面的樣式表試了一下。
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:a="http://a/2"
xmlns:b="http://b/2"
xmlns:c="http://c/2" >
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:copy-of select="document('')/*/namespace::*[name()='a']"/>
<xsl:copy-of select="document('')/*/namespace::*[name()='b']"/>
<xsl:copy-of select="document('')/*/namespace::*[name()='c']"/>
<xsl:copy-of select="node()|@*"/>
</xsl:element>
</xsl:template>
我碰到下面的錯誤輸出。
<a_0:rootElement xmlns:a_0="http://a/1" xmlns:a="http://a/2" xmlns:b="http://b/2" xmlns:c="http://c/2">
<child1 type="b:type" xmlns:a="http://a/1" xmlns:b="http://b/1" xmlns:c="http://c/1"/>
<child2 type="c:type" xmlns:a="http://a/1" xmlns:b="http://b/1" xmlns:c="http://c/1"/>
</a_0:rootElement>
我也嘗試了其他一些方法,但也沒有所需的輸出。用XSLT以這種方式更改名稱空間是否可能?
感謝您的任何意見
是不是有一個原因,你不這樣做在第一行用一個簡單的文本替換?看起來這比嘗試使用XSLT做起來要容易得多。 – Flynn1179 2012-01-17 14:40:03
我必須在XML結構中改變更多,但這是我無法解決的唯一任務。轉型是模型升級的一部分。 – 2012-01-17 14:52:08
你不能兩個都做?用文本替換來更改名稱空間,然後應用XSLT來完成剩下的工作?鑑於您正在有效地更改文檔中大多數(如果不是全部)元素的名稱空間,將其全部集成在一起的XSLT可能會變得更加複雜。 – Flynn1179 2012-01-17 14:54:55