473,549 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSL: need help with general * template

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>ab c</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="SPECIAL 1" />
<xsl:apply-templates select="SPECIAL 2" />
...
...
<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:valu e-of select="name(.) "/></name>
<value><xsl:val ue-of select="$tag_va lue"/></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
4 1578
Pi******@hotmai l.com wrote:
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?


A simple fix would be to apply the "mode" attribute:

<features>
<xsl:apply-templates mode="other" />
</features>

....
<xsl:template match="*" mode="other">
...
</xsl:template>
HTH;
JW
Dec 27 '05 #2
Hi Jan,

I tried what you suggested. I defined a general template of the form:

<xsl:template match="*" mode="other">
<feature>
<value><xsl:val ue-of select="."/></value>
</feature>
</xsl:template>

but i have the same problem again, the value for ALL the tags is caught
by the template, rather than only the ones without any template
definitions.

Someone had suggested using this approach:

*[not(self::X)][not(self::Y)][not(self::Z)]

but that would mean having to explicitly say "not the following tags" -
i wanted to avoid this approach, but if I have no other option, I would
like to know how I would define a template for this condition and how I
would call it?

is this the way to define it?

<xsl:template match="*[not(self::X)][not(self::Y)][not(self::Z)]">
</xsl:template>

and how do I call this?

<xsl:apply-templates
select="*[not(self::X)][not(self::Y)][not(self::Z)]" />

Thanks
Rohit.

Dec 28 '05 #3
Pi******@hotmai l.com wrote:
is this the way to define it?

<xsl:template match="*[not(self::X)][not(self::Y)][not(self::Z)]">
</xsl:template>

No, just match "*" as you did before.
and how do I call this?

<xsl:apply-templates
select="*[not(self::X)][not(self::Y)][not(self::Z)]" />


Correct. As an alternative, and when the SPECIAL* elements are always the
first two children, you could use:

<xsl:apply-templates select="*[position() &gt; 2]" />
JW
Dec 28 '05 #4
Thanks jan. I'm now getting the behaviour that I was expecting.

I guess we need to be really careful when working with general
templates, they might not act like we expect them to!

Dec 29 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3473
by: Michael Fork | last post by:
Note: I pasted the code the attachments as plain text after the message (I wasn't able to post it with an attachment...) Attached are the XSL and XML files that I am having problems with. I am trying to extract the stock information after having downloaded the HTML and converted to XML (well-formed HTML) using XSL and XPATH and am unable. ...
15
2551
by: Simon Harvey | last post by:
Hi everyone, I am fairly new to learning about xsl and xml, but one thing I have noticed is that anyone offering a tutorial or lesson on it seems to think that its the most incredible invention ever made. I don't. I'm wondering what everyone else thinks. I know the smart answer is, use it if you need it and don't if you don't.
2
2247
by: Adelard | last post by:
Here is my xsl and xml, I want the xml node: LABEL3 ( General Information ) to move under the ROW when viewing or printing this PDF Document. How can I do it? Thanks
0
1542
by: Sharon | last post by:
Hiya, I have this stylesheet (based on an example @ http://rdcpro.com/xmldev/filterandsort)which I'm trying to modify so that it will work for my own XML and I get some data but it doesn't appear within the table as intended. Does anyone know why this is? This is my XSL followed by my XML. I hope someone can help me! love, Sharon <?xml...
7
2155
by: Sharon | last post by:
Hi, Is it possible to check parameters against other parameters, as in: <xsl:variable name="tableData"> <xsl:apply-templates select="general/data/rows/row/fvalues" mode="tableData" /> </xsl:variable> (The parts I'm referring to are in capitals). Because this gives me no
0
1314
by: Piper707 | last post by:
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>
1
1706
by: sommarlov | last post by:
Hi everyone >From one of our systems an xml file is produced. I need to validate this file before we send it to an external system for a very lenghty process. I cannot change the xml file layout. The solution i got today is very slow, and i need help to find another solution. Here is the xml file. It consists of a list of position ids...
1
1275
by: IanK | last post by:
I have a file called product.xml as follows: <products> <product name="Maxi-ISA" friendlyname="Isa" /> <product name="PEP" friendlyname="Personal Equity Plan" /> <product name="General" friendlyname="General account" /> <product name="Pension" friendlyname="Pension drawdown" /> </products> This is a file that maps product names (the...
0
1469
by: zamba | last post by:
hi im new with xsl and i have to process a xsl with a lot of xml (about 50.000) the transform is slow ..and i want to optimize my xsl . here is the example so if anybody could give some advices to me i will appreciatte : <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:fox="http://xml.apache.org/fop/extensions"...
0
7524
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7451
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6048
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5372
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5089
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.