472,348 Members | 2,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,348 software developers and data experts.

How-To: Preserve embedded XHTML tags during XSLT tranform?

I have some XML data that has mixed content XML tags that embed XHTML
tags, for instance:

<note>Somebody wrote this note in XHTML and wanto to <a
href="link.html" target="_new">link</a> to a particular tag, and was
also pretty sure that they wanted the following
<ul><li>two</li><li>items</li></ul> to appear as a list. To make
matters worse <sarcastic>how could this be worse?</sarcastic>, the
XHTML can be mixed with our own tags.</note>

I am trying to transform this data with an XSLT into oddly formated
XHTML for display (I happen to be doing it server-side, with ASP.NET
2.0). The problem is that I need to preserve all of the embedded XHTML
tags in the XML document, while writing templates for all of our
custom content.

The only thing that I have successfully done so far is to write my own
templates for all of the XHTML tags, that output themselves and
process their children, for instance:

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

There are way too many XHTML tag possibilities for me to handle though
(and I have to deal with all of the possible attributes, and output
them correctly too).

Is there a better way to do this, or if there isn't, does anyone have
an XSLT document that handles all (or most) of the XHTML tags that I
can include? I can force everyone to use namespaces if necessary (ie
<xhtml:b></xhtml:b>).

Desperately seeking help,

Luke Dalessandro
Jul 20 '05 #1
4 3083

If your xhtml really is in xhtml (ie is in the xhtml namespace) then you
can easily match on all the xhtml elements without having to list them

<xsl:template match="h:*" xmlns:h="http://www.w3.org/1999/xhtml">
.....
However I suspect that all you want to do is copy the content of a note
element in to the output as-is, in which case you don't need templates
at all, eg

<xsl:template match="note">
<div>
<xsl:copy-of select="node()"/>
</div>
</xsl:template>
David

Jul 20 '05 #2
David,

Thanks for the advice:

I can't use copy-of because of the nesting that is possible in our schema.
Notes may contain custom tags that need their templates applied for output.
If I use copy-of, they won't get transformed.

I'm not sure how to implement the first option. I don't want to write the
templates for all of the xhtml tags, I want to use templates that already
exist, or just copy them while rendering their inner contents with
templates.

I see how that matches the tags, but not how it transforms them correctly.

Thanks,

Luke

"David Carlisle" <da****@nag.co.uk> wrote in message
news:yg*************@penguin.nag.co.uk...

If your xhtml really is in xhtml (ie is in the xhtml namespace) then you
can easily match on all the xhtml elements without having to list them

<xsl:template match="h:*" xmlns:h="http://www.w3.org/1999/xhtml">
....
However I suspect that all you want to do is copy the content of a note
element in to the output as-is, in which case you don't need templates
at all, eg

<xsl:template match="note">
<div>
<xsl:copy-of select="node()"/>
</div>
</xsl:template>
David

Jul 20 '05 #3
I see how that matches the tags, but not how it transforms them correctly.


you want to do an identity transform on xhtml but recursively process
its content (which may not be the identity) so:

<xsl:template match="h:*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
xsl:copy (unlike copy-of) does a "shallow copy" that just generates an
element with the same name as the current node, then you copy any
attributes over then just recursively apply templates which will copy
any child text or xhtml but do whatever it does with your own specific
elements that have their own templates.

the above assumes xsl: and h: bound to appropriate namespaces of course.

David
Jul 20 '05 #4
Perfect!

Thanks

"David Carlisle" <da****@nag.co.uk> wrote in message
news:yg*************@penguin.nag.co.uk...
I see how that matches the tags, but not how it transforms them
correctly.


you want to do an identity transform on xhtml but recursively process
its content (which may not be the identity) so:

<xsl:template match="h:*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
xsl:copy (unlike copy-of) does a "shallow copy" that just generates an
element with the same name as the current node, then you copy any
attributes over then just recursively apply templates which will copy
any child text or xhtml but do whatever it does with your own specific
elements that have their own templates.

the above assumes xsl: and h: bound to appropriate namespaces of course.

David

Jul 20 '05 #5

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

Similar topics

4
by: Tom Szabo | last post by:
Just wandering how can you close a browser remotely. In some web applications the browser closes or refreshes periodically. How is that done and...
15
by: Reid Nichol | last post by:
Hello, I was wondering if I could control how many bytes are in an int and the byte order. In C/C++ I can use int32 but how do I do this in...
0
by: v I n O | last post by:
hI Geeks Please do let me know how do i find how many instances sql server running on the single machine. or in the n/w my objective should with...
4
by: James Salisbury | last post by:
Hi, I was checking my parent's website salisbury.cabrera.net on google by entering villa rent spain cabrera I can see the required site, ranked...
2
by: Frances Del Rio | last post by:
http://www.emol.com/especiales/cocina_chilena/comida.asp this page is so neat (goes in pop-up..) how was this done? how do you give a layer (or...
4
by: | last post by:
Developing, building, and testing. How do it the best? Learning from the world leader - Microsoft I'm very interested in how the...
4
by: Supra | last post by:
in vb6 listbox1.remove item 5 how will i do in vb.net? regards
7
by: anushhprabu | last post by:
#define q(k)main(){ return!puts(#k"\nPRABUq("#k")");} q(#define q(k)main(){return!puts(#k"\nq("#k")");}) guys i'm working on this code.. i...
0
by: candra | last post by:
Learn What Hackers Know? -General Hacking Information -Password Security -Scanning, Fingerprinting And Similar Techniques -How Hackers Attack...
2
by: belred | last post by:
i just read this blog about how many objects (types) are loaded for a hello world program in C#. ...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

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.