XSLT:更改命名空间的值

时间:2012-08-09 21:23:48

标签: xml xslt xslt-1.0 xml-namespaces

我必须使用XSLT从一个XML转换为另一个XML。我的源文件和所需文件中有一些名称空间,除了更改xsi:schemaLocation的值并在adlcp:scormtype="sco"节点中添加<resource>属性外,我必须按原样保留所有名称空间。 / p>

我的输入文件:

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

的值

谢谢!

2 个答案:

答案 0 :(得分:2)

此转化

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

答案 1 :(得分:1)

在我看来,你忘了包含

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

命名空间。这应该是你得到错误的原因。