ghukill
ghukill

Reputation: 1222

XSLT not matching element - namespace declarations

I'm sure this is a very simple fix, but I'm stumped. I've got input XML with the following root element, and repeating child elements:

<modsCollection 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.loc.gov/mods/v3"
    xsi:schemaLocation="
      http://www.loc.gov/mods/v3
      http://www.loc.gov/standards/mods/v3/mods-3-4.xsd">
  <mods version="3.4">
            ...

I've got an XSLT sheet with the following to match each <mods> node, and kick it out as a separate file named by an <identifier type="local"> element.

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.loc.gov/mods/v3">
  <xsl:output method="xml" indent="yes"/>   

  <xsl:template match="/modsCollection">
    <xsl:for-each select="mods">
      <xsl:variable name="filename" 
          select="concat(normalize-space(
                           identifier[@type='local']),
                         '.xml')" />
      <xsl:result-document href="{$filename}">            
        <xsl:copy-of select="."></xsl:copy-of>     
      </xsl:result-document>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

This works if the XML input does not have the xmlns:xsi, xmlns, or xsi:schemaLoaction attributes in the root element. So, for example, it works on the following:

<modsCollection>   
  <mods version="3.4">
            ...

I know that some of our MODS files have had the prefix included but I'm unclear why this won't work without the prefix if our XSLT matching is not looking for the prefix. Any thoughts or advice would be greatly appreciated.

Upvotes: 1

Views: 3404

Answers (1)

David Carlisle
David Carlisle

Reputation: 5652

<xsl:template match="/modsCollection">

matches modsCollection in no namespace. You want

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.loc.gov/mods/v3"
    xmlns:m="http://www.loc.gov/mods/v3">

then

<xsl:template match="/m:modsCollection">

To match modsCollection in the mods namespace, and similarly use the m: prefix in all xslt patterns and xpath expressions in the stylesheet.

Upvotes: 3

Related Questions