加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – XSLT:添加了前缀和命名空间

发布时间:2020-12-16 22:57:28 所属栏目:百科 来源:网络整理
导读:我正在使用XSLT从一个 XML转换为另一个 XML.通过应用论坛中给出的答案,我能够实现我对所需输出的所有要求,但唯一的问题是在输出中一个额外的前缀ns0会自动添加到两个位置并且命名空间xmlns =“http:// www.imsglobal.org/xsd/imscp_v1p1“添加在每个节点的
我正在使用XSLT从一个 XML转换为另一个 XML.通过应用论坛中给出的答案,我能够实现我对所需输出的所有要求,但唯一的问题是在输出中一个额外的前缀ns0会自动添加到两个位置并且命名空间xmlns =“http:// www.imsglobal.org/xsd/imscp_v1p1“添加在每个节点的开头.

输入文件

<?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"/>
                 <file href="new1.js"/>
                 <file href="new2.js"/>
         </resource>
    </resources>   
</manifest>

我的XSLT:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ims="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"
  exclude-result-prefixes="xsl ims adlcp xsi">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />

<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="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="ims:resource" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1">
 <xsl:copy>
  <xsl:apply-templates select="@*"/>
  <xsl:attribute name="adlcp:scormtype">sco</xsl:attribute>
  <xsl:apply-templates select="node()"/>
  <file href="new1.js"/>
  <file href="new2.js"/>   
 </xsl:copy>
</xsl:template>
</xsl:stylesheet>

我得到的输出:
代替

<manifest xmlns="http://www.imsglobal.org/xsd/imscp_v1p1">

我明白了

<ns0:manifest xmlns:ns0="http://www.imsglobal.org/xsd/imscp_v1p1" >

而不是

<resources>

我明白了

<resources xmlns="http://www.imsglobal.org/xsd/imscp_v1p1">`

(并且这个xmlns也被添加到其他一些节点的开头),而其余的东西都很好.

谢谢!

解决方法

无法重现问题!

我在提供的XML文档上运行提供的转换,我在计算机上使用了所有11个XSLT处理器,并且所有处理器都生成了相同的,想要的,正确的结果.

无论如何,为什么不尝试这种稍微修改过的转换,这也会产生想要的结果?:

<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:ims="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"
      xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"
      exclude-result-prefixes="xsl ims adlcp xsi">
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*" />

    <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="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="ims:resource">
     <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:attribute name="adlcp:scormtype">sco</xsl:attribute>
      <xsl:apply-templates select="node()"/>
      <file href="new1.js" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"/>
      <file href="new2.js" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"/>
     </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读