Node processing containing internal escaped XML

I have an XML document with node containing the shielded XML serialization of another object, as in this example:

<attribute> <value> &lt;map&gt; &lt;item&gt; &lt;src&gt;something&lt;/src&gt; &lt;dest&gt;something else&lt;/dest&gt; &lt;/item&gt; &lt;/map&gt; </value> </attribute> 

How to apply xslt template to internal xml? In particular, I would like to get an html table with src / dest pairs:

 | src | dest | | something | something else | 
+3
source share
2 answers

I would do it as a two-step operation.

Step1.xsl:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:template match="/"> <root> <xsl:apply-templates select="attribute/value" /> </root> </xsl:template> <xsl:template match="value"> <object> <xsl:value-of select="." disable-output-escaping="yes" /> </object> </xsl:template> </xsl:stylesheet> 

to create intermediate XML:

 <root> <object> <map> <item> <src>something</src> <dest>something else</dest> </item> </map> </object> </root> 

Step2.xsl

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:template match="object"> <table> <tr> <xsl:for-each select="map/item/*"> <th> <xsl:value-of select="name()" /> </th> </xsl:for-each> </tr> <tr> <xsl:for-each select="map/item/*"> <td> <xsl:value-of select="." /> </td> </xsl:for-each> </tr> </table> </xsl:template> </xsl:stylesheet> 

to create an HTML table

 <table> <tr> <th>src</th> <th>dest</th> </tr> <tr> <td>something</td> <td>something else</td> </tr> </table> 
+6
source

Extract the value attribute into your own XML document and convert it.

You cannot do this in one XSLT without replacing substrings.

If you can control the format of an XML document, consider putting node data in CDATA rather than escaping <and>.

+1
source

All Articles