2012-12-07 36 views
1

我有一個xml文檔,其中一些元素有一個名稱空間,其他元素沒有。他們都需要命名空間,有些不同。元素具有我想要保留的屬性。向元素添加各種名稱空間

的XML:

<foo xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gco="http://www.isotc211.org/2005/gco" xsi:schemaLocation="http://www.isotc211.org/2005/gmd http://schemas.opengis.net/iso/19139/20060504/gmd/gmd.xsd" xmlns="http://www.isotc211.org/2005/gmd"> 
<bar y="2"> 
<baz z="3"/></bar> 
<a-special-element n="8"/> 
<another-special-element k="8"/> 
</foo> 

而XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="*"> 
    <xsl:element name="{local-name()}" namespace="A" > 
     <xsl:copy-of select="attribute::*"/> 
     <xsl:apply-templates /> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="foo"> 
    <xx:foo xmlns:xx="xx"> 
     <xsl:apply-templates/> 
    </xx:foo> 
</xsl:template> 

<xsl:template match="a-special-element"> 
    <B:a-special-element xmlns:B="B"> 
     <xsl:apply-templates/> 
    </B:a-special-element> 
</xsl:template> 

<xsl:template match="another-special-element"> 
    <C:a-special-element xmlns:C="C"> 
     <xsl:apply-templates/> 
    </C:another-special-element> 
</xsl:template> 

</xsl:stylesheet> 

這裏是輸出我想有:

<xx:foo xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gco="http://www.isotc211.org/2005/gco" xsi:schemaLocation="http://www.isotc211.org/2005/gmd http://schemas.opengis.net/iso/19139/20060504/gmd/gmd.xsd" xmlns="http://www.isotc211.org/2005/gmd"> <bar y="2"> 
    <baz z="3"/> 
</bar> 
<B:a-special-element n="8"/> 
<C:another-special-element k="4"/> 

</xx:foo> 

我檢查了這個線程,但那裏的「特殊要素」的財產已經被神奇地去除了。 Add a namespace to elements

我也有多個的xmlns:???在我想保留的foo中。

+0

沒有名稱空間「A」聲明。如果聲明不排除任何前綴(),則應輸出未使用的前綴。 – wst

回答

0

首先:命名空間是XML的一個基本概念。如果您不熟悉命名空間,請花時間學習並理解它們。

我有一個xml文檔,其中一些元素有一個命名空間,其他的沒有。

實際上,在您的代碼示例中,所有元素都屬於一個名稱空間。

代碼xmlns="http://www.isotc211.org/2005/gmd"在你的根元素定義了URI "http://www.isotc211.org/2005/gmd"默認命名空間。因此,如果元素名稱沒有名稱空間前綴,則此元素及其後代屬於此默認名稱空間。如果它使用默認名稱空間,因爲沒有名稱空間前綴,很容易忘記某個元素位於某個名稱空間中。請注意,默認名稱空間不適用於屬性,僅適用於元素。

您預期的結果文件不清晰。它還應該具有名稱空間前綴xxBC的名稱空間定義。我還假定你的命名空間A與你預期的結果文檔使用的默認命名空間相同。

無論如何,鑑於此輸入:

<foo xmlns:gml="http://www.opengis.net/gml" 
    xmlns:xlink="http://www.w3.org/1999/xlink" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:gco="http://www.isotc211.org/2005/gco" 
    xmlns="http://www.isotc211.org/2005/gmd" 
    xsi:schemaLocation="http://www.isotc211.org/2005/gmd http://schemas.opengis.net/iso/19139/20060504/gmd/gmd.xsd" 
    > 
    <bar y="2"> 
     <baz z="3"/> 
    </bar> 
    <a-special-element n="8"/> 
    <another-special-element k="8"/> 
</foo> 

這XSLT代碼:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:gml="http://www.opengis.net/gml" 
    xmlns:xlink="http://www.w3.org/1999/xlink" 
    xmlns:gco="http://www.isotc211.org/2005/gco" 
    xmlns:gmd="http://www.isotc211.org/2005/gmd" 
    xmlns="http://www.isotc211.org/2005/gmd" 
    xmlns:B="B" 
    xmlns:C="C" 
    xmlns:xx="xx" 
    > 

<xsl:template match="node() | @*" name="identity"> 
    <xsl:copy> 
     <xsl:apply-templates select="node() | @*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="*"> 
    <xsl:element name="{local-name()}"> 
     <xsl:apply-templates select="node() | @*"/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="gmd:foo"> 
    <xx:foo> 
     <xsl:apply-templates select="node() | @*"/> 
    </xx:foo> 
</xsl:template> 

<xsl:template match="gmd:a-special-element"> 
    <B:a-special-element> 
     <xsl:apply-templates select="node() | @*"/> 
    </B:a-special-element> 
</xsl:template> 

<xsl:template match="gmd:another-special-element"> 
    <C:another-special-element> 
     <xsl:apply-templates select="node() | @*"/> 
    </C:another-special-element> 
</xsl:template> 

</xsl:stylesheet> 

會產生這樣的輸出:

<xx:foo xmlns:xx="xx" 
     xmlns:gml="http://www.opengis.net/gml" 
     xmlns:xlink="http://www.w3.org/1999/xlink" 
     xmlns:gco="http://www.isotc211.org/2005/gco" 
     xmlns:gmd="http://www.isotc211.org/2005/gmd" 
     xmlns="http://www.isotc211.org/2005/gmd" 
     xmlns:B="B" 
     xmlns:C="C" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.isotc211.org/2005/gmd http://schemas.opengis.net/iso/19139/20060504/gmd/gmd.xsd" 
     > 
    <bar y="2"> 
     <baz z="3"/> 
    </bar> 
    <B:a-special-element n="8"/> 
    <C:another-special-element k="8"/> 
</xx:foo> 

請注意身份模板如何用於遞歸複製輸入文檔的內容,這是一種常用且有用的技術。

請注意,XSLT文檔的默認名稱空間不適用於<xsl:template match="ELEMENT-NAME">中使用的元素名稱。因此,你需要定義爲輸入文檔的默認命名空間的命名空間前綴,當你指的這些元素,例如在matchselect屬性使用的前綴。還請注意,因此,名稱空間URI http://www.isotc211.org/2005/gmd在結果文檔中定義了兩次 - 前綴gmd並作爲默認名稱空間。如果這使您困擾,您可以將exclude-result-prefixes="gmd"屬性添加到<xsl:stylesheet>元素。作爲副作用,這可能會導致默認名稱空間定義出現在文檔的後面,而不是在根元素中,但這只是一種視覺差異,底層功能保持不變。

+0

感謝您的回覆!然而,我被叫了幾天,現在不能嘗試。但我會保持你的發佈。 – Wilbert

+0

所以,現在我回來了,我有時間來審查你的答案。我讀過W3C在他們的網站上關於Namspaces的信息,它真的幫助我們指出了這一點。我現在知道默認名稱空間和「set」namspaces之間的區別。我想我一直在尋找解決我的問題的方法錯誤的地方。 – Wilbert

+0

問題是,數據獲取的HTML顯示在網站上。使用默認命名空間的xml輸入創建的Html與使用「set」namspaces創建的xml不同。既然w3c不說他們之間有真正的區別,我想這不是我應該擔心的命名空間。這是xslt – Wilbert

相關問題