【问题标题】:XSLT: Prefix and namespace get addedXSLT:添加了前缀和命名空间
【发布时间】:2012-08-10 03:45:16
【问题描述】:

我正在使用 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 xslt xslt-1.0 prefix


    【解决方案1】:

    无法重现问题!

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

    不管怎样,你为什么不试试这个稍加修改的转换,它也能产生想要的结果?:

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

    【讨论】:

    • 感谢您的回答。我不知道我的处理器有什么问题。它仍然产生相同的结果。我正在使用 JAXP 程序通过将 .xml 和 .xsl 文件作为输入来生成输出。那么这个问题也会因为那个程序而发生吗?谢谢!
    • @rahuldwivedi,我不知道。尝试将顶部元素指定为文字结果元素。您使用的是什么 XSLT 处理器?也许是时候摆脱它了……
    • 我刚刚测试了生成的输出,即使有这些额外的前缀,它也没有在我的项目中产生任何问题,所以我可以负担得起这个输出。我使用 SAXON,你能建议我什么是更好的处理器吗?谢谢。
    • @rahuldwivedi,这很奇怪,Saxon 是我无法重现您的结果的处理器之一。很可能您使用的是非常旧的版本,或者您没有向我们提供您所拥有的确切代码。
    【解决方案2】:

    只需做这个小改动:

    • 将样式表的默认命名空间设置为...

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

    【讨论】:

    • 感谢您的回答,我已经添加了这个默认命名空间,但仍然产生相同的结果。我无法找出问题所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 2014-08-17
    • 2018-04-30
    • 1970-01-01
    相关资源
    最近更新 更多