Getting unwanted xmlns prefix in transformation output

Here should go questions about transforming XML with XSLT and FOP.
rvanderh
Posts: 4
Joined: Thu Aug 02, 2018 8:48 pm

Getting unwanted xmlns prefix in transformation output

Post by rvanderh »

I'm using oXygen XML editor 20.1. We're downloading XML records from a remote site and transforming to a different XML schema. The transformed records then need to be uploaded to a different website (crossref.org). My stylesheet needs to be adjusted so that the namespace elements are correct in the root element (doi_batch).

Here is a sample XML record that we're starting with:

Code: Select all

<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>2018-03-26T13:03:14Z</responseDate>
<request verb="GetRecord" identifier="oai:escholarship.umassmed.edu:jeslib-1091" metadataPrefix="dcq">https://escholarship.umassmed.edu/do/oai/</request>
<GetRecord>
<record>
<header>
<identifier>oai:escholarship.umassmed.edu:jeslib-1091</identifier>
<datestamp>2016-07-19T12:50:14Z</datestamp>
<setSpec>publication:library</setSpec>
<setSpec>publication:jeslib</setSpec>
</header>
<metadata>
<oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bepress.com/OAI/2.0/qualified-dublin-core/ https://resources.bepress.com/assets/xsd/oai_qualified_dc.xsd">
<dc:title>
Discovery and Reuse of Open Datasets: An Exploratory Study
</dc:title>
<dc:creator>Mannheimer, Sara</dc:creator>
<dc:creator>Sterman, Leila Belle</dc:creator>
<dc:creator>Borda, Susan</dc:creator>
<dc:date.created>2016-07-19T07:00:00Z</dc:date.created>
<dc:type>Full-Length Paper</dc:type>
<dc:identifier.doi>10.7191/jeslib.2016.1091</dc:identifier.doi>
<dc:identifier>
https://escholarship.umassmed.edu/jeslib/vol5/iss1/5
</dc:identifier>
<dc:description>1</dc:description>
</oai_dc:dc>
</metadata>
</record>
</GetRecord>
</OAI-PMH>
Here is my XSL:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xpath-default-namespace="http://www.openarchives.org/OAI/2.0/"
exclude-result-prefixes="oai_dc dc">
<xsl:output method="xml" indent="yes" encoding="utf-8"/>
<xsl:template match="/">
<xsl:apply-templates select="OAI-PMH/GetRecord/record"/>
</xsl:template>
<xsl:template match="/*/@xsi:*">
<xsl:attribute name="xsi:{local-name()}" namespace="{namespace-uri()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="OAI-PMH/GetRecord/record">
<xsl:element name="doi_batch">
<xsl:namespace name="{local-name()}" select="'http://www.crossref.org/schema/4.4.1'"/>
<xsl:apply-templates select="/*/@xsi:*"/>
<xsl:attribute name="version">
<xsl:text>4.4.1</xsl:text>
</xsl:attribute>

<xsl:element name="head">
<xsl:element name="doi_batch_id">
<xsl:value-of select="format-dateTime(current-dateTime(), '[Y0001][M01][D01][H01][m01][s01]')"/>
</xsl:element>
<xsl:element name="timestamp">
<xsl:value-of select="format-dateTime(current-dateTime(), '[Y0001][M01][D01][H01][m01]')"/>
</xsl:element>
<xsl:element name="depositor">
<xsl:element name="depositor_name">
<xsl:text>umass</xsl:text>
</xsl:element>
<xsl:element name="email_address">
<xsl:text>lisa.palmer@umassmed.edu</xsl:text>
</xsl:element>
</xsl:element>
<xsl:element name="registrant">
<xsl:text>UMASS</xsl:text>
</xsl:element>
</xsl:element>
<xsl:element name="body">
<xsl:element name="journal">
<xsl:element name="journal_metadata">
<xsl:element name="full_title">
<xsl:text>Journal of eScience Librarianship</xsl:text>
</xsl:element>
<xsl:element name="abbrev_title">
<xsl:text>JeSLIB</xsl:text>
</xsl:element>
<xsl:element name="issn">
<xsl:attribute name="media_type">
<xsl:text>electronic</xsl:text>
</xsl:attribute>
<xsl:text>21613974</xsl:text>
</xsl:element>
<xsl:element name="doi_data">
<xsl:element name="doi">
<xsl:text>10.7191/jeslib</xsl:text>
</xsl:element>
<xsl:element name="resource">
<xsl:text>http://escholarship.umassmed.edu/jeslib/</xsl:text>
</xsl:element>
</xsl:element>
</xsl:element>
<xsl:element name="journal_issue">
<xsl:element name="publication_date">
<xsl:attribute name="media_type">
<xsl:text>online</xsl:text>
</xsl:attribute>
<xsl:element name="year">
<xsl:value-of select="substring(./metadata/oai_dc:dc/dc:date.created, 1, 4)"/>
</xsl:element>
</xsl:element>
<xsl:element name="journal_volume">
<xsl:element name="volume">
<xsl:value-of select="substring(./metadata/oai_dc:dc/dc:identifier, 46, 1)"/>
</xsl:element>
</xsl:element>
<xsl:element name="issue">
<xsl:value-of select="substring(./metadata/oai_dc:dc/dc:identifier, 51, 1)"/>
</xsl:element>
</xsl:element>
<xsl:element name="journal_article">
<xsl:attribute name="publication_type">
<xsl:text>full_text</xsl:text>
</xsl:attribute>
<xsl:element name="titles">
<xsl:element name="title">
<xsl:value-of select="./metadata/oai_dc:dc/dc:title"/>
</xsl:element>
</xsl:element>
<xsl:element name="contributors">
<xsl:for-each select="./metadata/oai_dc:dc/dc:creator">
<xsl:element name="person_name">
<xsl:attribute name="sequence">
<xsl:choose>
<xsl:when test="position() eq 1">
<xsl:text>first</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>additional</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:attribute name="contributor_role">
<xsl:text>author</xsl:text>
</xsl:attribute>
<xsl:element name="given_name">
<xsl:value-of select="substring-after(./text(), ', ')"/>
</xsl:element>
<xsl:element name="surname">
<xsl:value-of select="substring-before(./text(), ', ')"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
<xsl:element name="publication_date">
<xsl:attribute name="media_type">
<xsl:text>online</xsl:text>
</xsl:attribute>
<xsl:element name="month">
<xsl:value-of select="substring(./metadata/oai_dc:dc/dc:date.created, 6, 2)"/>
</xsl:element>
<xsl:element name="day">
<xsl:value-of select="substring(./metadata/oai_dc:dc/dc:date.created, 9, 2)"/>
</xsl:element>
<xsl:element name="year">
<xsl:value-of select="substring(./metadata/oai_dc:dc/dc:date.created, 1, 4)"/>
</xsl:element>
</xsl:element>
<xsl:element name="pages">
<xsl:element name="first_page">
<xsl:text>e</xsl:text>
<xsl:value-of select="substring(./metadata/oai_dc:dc/dc:identifier.doi, 21, 4)"/>
</xsl:element>
</xsl:element>
<xsl:element name="doi_data">
<xsl:element name="doi">
<xsl:value-of select="./metadata/oai_dc:dc/dc:identifier.doi"/>
</xsl:element>
<xsl:element name="resource">
<xsl:value-of select="./metadata/oai_dc:dc/dc:identifier"/>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
The problem is that in the transformed output, the doi_batch element has "xmlns:record=" which instead should be just "xmlns=". I've tried several different ways to fix this but nothing is working correctly.
Radu
Posts: 9051
Joined: Fri Jul 09, 2004 5:18 pm

Re: Getting unwanted xmlns prefix in transformation output

Post by Radu »

Hi,

That extra xmlns:record namespace mapping seems to be forcefully generated by you in the XSLT using:

Code: Select all

<xsl:namespace name="{local-name()}" select="'http://www.crossref.org/schema/4.4.1'"/>
on a template which matches an element called "record".

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
rvanderh
Posts: 4
Joined: Thu Aug 02, 2018 8:48 pm

Re: Getting unwanted xmlns prefix in transformation output

Post by rvanderh »

Agreed. But name is a required attribute for the namespace element and I don't know how to adjust the value of name so that the prefix :record is omitted. If I adjust lines 11 and 18 in the stylesheet to "OAI-PMH/GetRecord" and then add /record to my value-of expressions in the rest of the stylesheet below, the xmlns prefix in the doi_batch element becomes :GetRecord in the output. I want to eliminate the prefix altogether but everything I've tried throws an error.
rvanderh
Posts: 4
Joined: Thu Aug 02, 2018 8:48 pm

Re: Getting unwanted xmlns prefix in transformation output

Post by rvanderh »

Also, if I delete the xsl:namespace line (line 20) in the stylesheet and instead add a namespace attribute to the doi_batch element above (line 19), the xmlns prefix disappears in the output but then I also get empty xmlns="" added to the head and body elements in the output.
Radu
Posts: 9051
Joined: Fri Jul 09, 2004 5:18 pm

Re: Getting unwanted xmlns prefix in transformation output

Post by Radu »

Hi,

I think what you really need it to add an extra xmlns="http://www.crossref.org/schema/4.4.1" on the xsl:stylesheet root element so that all XML elements emitted by the XSLT are in that namespace. And remove that xsl:namespace element.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
rvanderh
Posts: 4
Joined: Thu Aug 02, 2018 8:48 pm

Re: Getting unwanted xmlns prefix in transformation output

Post by rvanderh »

Thanks! That worked.
Post Reply