简体   繁体   中英

XSLT: How to force namespace in output child nodes

I'm attempting to implement the OAI-PMH harvesting XML using XSLT 2.0 (I can up version to 3.0 if needs be) as stated in the OAI-PMH spec My problem is with getting the "xsi" namespace in BOTH the OAI-PMH tag and the metadata tag as the schema requires. Currently my code works a bit like this (I've removed a bunch of stuff for sake of brevity):

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

My output looks a bit like this:

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

Note that the

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

is missing from the oai_dc:dc tag. But based upon the OAI-PMH spec/schema, it needs to be there. Can anyone help me resolve this please?

I can tell you that you will get rid of all the xmlns="" by putting the xmlns="http://www.openarchives.org/OAI/2.0/" on the root of the stylesheet. As for the xmlns:xsi namespace declaration, I don't think you can enforce it with XSLT, it is in scope based on declaration on the ancestor, therefore the serializer does not need to add it to the descendant element.

The specification you refer to does indeed say: "every metadata part must include the attribute xmlns:xsi". I think the only sensible way one can interpret that is "A namespace binding for the prefix "xsi" to the namespace " http://www.w3.org/2001/XMLSchema-instance " must be in scope for every metadata part".

If the spec used a DTD to require namespace declarations to be physically present on particular elements (as some DTDs do) then you would have a problem. But I don't see any evidence that this spec uses DTDs. With an XSD schema, there is no way of saying that redundant namespace declarations must be present.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM