Output content of XML for current node


Update

Although the code below provides the output of the current node, an even better way to get the conntect of the current node is to use the XSLT visualizer to get the content returned from <xsl:copy-of select=”…” />.

To use the XSLT visualizer go to the XSLT file in Umbraco. Highlight the XSLT whose result you wish to visualize and click on the “Visualize XSLT” button. Choose the relevant page and check the “Encode/Decode result” checkbox. Then simply copy and paste the code produced into your favourite code editor.

——————————————————————–

The more I use XSLT in Umbraco, the more I realise how powerful it is. However, sometimes it would be nice to see the XML tree that is produced by Umbraco and for which we need XSLT to traverse. We could of course simply use <xsl:copy-of select=”…….”/> but having something “nicely” formatted was what I was after.

I came across this post answer by Dirk, who pointed me to this code. However, due to HTML encoding issues &lt; were being converted to < and &gt; to > and hence was causing issues when saving the XSLT in the Umbraco admin XSLT editor.

Below is the same code but with the &lt; and &gt; in place and hence that will avoid you from having to look for the offending characters. Oh and if anyone is actually reading my blog, I’d like to know where I can get hold of Doug Robar’s DebugPrintOutAllNodes macro as well as Tim Maynard Smith’s amended version as the links provided are no longer valid.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp " "> ]>
<xsl:stylesheet
 version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxml="urn:schemas-microsoft-com:xslt"
 xmlns:umbraco.library="urn:umbraco.library"
 exclude-result-prefixes="msxml umbraco.library">

<xsl:output method="html"/>
<xsl:param name="currentPage"/>

 <xsl:template match="/">
 <DIV style="float:bottom; margin: 15px;" >
 <h1>XPath view of your document</h1>
 <p>The structure of your document (as defined by
 the XPath standard) is outlined below.</p>
 <table cellspacing="5" cellpadding="2" border="0">
 <tr>
 <td colspan="7">
 <b>Node types:</b>
 </td>
 </tr>
 <tr>
 <td bgcolor="#99CCCC"><b>root</b></td>
 <td bgcolor="#CCCC99"><b>element</b></td>
 <td bgcolor="#FFFF99"><b>attribute</b></td>
 <td bgcolor="#FFCC99"><b>text</b></td>
 <td bgcolor="#CCCCFF"><b>comment</b></td>
 <td bgcolor="#99FF99"><b>processing instruction</b></td>
 <td bgcolor="#CC99CC"><b>namespace</b></td>
 </tr>
 </table>
 <br />
 <table width="300px" border="2" bgcolor="#99CCCC" cellspacing="2">
 <tr bgcolor="#99CCCC">
 <td colspan="2">
 <b>root:</b>
 </td>
 </tr>
 <xsl:for-each select="namespace::*">
 <tr bgcolor="#CC99CC">
 <td width="15">   </td>
 <td>
 <xsl:call-template name="namespace-node"/>
 </td>
 </tr>
 </xsl:for-each>
 <xsl:for-each select="$currentPage">
 <tr bgcolor="#99CCCC">
 <td width="15">   </td>
 <td>
 <xsl:apply-templates select="."/>
 </td>
 </tr>
 </xsl:for-each>
 </table>
 </DIV>
 </xsl:template>

 <xsl:template match="comment()">
 <table width="100%" cellspacing="2">
 <tr>
 <td bgcolor="#CCCCFF">
 <b>comment: </b>
 <span>
 <xsl:value-of select="."/>
 </span>
 </td>
 </tr>
 </table>
 </xsl:template>

 <xsl:template match="processing-instruction()">
 <table border="0" width="100%" cellspacing="2">
 <tr>
 <td bgcolor="#99FF99">
 <b>processing instruction: </b>
 <span>
 <xsl:text>&lt;?</xsl:text>
 <xsl:value-of select="name()"/>
 <xsl:text>?&gt;</xsl:text>
 <br />
 <xsl:value-of select="."/>
 </span>
 </td>
 </tr>
 </table>
 </xsl:template>

 <xsl:template match="text()">
 <xsl:if test="string-length(normalize-space(.))">
 <tr>
 <td bgcolor="#CCCC99" width="15">    </td>
 <td bgcolor="#FFCC99" width="100%">
 <b>text: </b>
 <span>
 <xsl:value-of select="."/>
 </span>
 </td>
 </tr>
 </xsl:if>
 </xsl:template>

 <xsl:template name="namespace-node">
 <table border="0" width="100%" cellspacing="2">
 <tr>
 <td bgcolor="#CC99CC">
 <b>namespace: </b>
 <span>
 <xsl:value-of select="name()"/>
 </span>
 <br />
 <span>
 <xsl:value-of select="."/>
 </span>
 </td>
 </tr>
 </table>
 </xsl:template>

 <xsl:template match="*">
 <table border="1" width="100%" cellspacing="2">
 <xsl:choose>
 <xsl:when test="count(@*) > 0">
 <tr>
 <td bgcolor="#CCCC99" colspan="2">
 <b>element: </b>
 <span>
 <xsl:text>&lt;</xsl:text>
 <xsl:value-of select="name()"/>
 <xsl:text>&gt;</xsl:text>
 </span>
 <table border="0" width="100%" cellspacing="2">
 <tr>
 <td bgcolor="#CCCC99" width="15">   </td>
 <td bgcolor="#FFFF99" width="20%">
 <b>attribute name</b>
 </td>
 <td bgcolor="#FFFF99">
 <b>value</b>
 </td>
 </tr>
 <xsl:for-each select="@*">
 <tr>
 <td bgcolor="#CCCC99" width="15">   </td>
 <td bgcolor="#FFFF99" width="20%">
 <span>
 <xsl:value-of select="name()"/>
 </span>
 </td>
 <td bgcolor="#FFFF99">
 <span>
 <xsl:value-of select="."/>
 </span>
 </td>
 </tr>
 </xsl:for-each>
 </table>
 </td>
 </tr>
 </xsl:when>
 <xsl:otherwise>
 <tr>
 <td bgcolor="#CCCC99" colspan="2">
 <b>element: </b>
 <span>
 <xsl:text>&lt;</xsl:text>
 <xsl:value-of select="name()"/>
 <xsl:text>&gt;</xsl:text>
 </span>
 </td>
 </tr>
 </xsl:otherwise>
 </xsl:choose>
 <xsl:for-each select="namespace::*">
 <tr>
 <td bgcolor="#CCCC99" width="15">    </td>
 <td bgcolor="#CC99CC">
 <xsl:call-template name="namespace-node"/>
 </td>
 </tr>
 </xsl:for-each>
 <xsl:for-each select="*|comment()|processing-instruction()|text()">
 <tr bgcolor="#CCCC99">
 <td width="15">   </td>
 <td>
 <xsl:apply-templates select="."/>
 </td>
 </tr>
 </xsl:for-each>
 </table>
 </xsl:template>

</xsl:stylesheet>
Advertisements
Tagged with: ,
Posted in Umbraco blogs, XSLT

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: