namespaces and attributes.

Here should go questions about transforming XML with XSLT and FOP.
christian
Posts: 5
Joined: Thu Jun 29, 2006 5:53 pm

namespaces and attributes.

Post by christian »

Hello,

I have been struggling with namespaces recently and have finally got my xsl doing what I want it to do. Below is the original xml and my xsl. I wanted to have the ml namespace simply as the default but found that <xsl:template match="formField"> was never invoked, so I added :ml to the default namespace and to <xsl:template match="ml:formField"> and got the right result.

My questions if anyone is interested in answering are

1) Why cant I have the ml namespace as the default namespace in the XSL?
2) Why don't I have to add ml: to other bits of the xsl where I reference attributes, eg <xsl:attribute name="fieldName">. Are attributes treated differently?

Here is the working code with :ml in the namespace declarations.

XML
-----------------------------------------
<?xml version="1.0"?>
<main xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.digitalpenandpaper.net/digit ... ormXmlFlat">
<form formID="FaceOverview" fhUid="962" fhtUid="2724">
<formFieldList>
<formField fieldName="P1.S2.NHSID">11</formField>
<formField fieldName="P1.S2.SocialServicesID" />
<formField fieldName="P1.S2.Team" />
<formField fieldName="P1.S2.Title" />
<formField fieldName="P1.S3.BriefDescription">This is a brief description</formField>
<formField fieldName="P1.S6.Transport" />
<formField fieldName="P1.S6.VoluntarySector" />
</formFieldList>
</form>
</main>


XSL
-------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ml="http://www.digitalpenandpaper.net/digit ... ormXmlFlat" version="1.0">

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

<xsl:template match="ml:formField">
<xsl:copy>
<xsl:attribute name="fieldName">
<xsl:call-template name="renameFiledNameAttrib">
<xsl:with-param name="fieldName" select="@fieldName"/>
</xsl:call-template>
</xsl:attribute>
<xsl:copy-of select="text() | attribute::*[local-name()!='fieldName']"/>
<xsl:apply-templates select="*"/>
</xsl:copy>
</xsl:template>

<xsl:template name="renameFiledNameAttrib">
<xsl:param name="fieldName"/>
<xsl:choose>
<xsl:when test="$fieldName='P1.S3.BriefDescription'">Text016</xsl:when>
<xsl:otherwise><xsl:value-of select="$fieldName"/></xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi,

1.
Because XSLT 1.0 does not allow a different default namespace than no namespace, so no prefix means no namespace. In XSLT 2.0 you can define the default namespace with the xpath-default-namespace attribute.

2.
The default namespace does not appy to attributes so an attribute whose name does not have a prefix is in no namespace.

Best Regards,
George
Post Reply