0

I have XML that I'm trying to transform using XSLT.

If I eliminate the namespace info from the XSLT & XML, it works fine, but I can't get it to work with the namespace info in place.

Here's what the failing XSLT looks like:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:dc="http://purl.org/dc/elements/1.1/"
                              xmlns:dcterms="http://purl.org/dc/terms/">

<xsl:template match="node()|@*">
  <full>
    <xsl:apply-templates select="//dc"/>
  </full>
</xsl:template>


<xsl:template match="dc">
  <record>
    <xsl:apply-templates select="dcterms:title"/>
    <xsl:apply-templates select="dcterms:type"/>
  </record>
</xsl:template>

<xsl:template match="dcterms:title">
  <xsl:element name="dcterms:title"><xsl:value-of select="."/></xsl:element>
</xsl:template>

<xsl:template match="dcterms:type">
  <xsl:element name="dcterms:type"><xsl:value-of select="."/></xsl:element>
</xsl:template>


</xsl:stylesheet>

Here's the XML it's running against:

<?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">
  <responseDate>2015-10-11T00:35:52Z</responseDate>
  <ListRecords>
    <record>
      <metadata>
        <dc xmlns="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:edm="http://www.europeana.eu/schemas/edm/" xmlns:oai-pmh="http://www.openarchives.org/OAI/2.0/" xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:oai_qdc="http://worldcat.org/xmlschemas/qdc-1.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 http://worldcat.org/xmlschemas/qdc-1.0/ http://worldcat.org/xmlschemas/qdc/1.0/qdc-1.0.xsd http://purl.org/net/oclcterms http://worldcat.org/xmlschemas/oclcterms/1.4/oclcterms-1.4.xsd">
          <edm:dataProvider>Some University</edm:dataProvider>
          <edm:rights>https://library.someplace.edu/statements/rights</edm:rights>
          <dcterms:title>This is a title</dcterms:title>
          <dcterms:type>Image</dcterms:type>
          <dcterms:creator>This is a creator</dcterms:creator>
          <dc:date>1981-07-17</dc:date>
          <dc:format/>
          <dc:format/>
        </dc>
      </metadata>
    </record>
    <record>
      <metadata>
        <dc xmlns="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:edm="http://www.europeana.eu/schemas/edm/" xmlns:oai-pmh="http://www.openarchives.org/OAI/2.0/" xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:oai_qdc="http://worldcat.org/xmlschemas/qdc-1.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 http://worldcat.org/xmlschemas/qdc-1.0/ http://worldcat.org/xmlschemas/qdc/1.0/qdc-1.0.xsd http://purl.org/net/oclcterms http://worldcat.org/xmlschemas/oclcterms/1.4/oclcterms-1.4.xsd">
          <edm:dataProvider>Some University</edm:dataProvider>
          <edm:rights>https://library.someplace.edu/statements/rights</edm:rights>
          <dcterms:title>This is another title</dcterms:title>
          <dcterms:type>Image</dcterms:type>
          <dcterms:creator>This is a creator</dcterms:creator>
          <dc:date>1981-07-24</dc:date>
          <dc:format/>
          <dc:format/>
        </dc>
      </metadata>
    </record>
  </ListRecords>
</OAI-PMH>

The failing output looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<full xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/"/>

And this is what I am hoping to get:

<?xml version="1.0" encoding="UTF-8"?>
<full>
  <record>
    <title>This is a title</title>
    <type>Image</type>
  </record>
  <record>
    <title>This is another title</title>
    <type>Image</type>
  </record>
</full>

The absence of the "record" elements clearly show that the "dc" isn't being matched, but I'm not sure what I need to do to match these elements.

I'm using Saxon on a CentOS box, if that matters.

Saxon's throwing the message "Cannot find CatalogManager.properties" regardless of whether it's failing (with the namespace-enabled code) or working (with the namespace-less code). I suspect that's unrelated based on what I've seen thus far on the 'net, but can't swear to that.

I'm assuming this is something simple, but as a new XSLT user working with an XML document that's chock full of namespaces (more than listed in my sample, above) I'm getting a bit confused by the spaghetti the namespaces create.

Peter
  • 53
  • 6
  • Your stylesheet is attempting to select and match elements whose expanded name is "dc" *in no namespace*. Your input document has no such elements, but it does have elements with expanded name "dc" in namespace `http://www.openarchives.org/OAI/2.0/oai_dc/`. – John Bollinger Sep 28 '17 at 21:30
  • A search for "XSLT default namespace" will give you 612 identical questions. Someone asks this nearly every day. – Michael Kay Sep 28 '17 at 23:40

1 Answers1

1

The expected output can be achieved quite simply by:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:oai="http://www.openarchives.org/OAI/2.0/"
xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
xmlns:dcterms="http://purl.org/dc/terms/"
exclude-result-prefixes="oai oai_dc dcterms">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/oai:OAI-PMH">
    <full>
        <xsl:for-each select="oai:ListRecords/oai:record">
            <record>
                <title>
                    <xsl:value-of select="oai:metadata/oai_dc:dc/dcterms:title" />
                </title>
                <type>
                    <xsl:value-of select="oai:metadata/oai_dc:dc/dcterms:type" />
                </type>
            </record>
        </xsl:for-each>     
    </full>
</xsl:template>

</xsl:stylesheet>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51