namespace prefixes in output xml

H

Hubidubi

Hi,

I would like to generate XML (struts code) from XML with XSL
transformation. I run into problem when I wanted to use tags with
prefixes, like <html:text />.

I included namespace specification to the header:
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:html="struts">

It works, but it also includes namespace information in every tag,
that's not perfect:

<html:text xmlns:html="struts">
ez a teszt
</html:text>

So how can I use the prefixes without the additional namespace
information in the output tags?

Thanks,

Hubidubi
 
G

George Bina

Hi,

You can simply iterate on the ancestor or self axes and output the
XPath expressin matching the current element. Below you can find a
sample stylesheet that outputs the XPath expressions matching all the
document nodes.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput indent="yes"/>

<xsl:template match="/">
<xpaths>
<xsl:apply-templates/>
</xpaths>
</xsl:template>
<xsl:template match="*">
<xpath>
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="name()"/>
<xsl:variable name="count">
<xsl:number/>
</xsl:variable>
<xsl:text>[</xsl:text>
<xsl:value-of select="$count"/>
<xsl:text>]</xsl:text>
<xsl:if test="position()!=last()">/</xsl:if>
</xsl:for-each>
</xpath>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>

On something like

<?xml version="1.0" encoding="UTF-8"?>
<test>
<a> </a>
<b/>
<a>
<c> </c>
<x> </x>
</a>
</test>


it will give you
<?xml version="1.0" encoding="UTF-8"?>
<xpaths>
<xpath>test[1]</xpath>
<xpath>test[1]/a[1]</xpath>
<xpath>test[1]/b[1]</xpath>
<xpath>test[1]/a[2]</xpath>
<xpath>test[1]/a[2]/c[1]</xpath>
<xpath>test[1]/a[2]/x[1]</xpath>
</xpaths>

Best Regards,
George
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top