Identity transform adds attributes to elements.

Having trouble installing Oxygen? Got a bug to report? Post it all here.
bds
Posts: 47
Joined: Mon Jun 26, 2006 10:40 pm

Identity transform adds attributes to elements.

Post by bds »

Hi all,
I'm seeing a problem in the following identity transform. I need to add xml:id attributes to <div>s and <pb>s, but when I apply the transform the <p> elements have attributes added unexpectedly. I've tried running the transform from the command line (with both an older version of Saxon, as well as the saxonEE.jar included with oXygen) without experiencing the problem(s) I encounter in the XSLT debugger.

Is this a bug, or am I making a mistake somewhere in my transform?
oXygen 15, Saxon 9.5.0.2.
Thanks for your help!

Sample input:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_lite.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?>
<?xml-model href="http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_lite.rng" type="application/xml"
schematypens="http://purl.oclc.org/dsdl/schematron"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader>
<fileDesc>
<titleStmt>
<title>Title</title>
</titleStmt>
<publicationStmt>
<idno>1234</idno>
</publicationStmt>
<sourceDesc>
<p>Information about the source</p>
</sourceDesc>
</fileDesc>
</teiHeader>
<text>
<body>
<div type="book">
<div type="chapter" n="1">
<pb facs="003" n="1"/>
<head type="main">The Book's Title</head>
<head type="subhead">Chapter I.</head>
<p>Mauris suscipit ultricies odio, nec tincidunt justo laoreet et.</p>
</div>
<div type="chapter" n="2">
<head type="subhead">Chapter 2.</head>
<pb facs="005" n="3"/>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
<div type="chapter" n="3">
<head type="subhead">Chapter 3.</head>
<pb facs="007" n="5"/>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</div>
</body>
</text>
</TEI>
Identity transform:

Code: Select all

<?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"
xmlns="http://www.tei-c.org/ns/1.0"
xpath-default-namespace="http://www.tei-c.org/ns/1.0"
exclude-result-prefixes="#all"
version="2.0">
<!-- adding xml:id values to P5 TEI -->

<!-- variables -->
<xsl:variable name="vID" select="/TEI/*/*/publicationStmt/idno[1]"/>
<xsl:variable name="vID-fileName" select="concat($vID, '_new.xml')"/>
<xsl:variable name="vID-primary" select="concat('ms', $vID)"/>

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

<!-- add xml:id to document node -->
<xsl:template match="TEI">
<TEI xmlns="http://www.tei-c.org/ns/1.0" xml:id="{$vID-primary}">
<xsl:apply-templates/>
</TEI>
</xsl:template>

<!-- add xml:id to divs -->
<xsl:template match="div">
<div>
<xsl:if test="@type">
<xsl:attribute name="type">
<xsl:value-of select="@type"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="@n">
<xsl:attribute name="n">
<xsl:value-of select="@n"/>
</xsl:attribute>
</xsl:if>
<xsl:attribute name="xml:id">
<xsl:value-of select="generate-id(.)"/>
</xsl:attribute>
<xsl:apply-templates/>
</div>
</xsl:template>

<!-- add xml:id to pbs -->
<xsl:template match="pb">
<xsl:variable name="vN" select="@n"/>
<xsl:variable name="vFacs" select="@facs"/>
<pb facs="{$vFacs}" n="{$vN}" xml:id="{generate-id(.)}"/>
</xsl:template>
</xsl:stylesheet>
Output generated by creating and applying a transformation scenario:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?><?xml-model href="http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_lite.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?><?xml-model href="http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_lite.rng" type="application/xml"
schematypens="http://purl.oclc.org/dsdl/schematron"?><TEI xmlns="http://www.tei-c.org/ns/1.0" xml:id="ms1234">
<teiHeader type="text">
<fileDesc>
<titleStmt>
<title>Title</title>
</titleStmt>
<publicationStmt>
<idno>1234</idno>
</publicationStmt>
<sourceDesc default="false">
<p part="N">Information about the source</p>
</sourceDesc>
</fileDesc>
</teiHeader>
<text>
<body>
<div type="book" xml:id="d2e33">
<div type="chapter" n="1" xml:id="d2e35">
<pb facs="003" n="1" xml:id="d2e37"/>
<head type="main">The Book's Title</head>
<head type="subhead">Chapter I.</head>
<p part="N">Mauris suscipit ultricies odio, nec tincidunt justo laoreet et.</p>
</div>
<div type="chapter" n="2" xml:id="d2e49">
<head type="subhead">Chapter 2.</head>
<pb facs="005" n="3" xml:id="d2e54"/>
<p part="N">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
<div type="chapter" n="3" xml:id="d2e60">
<head type="subhead">Chapter 3.</head>
<pb facs="007" n="5" xml:id="d2e65"/>
<p part="N">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</div>
</body>
</text>
</TEI>
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Identity transform adds attributes to elements.

Post by adrian »

Hi,

This happens because by default Oxygen feeds to the XSLT engine the model of the XML document, which in this case is enriched with the default attributes from the TEI RNG schema.

You can easily prevent this by setting an option in the Saxon-HE/PE/EE Advanced options (cogwheel button icon) from the transformation scenario configuration or directly in the XSLT debugger.
Disable the option: Expand attribute defaults ("-expand").

If you want to disable this globally in Oxygen, you can find the same option in: Options > Preferences, XML > XSLT/FO/XQuery > XSLT > Saxon > Saxon HE/PE/EE.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
bds
Posts: 47
Joined: Mon Jun 26, 2006 10:40 pm

Re: Identity transform adds attributes to elements.

Post by bds »

Hi Adrian,
that's brilliant - thanks very much for the help.

Best,
bds
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Identity transform adds attributes to elements.

Post by adrian »

Hi,

It has been pointed out to me that for Relax NG (which applies in the TEI case), you can disable the default attribute values from a different Oxygen option. This way you won't have to change the default behavior of the XSLT engine.
For Relax NG the discussed option is located in:
Options > Preferences, XML > XML Parser > RELAX NG, Add default attribute values.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply