removing a namespace prefix and removing all attributes not in that same prefix

C

Chris Chiasson

Hi,
After reading and experimenting for a several hours, I have this
stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mml="http://www.w3.org/1998/Math/MathML"
xmlns:xmlns="http://www.w3.org/2000/xmlns/"
xmlns:mathematica="http://www.wolfram.com/XML/"
version="1.0">
<xsl:template match="mml:*">
<xsl:element name="{local-name(.)}" namespace="{namespace-uri(.)}">
<xsl:for-each select="@mml:*">
<xsl:attribute name="{local-name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

which transforms this file

<?xml version="1.0" encoding="UTF-8"?>
<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"
mathematica:form="TraditionalForm"
xmlns:mathematica="http://www.wolfram.com/XML/">
<mml:mstyle mathsize="10" fontfamily="Times New Roman">
<mml:mrow>
<mml:mi>F</mml:mi>
<mml:mo>=</mml:mo>
<mml:mrow>
<mml:msup>
<mml:msub>
<mml:mi>X</mml:mi>
<mml:mn>1</mml:mn>
</mml:msub>
<mml:mn>2</mml:mn>
</mml:msup>
<mml:mo>-</mml:mo>
<mml:mrow>
<mml:mn>3</mml:mn>
<mml:mo>⁢</mml:mo>
<mml:msub>
<mml:mi>X</mml:mi>
<mml:mn>2</mml:mn>
</mml:msub>
<mml:mo>⁢</mml:mo>
<mml:msub>
<mml:mi>X</mml:mi>
<mml:mn>1</mml:mn>
</mml:msub>
</mml:mrow>
<mml:mo>+</mml:mo>
<mml:msub>
<mml:mi>X</mml:mi>
<mml:mn>1</mml:mn>
</mml:msub>
<mml:mo>+</mml:mo>
<mml:mrow>
<mml:mn>4</mml:mn>
<mml:mo>⁢</mml:mo>
<mml:msup>
<mml:msub>
<mml:mi>X</mml:mi>
<mml:mn>2</mml:mn>
</mml:msub>
<mml:mn>2</mml:mn>
</mml:msup>
</mml:mrow>
<mml:mo>-</mml:mo>
<mml:msub>
<mml:mi>X</mml:mi>
<mml:mn>2</mml:mn>
</mml:msub>
</mml:mrow>
</mml:mrow>
</mml:mstyle>
</mml:math>

into this:

<?xml version="1.0"?>
<math xmlns="http://www.w3.org/1998/Math/MathML"
form="TraditionalForm">
<mstyle mathsize="10" fontfamily="Times New Roman">
<mrow>
<mi>F</mi>
<mo>=</mo>
<mrow>
<snip/>
</math>

as you can see, it does properly localize the MathML elements and
attributes. Unfortunately, it doesn't exclude the mathematica:form
attribute - it just localizes it. So, how can this attribute be
removed?

Thank you,
 
D

Dimitre Novatchev

Read about "identity rule" or "identity transformation"

Cheers,
Dimitre Novatchev
 
C

Chris Chiasson

I started with an identity transformation to make what I already have.
The problem I am experiencing is that I am somehow misusing the
namespace axis of Xpath in a way that is giving me all the attributes
instead of just the ones in the MathML namespace.
 
C

Chris Chiasson

Mauritz Jeanson helped me out. Here a the solution:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mml="http://www.w3.org/1998/Math/MathML"
xmlns:mathematica="http://www.wolfram.com/XML/"
version="1.0">
<xsl:template match="mml:*">
<xsl:element name="{local-name(.)}" namespace="{namespace-uri(.)}">
<xsl:for-each
select="@*[namespace-uri()=namespace-uri(parent::self)]">
<xsl:copy/>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
 
C

Chris Chiasson

Actually, Mauritz Jeanson wrote me again to say that parent::self is
wrong. I don't know why it works in xsltrproc on windows, but it
shouldn't have.

Anyway,

the "right" xpath for the attributes is as follows:
@*[namespace-uri()='' or namespace-uri()=namespace-uri(parent::*)]
 
R

Richard Tobin

Chris Chiasson said:
Actually, Mauritz Jeanson wrote me again to say that parent::self is
wrong. I don't know why it works in xsltrproc on windows, but it
shouldn't have.

parent::self will typically be an empty node set (unless the parent
happens to be called "self"), so namespace-uri(parent::self) will be
the empty string. That will "work" when the parent is in no
namespace.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top