Need help inserting namespace

C

CB

I have a group of documents that previously had no schema, now I have
a schema, and I want to use XSLT to fixup the documents by inserting
the namespace reference.

In other words, my input document looks like

<SiteAdaptationFile>
...
</SiteAdaptationFile>

and I'm looking for output like

<SiteAdaptationFile xmlns="http://www.sensis.com/AdaptSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sensis.com/AdaptSchema IddAdapt.xsd">
...
</SiteAdaptationFile>

Here is my xslt script. The foo attribute is just a dummy, and I
put it in because Xalan complains about the other attribute names,
and ignores them. i.e., i wanted to see something change.

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

<xsl:eek:utput method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>

<xsl:template match="/">
<xsl:apply-templates select="SiteAdaptationFile"/>
</xsl:template>

<xsl:template match="SiteAdaptationFile">
<xsl:element name="SiteAdaptationFile">
<xsl:attribute name="foo" >
<xsl:text>bar</xsl:text>
</xsl:attribute>
<xsl:attribute name="xmlns" >
<xsl:text>http://www.sensis.com/AdaptSchema</xsl:text>
</xsl:attribute>
<xsl:attribute name="xmlns:xsi" >
<xsl:text>http://www.w3.org/2001/XMLSchema-instance</xsl:text>
</xsl:attribute>
<xsl:attribute name="xsi:schemaLocation" >
<xsl:text>http://www.sensis.com/AdaptSchema
IddAdapt.xsd</xsl:text>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

My actual output is

<SiteAdaptationFile foo="bar">
...
</SiteAdaptationFile>

I'm sure my question exposes my ignorance of XSLT, and I appreciate
any help, very much.
 
J

Julian F. Reschke

CB said:
I have a group of documents that previously had no schema, now I have
a schema, and I want to use XSLT to fixup the documents by inserting
the namespace reference.

In other words, my input document looks like

<SiteAdaptationFile>
...
</SiteAdaptationFile>

and I'm looking for output like

<SiteAdaptationFile xmlns="http://www.sensis.com/AdaptSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sensis.com/AdaptSchema IddAdapt.xsd">
...
</SiteAdaptationFile>

Here is my xslt script. The foo attribute is just a dummy, and I
put it in because Xalan complains about the other attribute names,
and ignores them. i.e., i wanted to see something change.

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

<xsl:eek:utput method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>

<xsl:template match="/">
<xsl:apply-templates select="SiteAdaptationFile"/>
</xsl:template>

<xsl:template match="SiteAdaptationFile">
<xsl:element name="SiteAdaptationFile">
<xsl:attribute name="foo" >
<xsl:text>bar</xsl:text>
</xsl:attribute>
<xsl:attribute name="xmlns" >
<xsl:text>http://www.sensis.com/AdaptSchema</xsl:text>
</xsl:attribute>
<xsl:attribute name="xmlns:xsi" >
<xsl:text>http://www.w3.org/2001/XMLSchema-instance</xsl:text>
</xsl:attribute>
<xsl:attribute name="xsi:schemaLocation" >
<xsl:text>http://www.sensis.com/AdaptSchema
IddAdapt.xsd</xsl:text>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

My actual output is

<SiteAdaptationFile foo="bar">
...
</SiteAdaptationFile>

I'm sure my question exposes my ignorance of XSLT, and I appreciate
any help, very much.

XSLT doesn't treat namespace declarions as attributes.

Just do something like:

<xsl:template match="SiteAdaptationFile">
<SiteAdaptationFile xmlns="http://www.sensis.com/AdaptSchema">
<xsl:apply-templates/>
</SiteAdaptionFile>
</xsl:template>
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top