XSLT:如何在输出子节点中强制命名空间

时间:2015-09-30 16:05:02

标签: xml xslt xslt-2.0

我尝试使用XSLT 2.0实现OAI-PMH收集XML(如果需要,我可以将版本升级到3.0),如OAI-PMH spec中所述 我的问题是获得" xsi"命名空间同时包含 OAI-PMH 标记和元数据标记,如模式所需。 目前我的代码有点像这样(为了简洁,我删除了一堆东西):

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
exclude-result-prefixes="xs" version="2.0"
>

<xsl:output indent="no" encoding="UTF-8" method="xml" omit-xml-

 declaration="no" media-type="application/xml;charset=UTF-8"/>

<xsl:template match="/">
    <xsl:apply-templates select="response" />        
</xsl:template>

<xsl:template match="response">
    <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">
       <xsl:apply-templates select="doc"/>    
    </OAI-PMH>       
</xsl:template>

<xsl:template match="doc">
    <record>
        <header>
        </header>
         <metadata>
             <oai_dc:dc xmlns:dc="http://purl.org/dc/elements/1.1/" 
              xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
              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/oai_dc/ 
              http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
              <xsl:text>call some templates</xsltext>
      </metadata> 
    </record>
</xsl:template>

我的输出看起来有点像这样:

<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">
<responseDate xmlns="">2015-09-30T16:47:19Z</responseDate>
<request xmlns="" verb="ListRecords" metadataPrefix="oai_dc" set="null" from="null" until="null">http://manchester.ac.uk/escholar/api/oai2</request>
<ListRecords xmlns="">
    <record>
        <header>
            <identifier>oai:escholar.manchester.ac.uk:uk-ac-man-scw-1964</identifier>
            2010-12-02T15:44:59.733Z
        </header>
        <metadata>
            <oai_dc:dc xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns="http://www.openarchives.org/OAI/2.0/" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
            </oai_dc:dc>
        </metadata>
    </record>
</ListRecords>
</OAI-PMH>

请注意

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
oai_dc:dc标记中缺少

。但基于OAI-PMH规范/架构,它需要存在。有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

我可以告诉你,通过将xmlns=""放在样式表的根目录上,你将摆脱所有xmlns="http://www.openarchives.org/OAI/2.0/"。至于xmlns:xsi命名空间声明,我认为你不能用XSLT强制它,它在范围内基于祖先的声明,因此序列化器不需要将它添加到后代元素。

答案 1 :(得分:0)

您所引用的规范确实说:&#34;每个元数据部分必须包含属性xmlns:xsi&#34;。我认为唯一合理的解释方式是&#34;前缀的命名空间绑定&#34; xsi&#34;到命名空间&#34; http://www.w3.org/2001/XMLSchema-instance&#34;必须在每个元数据部分的范围内&#34;。

如果规范使用DTD要求命名空间声明实际存在于特定元素上(如某些DTD那样)那么您就会遇到问题。但我没有看到任何证据证明此规范使用DTD。使用XSD架构,无法说明必须存在冗余的命名空间声明。