2012-07-06 161 views
0

我是XSLT的新手,我試圖將一個XML文件轉換爲另一個XML文件。我的問題是有一個命名空間'xmlns'在原始xml文件中沒有任何前綴,當我通過xslt將它轉換爲另一個時,則沒有任何反應,但是如果我刪除了那個xmlns命名空間,那麼它可以解決,但實際上我無法修改原始.xml文件,因爲我只能使用該文件,所以我必須將xmlns保留在原始文件中。所以任何人都可以在我的.xsl或java代碼中建議一些修改來克服這個問題。XSLT和Java:默認名稱空間XMLNS

我原來的XML命名空間看起來喜歡 -

<?xml version="1.0" encoding="UTF-8"?> 
     <manifest identifier="eXeorm_sample4823c6301f29a89a4c1f" 
     xmlns:ims="http://www.imsglobal.org/xsd/imscp_v1p1" 
     xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" 
     xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" 
     xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
</manifest> 

我想要的XML是:

<?xml version="1.0" encoding="UTF-8"?> 
     <manifest identifier="eXescorm_quiz4823c6301f29a8419515" 
      xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2" 
      xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" 
      xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     </manifest> 

我修改XSLT-

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2" 
xmlns:ims="http://www.imsglobal.org/xsd/imscp_v1p1" 
xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" 
xmlns:imsmd="http://www.imsglobal.org/xsd/imsmd_v1p2" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:template match="ims:manifest"> 
</xsl:stylesheet> 

回答

1

在此之上有一個搜索框頁面:鍵入「XSLT默認命名空間」,您會發現數百個這個問題的答案。

順便說一句,您的代碼非常冗長。取而代之的是:

<xsl:element name="item"> 
    <xsl:attribute name="identifier">ITEM-eXeorm_sample4823c6301f29a89a4d27</xsl:attribute> 
    <xsl:attribute name="isvisible">true</xsl:attribute> 
    <xsl:attribute name="identifierref">RES-eXeorm_sample4823c6301f29a89a4d28</xsl:attribute> 
</xsl:element> 

使用本:

<item identifier="ITEM-eXeorm_sample4823c6301f29a89a4d27" invisible="true" 
     identifierref="RES-eXeorm_sample4823c6301f29a89a4d28"/> 
+0

非常感謝您的答覆和建議。在發佈問題之前,我已經通過了關於命名空間前綴的一些線索,但那些問題沒有解決。我會根據你的建議嘗試並回復你。感謝您! – RahulD 2012-07-06 15:23:58

+0

@MichaelKey我根據您的建議修改了我的XSLT,並且一些解決方案提到了其他線程。現在我的第二個問題解決了,我沒有得到adlcp相關的錯誤,但第一個問題仍然保持原樣。它不適用於默認名稱空間。在我的情況下,默認命名空間的值在兩個XML文件中都不同,並且它在清單標記下列出。請建議我對該部分進行必要的修改,如果可能的話,請在我的代碼中編輯該xmlns部分。 感謝你! – RahulD 2012-07-07 06:04:43

+0

Dr.Michael我能夠通過在原始XML文件中保留默認命名空間來生成輸出,但輸出與我想要的不完全相同。清單標籤下的名稱空間的聲明區域看起來不同,並且某些名稱空間(如xmlns:adlcp =「http://www.adlnet.org/xsd/adlcp_rootv1p2」)會自動添加到其他標記中,這些標記不是必需的。請建議我進行修改。 感謝你! – RahulD 2012-07-07 22:52:15

相關問題