如何使用XSLT crosswalk更改文档中间的命名空间

时间:2016-02-07 04:55:41

标签: xml xslt

我需要创建一个XML文档,其中包含一组其他XML文档。它基本上应该是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<OAI-PMH 
  xmlns="http://www.openarchives.org/OAI/2.0/" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">    

  <ListRecords>
    <record>
        <metadata>
            <registryObjects 
              xmlns="http://ands.org.au/standards/rif-cs/registryObjects" 
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
              xsi:schemaLocation="http://ands.org.au/standards/rif-cs/registryObjects http://services.ands.org.au/documentation/rifcs/schema/registryObjects.xsd">

             <registryObject> 
             ...
             </registryObject>
           </registryObjects>
         </metadata>
       </record>
     </ListRecords>
   </OAI-PMH>

我发现我可以使用xsl:element:

来更改命名空间
<xsl:element name="registryObjects" namespace="http://ands.org.au/standards/rif-cs/registryObjects">

但是,它只更改了此特定元素的命名空间。下一个又变成了“http://www.openarchives.org/OAI/2.0/”:

<metadata>
    <registryObjects xmlns="http://ands.org.au/standards/rif-cs/registryObjects" xsi:schemaLocation="http://ands.org.au/standards/rif-cs/registryObjects http://services.ands.org.au/documentation/rifcs/schema/registryObjects.xsd">
      <registryObject xmlns="http://www.openarchives.org/OAI/2.0/" group="NII">

我的代码如下所示:

 <xsl:template match="oai:record">
    <record>
       <xsl:element name="registryObjects" namespace="http://ands.org.au/standards/rif-cs/registryObjects">
       <xsl:attribute name="xsi:schemaLocation">
        <xsl:text>http://ands.org.au/standards/rif-cs/registryObjects http://services.ands.org.au/documentation/rifcs/schema/registryObjects.xsd</xsl:text>
      </xsl:attribute>
         <registryObject>

是否可以在registryObjects之后更改所有元素的命名空间?

1 个答案:

答案 0 :(得分:1)

当你这样做时:

<xsl:element name="registryObjects" namespace="http://ands.org.au/standards/rif-cs/registryObjects">

您将registryObjects放在指定的命名空间中 - 但您没有更改默认命名空间

因此,后代元素(示例中为registryObject)继承默认命名空间当前在范围 - 根据您的结果,恰好是{ {1}}(您还没有向我们展示完整的样式表)。

这里的简单解决方案是使用文字结果元素。而不是:

"http://www.openarchives.org/OAI/2.0/"

做的:

<xsl:element name="registryObjects" namespace="http://ands.org.au/standards/rif-cs/registryObjects">
       <xsl:attribute name="xsi:schemaLocation">
        <xsl:text>http://ands.org.au/standards/rif-cs/registryObjects http://services.ands.org.au/documentation/rifcs/schema/registryObjects.xsd</xsl:text>
      </xsl:attribute>
         <registryObject>

这不仅会将<registryObjects xmlns="http://ands.org.au/standards/rif-cs/registryObjects" xsi:schemaLocation="http://ands.org.au/standards/rif-cs/registryObjects http://services.ands.org.au/documentation/rifcs/schema/registryObjects.xsd"> <registryObject> 放在指定的命名空间中,还会更改默认命名空间,以便registryObjects继承它。

在不更改默认命名空间的情况下,您必须将registryObject放置在指定的命名空间显式中,例如:

registryObject

或:

<registryObject xmlns="http://ands.org.au/standards/rif-cs/registryObjects">

值得注意的是,在您向我们展示的预期结果文档中,所有元素都在<xsl:element name="registryObject" namespace="http://ands.org.au/standards/rif-cs/registryObjects"> 命名空间中(由根"http://www.openarchives.org/OAI/2.0/"元素默认建立) ) - 除了 OAI-PMHregistryObjects,它们位于自己的registryObject命名空间中(由"http://ands.org.au/standards/rif-cs/registryObjects"元素默认设置)。

相关问题