How to output raw XML via XSL

2,642 views
Skip to first unread message

Bryan Zera

unread,
Aug 26, 2009, 12:45:02 PM8/26/09
to XSLT
We have some HTML embedded inside one of our XML elements.
<pre>
<page>
<additional_links>
<strong>I'm pretty awesome</strong>
<br/>
<ul>
<li>I have a mohawk</li>
<li>I listen to bands you've never heard of</li>
</ul>
</additional_links>
</page>
</pre>

Rather than write the XSL that will rewrite every permutation of HTML
tags that could exist in that space, I'd like to just output the XML
text of the content of the <additional_links> element. Is that
possible and, if so, how would I do it?

Thank you for assisting a neophyte,
Bryan

Bryan Zera

unread,
Aug 26, 2009, 1:01:43 PM8/26/09
to XSLT
Been playing with the transformation in Oxygen and I think I got it:

<xsl:for-each select="additional_links/*">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:for-each>

Bryan Zera

unread,
Aug 26, 2009, 1:05:31 PM8/26/09
to XSLT
The previous code snippet gets all the tags, but none of the
attributes. Is there a better way that I'm not seeing?

Lukek

unread,
Aug 27, 2009, 7:24:30 AM8/27/09
to XSLT
<xsl:for-each select="additional_links/*">
<xsl:copy>
<xsl:for-each select="./@*">
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:for-each
<xsl:apply-templates />
</xsl:copy>
</xsl:for-each>

Bryan Zera

unread,
Aug 28, 2009, 11:20:22 AM8/28/09
to XSLT
Lukek> Thanks so much! This is getting me so much closer. However,
the output is still incorrect. It processes the open and close of the
LI elements before it processes the A elements inside them. This is
correct according to XSL, but it provides pretty useless output.

Isn't there some sort of XSL processing directive that can return the
exact XML node (or even better, the tree of nodes from a given point)?

XML:
<additional_links>
<b cmid="Wide Open Page:Additional Links"><br/>Student Basics:</b><br/
>
<ul>
<li><a class="" href="http://cms.colum.edu/student_handbook/"
target="_self" title="">The Informer: The Student Handbook</a></li>
<li><a class="" href="../Administrative_offices/Campus_Card/
index.xml" target="_self" title="Campus Card Office">U-Pass + Campus
Card</a></li>
<li><a class="" href="../Student_Financial_Services/index.xml"
target="_self" title="Student Financial Services">Student Financial
Services</a></li>
<li><a class="" href="../Administrative_offices/Records/index.xml"
target="_self" title="Records Office">Records Office</a></li>
<li><a class="" href="http://www.lib.colum.edu/" target="_self"
title="Library">The Library</a></li>
</ul>
</additional_links>

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

<xsl:template match="/system-index-block">
<xsl:apply-templates select="descendant::additional_links" /
>
</xsl:template>

<xsl:template match="additional_links">
<xsl:for-each select="descendant::*">
<xsl:call-template name="raw-xml-output" />
</xsl:for-each>
</xsl:template>

<xsl:template name="raw-xml-output">
<xsl:copy>
<xsl:for-each select="./@*">
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:for-each>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>

</xsl:stylesheet>


OUTPUT:
<?xml version="1.0" encoding="utf-8"?><b cmid="Wide Open
Page:Additional Links">Student Basics:</b><br/><br/><ul>
The Informer: The Student Handbook
U-Pass + Campus Card
Student Financial Services
Records Office
The Library</ul><li>The Informer: The Student Handbook
</li><a class="" href="http://cms.colum.edu/student_handbook/"
target="_self" title="">The Informer: The Student Handbook</a><li>U-
Pass + Campus Card
</li><a class="" href="../Administrative_offices/Campus_Card/
index.xml" target="_self" title="Campus Card Office">U-Pass + Campus
Card</a><li>Student Financial Services
</li><a class="" href="../Student_Financial_Services/index.xml"
target="_self" title="Student Financial Services">Student Financial
Services</a><br/><li>Records Office
</li><a class="" href="../Administrative_offices/Records/
index.xml" target="_self" title="Records Office">Records Office</
a><li>The Library</li><a class="" href="http://www.lib.colum.edu/"
target="_self" title="Library">The Library</a><br/>

Lukek

unread,
Aug 29, 2009, 9:16:56 AM8/29/09
to XSLT
if you just want to do an exact copy of <additional_links> just use
this
<xsl:template match="additional_links">
<xsl:copy-of select="."/>
</xsl:template>

use if you only want the children
<xsl:template match="additional_links">
<xsl:for-each select="./*">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:template>
> > > > > Bryan- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages