By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,314 Members | 2,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,314 IT Pros & Developers. It's quick & easy.

XSL: question on using general * templates

P: n/a
I need help with using a general template which would process all tags
other than the ones for which specific templates have been written.

My XML looks like this:

<ITEMS>
<SPECIAL1>abc</SPECIAL1>
<SPECIAL2>abc</SPECIAL2>
...
<SPECIAL12>abc</SPECIAL9>
...
<KEY>0</KEY>
<KEY>1</KEY>
...
<OTHER1>xyz</OTHER1>
<OTHER2>xyz</OTHER2>
...
</ITEMS>
--------------------------------------

I need to convert to this format:

<elements>
//all SPECIAL tags are processed with a template correctly
<keys>
// all KEY tags processed with a template correctly
<keys>
//all tags other than the above mentioned need to be converted like
this.
<features>
<feature>
<name>other1</name>
<value>xyz</value>
</feature>
</feature>
<name>other2</name>
<value>xyz</value>
</feature>
</features>
</elements>
--------------------------------------

My xsl looks like this:

<xsl:template match="ITEMS">
<elements>
<xsl:apply-templates select="SPECIAL1" />
<xsl:apply-templates select="SPECIAL2" />
...
...
<xsl:if test="count(KEY) &gt; 0">
<keys>
<xsl:apply-templates select="KEY" />
</keys>
</xsl:if>

//WORKS FINE TILL THIS POINT

<features>
<xsl:apply-templates/>
</features>
</elements>
</xsl:template>

//TEMPLATES FOR SPECIAL1, SPECIAL2, KEY etc defined here

//general template definition
<xsl:template match="*">
<xsl:variable name="tag_value">
<xsl:value-of select="."/>
</xsl:variable>
<feature>
<name><xsl:value-of select="name(.)"/></name>
<value><xsl:value-of select="$tag_value"/></value>
</feature>
</xsl:template>

--------------------------------------

But this general template doesn't work. It gets invoked for all tags
regardless of the fact that they have their own templates defined.

I really need to figure out when I get a tag that is NOT SPECIAL1,
SPECIAL2...KEY then dump all those tags into a
<features></features> root tag in the new format.

How can I get the above behaviour?

Thanks for any help.
Rohit.

Dec 27 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.