2012-08-09 108 views
1

我必須使用XSLT從一個XML轉換爲另一個XML。我的源文件中有一些命名空間,並且在所需的文件中,我必須保留所有這些命名空間,除了更改xsi:schemaLocation的值並在<resource>節點中添加adlcp:scormtype="sco"屬性。XSLT:更改名稱空間的值

我的輸入文件:

<?xml version="1.0" encoding="UTF-8"?> 
      <manifest identifier="eXescorm_quiz4823c6301f3d3afc1c1f" 
      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" 
      xsi:schemaLocation="http://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd"> 

    <resources> 
     <resource identifier="RES22" type="webcontent" href="index.html"> 
       <file href="index.html"/> 
       <file href="common.js"/> 
     </resource> 
    </resources> 
</manifest> 

所需的輸出:

<?xml version="1.0" encoding="UTF-8"?> 
    <manifest xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" 
       identifier="eXeorm_sample4823c6301f29a89a4c1f" 
       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" 
       xsi:schemalocation="http://www.imsproject.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd"> 

    <resources> 
     <resource identifier="RES22" type="webcontent" href="index.html" adlcp:scormtype="sco"> 
       <file href="index.html"/> 
       <file href="common.js"/> 
     </resource> 
    </resources> 
</manifest> 

我的XSLT(更新)

<xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" 
     xmlns:xhtml="http://www.imsglobal.org/xsd/imscp_v1p1" 
     xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" 
     exclude-result-prefixes="xhtml"> 
     <xsl:output method="html" indent="yes" encoding="UTF-8"/> 
     <xsl:strip-space elements="*" /> 

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

<xsl:template match="xhtml:resource[@type='webcontent']" 
    xmlns="http://www.w3.org/1999/xhtml"> 
    <resource adlcp:scormtype="sco"> 
    <xsl:apply-templates select=" 
     (@*[local-name()!='adlcp:scormtype']) 
     | node()"/> 
    </resource> 
</xsl:template> 

請幫我改變命名空間的價值xsi:schemalocation

謝謝!

回答

1

這種轉變

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:x="http://www.imsglobal.org/xsd/imscp_v1p1" 
xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="*"> 
    <xsl:element name="{name()}" namespace="{namespace-uri()}"> 
    <xsl:copy-of select="namespace::*[name()]"/> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="@*"> 
    <xsl:copy-of select="."/> 
</xsl:template> 

<xsl:template match="/*"> 
    <xsl:element name="{name()}" namespace="{namespace-uri()}"> 
    <xsl:copy-of select="namespace::*[name()]"/> 
    <xsl:apply-templates select="@*"/> 
    <xsl:attribute name="xsi:schemaLocation"> 
    <xsl:value-of select= 
    "'http://www.imsproject.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd'" 
    /> 
    </xsl:attribute> 
    <xsl:apply-templates select="node()"/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="x:resource"> 
    <xsl:element name="{name()}" namespace="{namespace-uri()}"> 
    <xsl:copy-of select="namespace::*[name()]"/> 
    <xsl:apply-templates select="@*"/> 
    <xsl:attribute name="adlcp:scormtype">sco</xsl:attribute> 
    <xsl:apply-templates select="node()"/> 
    </xsl:element> 
</xsl:template> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<manifest identifier="eXescorm_quiz4823c6301f3d3afc1c1f" 
       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" 
       xsi:schemaLocation="http://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd"> 

     <resources> 
      <resource identifier="RES22" type="webcontent" href="index.html"> 
        <file href="index.html"/> 
        <file href="common.js"/> 
      </resource> 
     </resources> 
</manifest> 

產生想要的,正確的結果:

<manifest 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" identifier="eXescorm_quiz4823c6301f3d3afc1c1f" xsi:schemaLocation="http://www.imsproject.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd"> 
    <resources> 
     <resource identifier="RES22" type="webcontent" href="index.html" adlcp:scormtype="sco"> 
     <file href="index.html"/> 
     <file href="common.js"/> 
     </resource> 
    </resources> 
</manifest> 
+0

非常感謝。這是完美的。 :) 結合我的兩個解決方案的不同問題後,我有一分鐘的問題,我將作爲一個新的問題發佈。再次感謝你的幫助。 :) – RahulD 2012-08-10 02:58:26

+0

@rahuldwivedi:不客氣。 – 2012-08-10 03:27:53

1

在我看來,你忘了,包括在你的XSLT的

xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" 

命名空間。這應該是你得到錯誤的原因。

+0

感謝您的回覆(+1)。我添加了它,現在我可以在資源標籤內添加adlcp:scormtype屬性,但在文件標籤中添加命名空間並返回,如 此外,我無法更改xsi:schemalocation命名空間的值。你能幫我解決嗎?謝謝。 – RahulD 2012-08-09 21:47:24