[xsl] Help namespacing an attribute

Subject: [xsl] Help namespacing an attribute
From: "Bridger Dyson-Smith bdysonsmith@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 20 Sep 2017 16:56:39 -0000
Hi all,

I'm having some trouble getting a namespace attached to an attribute --
instead the attribute's namespace is getting attached to the element. I
worked up a minimized example that worked the way I expected it to work,
but when trying to implement my expectations I'm failing.

I'm roundly convinced that this doesn't work in my real data because I'm
missing something with namespaces but I'm not sure what. Despite trying
both serialization methods (literal and programmatic), both return the same
result.

I'm using XSLT 2.0, but the application is using Saxon 8.7.

I'm looking for

<accessCondition type="use and reproduction" xlink:href="http://some.uri"/>

but what I'm getting in my real work is

<accessCondition xmlns:xlink="http://www.w3.org/1999/xlink"; type="use and
reproduction" xlink:href="http://some.uri"/>


Thanks in advance for any light you can shed on this.
Best,
Bridger

-------
Here are my example data and transform that work correctly. I have input
data that gets identity transformed into new data; e.g.

<?xml version="1.0" encoding="UTF-8"?>
<!-- example input -->
<record xmlns="http://abc.net/ns";>
  <title>title</title>
  <rights>rights text</rights>
  <license>license text</license>
</record>

and the transform:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  xmlns:xlink="http://www.w3.org/1999/xlink";
  xmlns="http://def.net/ns";
  xpath-default-namespace="http://abc.net/ns";
  exclude-result-prefixes="xs"
  version="2.0">

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="record">
    <xsl:variable name="vACC"
                  select="if ((license and rights) or (license and
not(rights)))
                          then (license)
                          else if (not(license) and rights)
                            then (rights)
                            else ()"/>
    <new-record xmlns="http://def.net/ns"; xmlns:xlink="
http://www.w3.org/1999/xlink";>
      <xsl:apply-templates select="title"/>
      <xsl:element name="condensed-rights">
        <xsl:attribute name="type" select="'use and reproduction'"/>
        <xsl:attribute name="xlink:href">
          <xsl:value-of select="$vACC"/>
        </xsl:attribute>
      </xsl:element>
    <!-- direct serialization works, too -->
    <!--<condensed-rights type="use and reproduction"
xlink:href="{$vACC}"/>-->
    </new-record>
  </xsl:template>

  <xsl:template match="title">
    <new-title><xsl:apply-templates/></new-title>
  </xsl:template>
</xsl:stylesheet>

and results with Saxon 8.7 and Saxon-HE 9.7.0.15:
<new-record xmlns="http://def.net/ns"; xmlns:xlink="
http://www.w3.org/1999/xlink";>
   <new-title>title</new-title>
   <condensed-rights type="use and reproduction" xlink:href="license text"/>
</new-record>
------

Here are abbrieveated real world data and transform examples. I have a few
different serialization attempts in the transform, but I'm not able to get
them to behave as expected.

<!-- example input -->
<oai_qdc:qualifieddc xmlns:oai_qdc="http://worldcat.org/xmlschemas/qdc-1.0/";
  xmlns:dcterms="http://purl.org/dc/terms/"; xmlns:dc="
http://purl.org/dc/elements/1.1/";
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
  xsi:schemaLocation="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";>
  <dc:title>Title</dc:title>
  <dc:rights>http://rightsstatements.org/vocab/NoC-US/1.0/</dc:rights>
  <dcterms:license>https://creativecommons.org/publicdomain/mark/1.0/
</dcterms:license>
</oai_qdc:qualifieddc>

<!-- shortened transform -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:xs="http://www.w3.org/2001/XMLSchema";
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
                xmlns:xlink="http://www.w3.org/1999/xlink";
                xmlns:oai_qdc="http://worldcat.org/xmlschemas/qdc-1.0/";
                xmlns:dcterms="http://purl.org/dc/terms/";
                xmlns:dc="http://purl.org/dc/elements/1.1/";
                xmlns:oai="http://www.openarchives.org/OAI/2.0/";
                xmlns="http://www.loc.gov/mods/v3";
                exclude-result-prefixes="#all"
                version="2.0">

  <!-- output settings -->
  <xsl:output encoding="UTF-8" method="xml" omit-xml-declaration="yes"
indent="yes"/>
  <xsl:strip-space elements="*"/>

  <!-- identity transform -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- match oai_qdc:qualifieddc -->
  <xsl:template match="oai_qdc:qualifieddc">
    <!-- document-level variables -->
    <xsl:variable name="vAC"
      select="if ((dcterms:license and dc:rights) or (dcterms:license and
not(dc:rights)))
              then (dcterms:license)
                else if (not(dcterms:license) and dc:rights)
                then (dc:rights)
                else ()"/>
    <!-- match the document root and return a MODS record -->
    <mods xmlns="http://www.loc.gov/mods/v3"; version="3.5" xmlns:xlink="
http://www.w3.org/1999/xlink";>
      <xsl:apply-templates select="dc:title"/>
      <accessCondition type="use and reprod" xlink:href="
http://www.google.com"/>
      <accessCondition>
        <xsl:attribute name="type" select="'use and reproduction'"/>
        <xsl:attribute name="xlink:href" select="'http://some.url'"/>
      </accessCondition>
      <xsl:element name="accessCondition">
        <xsl:attribute name="type" select="'u.a.r.'"/>
        <xsl:attribute name="xlink:href" select="'http://some.url'"/>
      </xsl:element>
    </mods>
  </xsl:template>

  <!-- title -->
  <xsl:template match="dc:title">
    <titleInfo>
      <title><xsl:apply-templates/></title>
    </titleInfo>
  </xsl:template>
</xsl:stylesheet>

and results with Saxon 8.7 and Saxon-HE 9.7.0.15

<mods xmlns="http://www.loc.gov/mods/v3"; version="3.5">
  <titleInfo>
    <title>Title</title>
  </titleInfo>
  <accessCondition xmlns:xlink="http://www.w3.org/1999/xlink"; type="use and
reprod"
    xlink:href="http://www.google.com"/>
  <accessCondition xmlns:xlink="http://www.w3.org/1999/xlink"; type="use and
reproduction"
    xlink:href="http://some.url"/>
  <accessCondition xmlns:xlink="http://www.w3.org/1999/xlink"; type="u a r"
    xlink:href="http://some.url"/>
</mods>

Current Thread