I am trying to create a super generic XSLT that basically can only count on the table & row elements being there. I have the following sample XML I am trying to make an XSLT for:
<?xml version="1.0" encoding="UTF-8"?>
<table>
<row>
<id>5311</id>
<status>Active</status>
<id_vendor>Verizon</id_vendor>
<mobile_number>555123456</mobile_number>
</row>
<row>
<id>5312</id>
<status>Inactive</status>
<id_vendor>Sprint</id_vendor>
<mobile_number>555123457</mobile_number>
</row>
<row>
<id>5313</id>
<status>Active</status>
<id_vendor>ATT</id_vendor>
<mobile_number>555123458</mobile_number>
</row>
</table>
The problem is that I do not know what the fields are going to end up being (id, status, id_vendor, mobile_number, etc. are all subject to change.
I want to make pleasant to look at view such as:
<div style="background-color:#E3CA87;padding:.2em">
<div style="background-color:#F1E2BB;padding:.2em">
<div>id: x<div>
<div>status: active<div>
<div>id_vendor: Verizon<div>
<div>mobile_number: 555123456<div>
<div>
<div>
Here is what I have so far.. but I can't get the anonymous elements going right:
<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<body style="background-color:#eee;font-size:12pt;font-family:Arial;">
<xsl:for-each select="table/row">
<div style="background-color:#eee;padding:.5em">
<xsl:for-each select="/*">
<div style="background-color:#E3CA87;padding:.2em;border: 1px solid black">
<xsl:for-each select="./*">
<div style="background-color:#F1E2BB;padding:.2em">
<xsl:value-of select ="name(./*)"/>:
<xsl:value-of select="*" />
</div>
</xsl:for-each>
</div>
</xsl:for-each>
</div>
</xsl:for-each>
</body>
</html>
Close... but it was actually a little differnt.. just figured it out for anyone that want's to know:
<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<body style="background-color:#eee;font-size:12pt;font-family:Arial;">
<xsl:for-each select="table/row">
<div style="background-color:#eee;padding:.5em">
<xsl:for-each select=".">
<div style="background-color:#E3CA87;padding:.2em;border: 1px solid black">
<xsl:for-each select=".">
<xsl:for-each select="./*">
<div style="background-color:#F1E2BB;padding:.2em">
<xsl:value-of select="concat(local-name(.), ': ', .)" />
</div>
</xsl:for-each>
</xsl:for-each>
</div>
</xsl:for-each>
</div>
</xsl:for-each>
</body>
</html>
Use relative paths e.g. <xsl:for-each select="*"> instead of <xsl:for-each select="/*">. Then I think you want to drop the inner for-each and simply replace it with
<div style="background-color:#F1E2BB;padding:.2em">
<xsl:value-of select="concat(local-name(), ': ', .)" />
</div>
Related
I've been trying to display several sheets thanks to the more/less button with pure CSS based on this example, one of the solution suggested for Pure CSS - Read more/Read Less images. I have no problem displaying the first sheet. But for the others, it doesn't work. Each time I click on the "read more" button, only the first sheet appears. I guess that I need to change the id input ref. As you will see <xsl:for-each select="./#xml:id"> is a different sheet.
This is a xsl section:
<xsl:template match="Name of the template">
<xsl:for-each select=".">
<xsl:if test="count(./key('whatAction-subCat', #xml:id)) != 0">
<li>
<!-- some data -->
<br />
<xsl:call-template name="whatResult"/>
</li>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template name="whatResult">
<input type="checkbox" class="read-more-state" id="result" />
<div class="read-more-wrap">
<xsl:text>Read</xsl:text>
<span class="read-more-target">
<table class="table-2">
<caption>What Result</caption>
<tr>
<th>Occur. in</th>
<th>What Role</th>
<th>What Context</th>
<th>What Sphere</th>
</tr>
<xsl:for-each select="./#xml:id">
<!-- verb competition -->
<xsl:call-template name="contend"/>
<!-- verb motion -->
<xsl:call-template name="self_motion"/>
<xsl:call-template name="arriving"/>
<!-- etc -->
...
</xsl:for-each>
</table>
</span>
</div>
<label for="result" class="read-more-trigger"></label>
</xsl:template>
A few print screen for a better understanding: show less btn, show more btn, and show more of several btn.
As you will see, another concern is the space between the hidden content and the more button. I need to look to the CSS--one step at a time...
In advance, thanks so very much for your help!
Vanessa
I found the following solution:
My main concern is to display a long list of data at the request of a user. Thus, I choose the modal solution based on this example. In order to avoid the display of the same sheet, I added the modal solution to each <xsl:template name="A NAME>.
<!-- I call the templates where I add the modal solution -->
<xsl:template match="category[#corresp]">
<xsl:for-each select="./#xml:id">
<!--to display content in each sub-category, ex. 'contend', 'self_motion' -->
<xsl:if test=".!contains(., 'contend') and //ref[#corresp='#contend'] ">
<xsl:call-template name="contend"/>
</xsl:if>
<xsl:if test=".!contains(., 'self_motion') and //ref[#corresp='#self_motion'] ">
<xsl:call-template name="self_motion"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- some templates of data with the modal solution -->```
<xsl:template name="contend">
<div id="contend">
</div>
<a href="#modal-container-contend" aria-label="Open Navigation">
<xsl:text>Read the detailed structural unit for this sub-category</xsl:text>
</a>
<div id="modal-container-contend">
<div class="modal">
<table class="table-2">
<caption>What Result</caption>
<tr>
<th>Occur. in</th>
<th>What Role</th>
<th>What Context</th>
<th>What Sphere</th>
<th style="text-align: center">What Behavior</th>
</tr>
<xsl:for-each
select="//ref[#corresp = '#contend']/ancestor::node()/../following-sibling::ref[#n = '2']">
<xsl:call-template name="following-subCatVerb"/>
</xsl:for-each>
</table>
close
</div>
</div>
</xsl:template>
<xsl:template name="self_motion">
<div id="self_motion">
</div>
<a href="#modal-container-self-motion" aria-label="Open Navigation">
<xsl:text>Read the detailed structural unit for this sub-category</xsl:text>
</a>
<div id="modal-container-self-motion">
<div class="modal">
<table class="table-2">
<caption>What Result</caption>
<tr>
<th>Occur. in</th>
<th>What Role</th>
<th>What Context</th>
<th>What Sphere</th>
<th style="text-align: center">What Behavior</th>
</tr>
<xsl:for-each
select="//ref[#corresp = '#self_motion']/ancestor::node()/../following-sibling::ref[#n = '2']">
<xsl:call-template name="following-subCatVerb"/>
</xsl:for-each>
</table>
close
</div>
</div>
</xsl:template>
<!-- the same logic for same sheets of data-->
And the CSS for the modal solution:
#modal-container-contend,
#modal-container-self-motion,
#modal-container-arriving,
#modal-container-satisfying {position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.8); justify-content: center; align-items: center; display: flex;}
.modal {width: 70%; background: #fff; padding: 20px; text-align: center;}
#modal-container-contend:not(:target),
#modal-container-self-motion:not(:target),
#modal-container-arriving:not(:target),
#modal-container-satisfying:not(:target) {opacity: 0; visibility: hidden; transition: opacity 1s, visibility 1s;}
#modal-container-contend:target,
#modal-container-self-motion:target,
#modal-container-arriving:target,
#modal-container-satisfying:target {opacity: 1; visibility: visible; transition: opacity 1s, visibility 1s;}
I have been trying to display image for quite sometimes now and some of the solutions in this website does not work for me...
This is my xslt code
<xsl:template match="/">
<div class="main">
<h2>Product Catalogue</h2>
<xsl:for-each select="productdetails/product">
<div class="box">
<img src="<xsl:value-of select="product_image"/>"> </img>
<xsl:value-of select="format-number(product_Price,'0.00')" />
<h3>
<xsl:value-of select="product_Name"/>
</h3>
RM<xsl:value-of select="format-number(product_Price,'0.00')" />
<br/>
<p style="font-size:9pt; font-style: italic;">
<xsl:value-of select="product_description"/>
</p>
<br/>
Add to Cart
</div>
</xsl:for-each>
</div>
</xsl:template>
Just so you know product_image in the xml is already the imagepath thats why I did not need to write the path in the xslt code.
In XML, a tag cannot contain another tag - so this is invalid code:
<img src="<xsl:value-of select="product_image"/>"> </img>
Use either:
<img>
<xsl:attribute name="src">
<xsl:value-of select="product_image"/>
</xsl:attribute>
</img>
or:
<img src="{product_image}"/>
To understand the 2nd syntax, read about Attribute Value Templates.
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="#..OfferLetter.xslt"?>
<Doc>
<assembly>
<Heading>Offer Letter</Heading>
</assembly>
<RefNo>Ref No:0007</RefNo>
<Date></Date>
<to>To</to>
<name></name>
<city></city>
<dear>
<a>Dear Mr.</a>
<name></name>
</dear>
<p1>
<a1>
With reference to your application and the subsequent personal interview attended by you,
we are pleased to inform that you have been selected for employment in ..
(hereinafter referred to as “Company”).
We are delighted to make you the following offer for employment.
</a1>
</p1>
</Doc>
here my xslt code for that..
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:template match="/">
<html>
<body>
<h2 style ="text-align: center;">Offer Letter</h2>
<h3 style="text-align:Right; margin-right: 110px;">Ref No:K070813</h3>
<h3 style="text-align:Right ; margin-right: 224px; ">Date:</h3>
<h3 style="text-align:Left; margin-left: 50px;">To</h3>
<h3 style="text-align:Left; margin-left: 50px;">MR.</h3>
<h3 style="text-align:Left; margin-left: 50px;">Hyderabad</h3>
<br></br>
<h3 style="text-align:Left; margin-left: 50px;">Dear Mr.</h3>
<xsl:for-each select="Doc/p1">
<h3 style="text-align:Left; margin-left: 50px;">
<xsl:value-of select="a1"/>
</h3>
</xsl:for-each>
</body>
</html>
here my TransformHRML() code
public static void TransformXML()
{
// Create a resolver with default credentials.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
// transform the OfferLetter.xml file to HTML
XslTransform transform = new XslTransform();
// load up the stylesheetfile:
transform.Load(HttpContext.Current.Server.MapPath("OfferLetter.xslt"));
// perform the transformation
transform.Transform(#"..\OfferLetter.xml", #"..\OfferLetter.html", resolver);
// transform the OfferLetter.xml file to comma delimited format
// load up the stylesheet
transform.Transform(HttpContext.Current.Server.MapPath("OfferLetter.xslt"), #"..\OfferLetter.html",resolver);
}
please help me..
There are a bunch of close-tag issues, and the extraneous # in the xml file as picked up by Tim.
After tidying these up, your xslt works, but I would push you to use apply-templates and not for-each:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="OfferLetter.xslt"?>
<Doc>
<assembly>
<Heading>Offer Letter</Heading>
</assembly>
<RefNo>Ref No:0007</RefNo>
<!--etc-->
<p1>
<a1>
With reference to your application and the subsequent , ...
</a1>
</p1>
<p1>
<a1>
Another Paragraph
</a1>
</p1>
</Doc>
With the XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:template match="/">
<html>
<body>
<!--Did you mean to hard code these fields? -->
<h2 style ="text-align: center;">
<xsl:value-of select="assembly/Heading"/>
</h2>
<h3 style="text-align:Right; margin-right: 110px;">
<xsl:value-of select="assembly/RefNo"/>
</h3>
<!--etc-->
<xsl:apply-templates select="Doc/p1">
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="p1">
<h3 style="text-align:Left; margin-left: 50px;">
<xsl:value-of select="a1"/>
</h3>
</xsl:template>
</xsl:stylesheet>
If I select a DIV tag in a XHTML file with XSLT, like //*[#id='background'] How do I add a style, like a background color or other CSS styles like borders to the DIV? And if I have a list inside the DIV ID=background, how can I style the list, like removing the bullets? :)
This is really easy with XSLT. For instance, your input is:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title></title>
</head>
<body>
<div id="background">
<ul style="list-style-type: bullet">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</div>
</body>
</html>
You can use the identity transform to copy the input XML as is, and override the nodes of interest:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:x="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="x">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:template match="x:div[#id='background']">
<xsl:copy>
<xsl:attribute name="style">
<xsl:text>border-style:solid;border-width:medium</xsl:text>
</xsl:attribute>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="x:ul[ancestor::*
[name()='div' and #id='background']]/#style">
<xsl:attribute name="style">
<xsl:text>list-style-type: none</xsl:text>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
The output will be:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title></title>
</head>
<body>
<div style="border-style:solid;border-width:medium" id="background">
<ul style="list-style-type: none">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</div>
</body>
</html>
I have a string containing XML document using LinqtoXML
What is the best way of displaying it on an asp.net page as is.
I would have liked to do it the way Dennis has mentioned (using an <asp:Xml> control). But that necessitates the use of an XSL stylesheet to format the XML. The Xml control does not allow an HTML encoded string to be passed as the DocumentContent property and does not expose any method to encode the content.
As I have mentioned in the comments to Dennis' post, the defaultss.xsl contained within msxml.dll is not available publicly (and is not XSL 1.0 compliant). However, a public converted version was posted here: http://www.dpawson.co.uk/xsl/sect2/microsoft.html#d7615e227. I have tested it and it works, though a bit buggy.
Therefore, I think the simplest way would be to use an <asp:Literal> control to output pre-encoded XML to the page. The following sample demonstrates this method:
<%# Page Language="C#" %>
<%# Import Namespace="System.IO" %>
<%# Import Namespace="System.Xml" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string theXML = Server.HtmlEncode(File.ReadAllText(Server.MapPath("~/XML/myxmlfile.xml")));
lit1.Text = theXML;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<pre>
<asp:Literal ID="lit1" runat="server" />
</pre>
</div>
</form>
</body>
</html>
Use the asp:Xml control together with this Default Stylesheet as the TransformSource. This will give you a look very similar to what you see when you open an xml file in Internet Explorer complete with syntax highlighting and coloration.
I removed the expanding/collapsing node functionality because I couldn't get that to work. This file is xsl 1.0 and works great without bugs for my purposes.
To avoid any future problems with my linked file, here are the contents:
<!--
|
| XSLT REC Compliant Version of IE5 Default Stylesheet
|
| Original version by Jonathan Marsh (jmarsh#xxxxxxxxxxxxx)
| http://msdn.microsoft.com/xml/samples/defaultss/defaultss.xsl
|
| Conversion to XSLT 1.0 REC Syntax by Steve Muench (smuench#xxxxxxxxxx)
|
+-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="no" method="html"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="processing-instruction()">
<DIV class="e">
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:text><?</xsl:text>
</SPAN>
<SPAN class="pi">
<xsl:value-of select="name(.)"/>
<xsl:value-of select="."/>
</SPAN>
<SPAN class="m">
<xsl:text>?></xsl:text>
</SPAN>
</DIV>
</xsl:template>
<xsl:template match="processing-instruction('xml')">
<DIV class="e">
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:text><?</xsl:text>
</SPAN>
<SPAN class="pi">
<xsl:text>xml </xsl:text>
<xsl:for-each select="#*">
<xsl:value-of select="name(.)"/>
<xsl:text>="</xsl:text>
<xsl:value-of select="."/>
<xsl:text>" </xsl:text>
</xsl:for-each>
</SPAN>
<SPAN class="m">
<xsl:text>?></xsl:text>
</SPAN>
</DIV>
</xsl:template>
<xsl:template match="#*">
<SPAN>
<xsl:attribute name="class"><xsl:if test="xsl:*/#*"><xsl:text>x</xsl:text></xsl:if><xsl:text>t</xsl:text></xsl:attribute>
<xsl:value-of select="name(.)"/>
</SPAN>
<SPAN class="m">="</SPAN>
<B>
<xsl:value-of select="."/>
</B>
<SPAN class="m">"</SPAN>
</xsl:template>
<xsl:template match="text()">
<DIV class="e">
<SPAN class="b"> </SPAN>
<SPAN class="tx">
<xsl:value-of select="."/>
</SPAN>
</DIV>
</xsl:template>
<xsl:template match="comment()">
<DIV class="k">
<SPAN>
<!--<A STYLE="visibility:hidden" class="b" onclick="return false" onfocus="h()">-</A>-->
<SPAN class="m">
<xsl:text><!--</xsl:text>
</SPAN>
</SPAN>
<SPAN class="ci" id="clean">
<PRE>
<xsl:value-of select="."/>
</PRE>
</SPAN>
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:text>--></xsl:text>
</SPAN>
<SCRIPT>f(clean);</SCRIPT>
</DIV>
</xsl:template>
<xsl:template match="*">
<DIV class="e">
<DIV STYLE="margin-left:1em;text-indent:-2em">
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m"><</SPAN>
<SPAN>
<xsl:attribute name="class"><xsl:if test="xsl:*"><xsl:text>x</xsl:text></xsl:if><xsl:text>t</xsl:text></xsl:attribute>
<xsl:value-of select="name(.)"/>
<xsl:if test="#*">
<xsl:text> </xsl:text>
</xsl:if>
</SPAN>
<xsl:apply-templates select="#*"/>
<SPAN class="m">
<xsl:text>/></xsl:text>
</SPAN>
</DIV>
</DIV>
</xsl:template>
<xsl:template match="*[node()]">
<DIV class="e">
<DIV class="c">
<!--<A class="b" href="#" onclick="return false" onfocus="h()">-</A>-->
<SPAN class="m"><</SPAN>
<SPAN>
<xsl:attribute name="class"><xsl:if test="xsl:*"><xsl:text>x</xsl:text></xsl:if><xsl:text>t</xsl:text></xsl:attribute>
<xsl:value-of select="name(.)"/>
<xsl:if test="#*">
<xsl:text> </xsl:text>
</xsl:if>
</SPAN>
<xsl:apply-templates select="#*"/>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
</DIV>
<DIV>
<xsl:apply-templates/>
<DIV>
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:text></</xsl:text>
</SPAN>
<SPAN>
<xsl:attribute name="class"><xsl:if test="xsl:*"><xsl:text>x</xsl:text></xsl:if><xsl:text>t</xsl:text></xsl:attribute>
<xsl:value-of select="name(.)"/>
</SPAN>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
</DIV>
</DIV>
</DIV>
</xsl:template>
<xsl:template match="*[text() and not (comment() or processing-instruction())]">
<DIV class="e">
<DIV STYLE="margin-left:1em;text-indent:-2em">
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:text><</xsl:text>
</SPAN>
<SPAN>
<xsl:attribute name="class"><xsl:if test="xsl:*"><xsl:text>x</xsl:text></xsl:if><xsl:text>t</xsl:text></xsl:attribute>
<xsl:value-of select="name(.)"/>
<xsl:if test="#*">
<xsl:text> </xsl:text>
</xsl:if>
</SPAN>
<xsl:apply-templates select="#*"/>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
<SPAN class="tx">
<xsl:value-of select="."/>
</SPAN>
<SPAN class="m"></</SPAN>
<SPAN>
<xsl:attribute name="class"><xsl:if test="xsl:*"><xsl:text>x</xsl:text></xsl:if><xsl:text>t</xsl:text></xsl:attribute>
<xsl:value-of select="name(.)"/>
</SPAN>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
</DIV>
</DIV>
</xsl:template>
<xsl:template match="*[*]" priority="20">
<DIV class="e">
<DIV STYLE="margin-left:1em;text-indent:-2em" class="c">
<!--<A class="b" href="#" onclick="return false" onfocus="h()">-</A>-->
<SPAN class="m"><</SPAN>
<SPAN>
<xsl:attribute name="class"><xsl:if test="xsl:*"><xsl:text>x</xsl:text></xsl:if><xsl:text>t</xsl:text></xsl:attribute>
<xsl:value-of select="name(.)"/>
<xsl:if test="#*">
<xsl:text> </xsl:text>
</xsl:if>
</SPAN>
<xsl:apply-templates select="#*"/>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
</DIV>
<DIV>
<xsl:apply-templates/>
<DIV>
<SPAN class="b">
<xsl:call-template name="entity-ref">
<xsl:with-param name="name">nbsp</xsl:with-param>
</xsl:call-template>
</SPAN>
<SPAN class="m">
<xsl:text></</xsl:text>
</SPAN>
<SPAN>
<xsl:attribute name="class"><xsl:if test="xsl:*"><xsl:text>x</xsl:text></xsl:if><xsl:text>t</xsl:text></xsl:attribute>
<xsl:value-of select="name(.)"/>
</SPAN>
<SPAN class="m">
<xsl:text>></xsl:text>
</SPAN>
</DIV>
</DIV>
</DIV>
</xsl:template>
<xsl:template name="entity-ref">
<xsl:param name="name"/>
<xsl:text disable-output-escaping="yes">&</xsl:text>
<xsl:value-of select="$name"/>
<xsl:text>;</xsl:text>
</xsl:template>
</xsl:stylesheet>
c# solution
Thats worked for me:
string encodedXml = String.Format("<pre>{0}</pre>", HttpUtility.HtmlEncode(xmlStr));
testLb.Text = encodedXml;
Displaying XML Formatted Code Without Using an XML Control
Here is a VB.NET solution:
Public Shared Function FormatXml(ByVal xmlDoc As XmlDocument) As String
Dim sb As New StringBuilder()
`'We will use stringWriter to push the formated xml into our StringBuilder sb.`
Using stringWriter As New StringWriter(sb)
`'We will use the Formatting of our xmlTextWriter to provide our indentation.`
Using xmlTextWriter As New XmlTextWriter(stringWriter)
xmlTextWriter.Formatting = Formatting.Indented
xmlDoc.WriteTo(xmlTextWriter)
End Using
End Using
Return sb.ToString()
End Function
Use this Function to set the Text of a multiline TextBox or a Label:
XmlViewTextBox.Text = FormatXml(xmlDoc)
If you are trying to use an XML string, convert it to an XmlDocument first:
Dim xmlDoc As New XmlDocument
xmlDoc.LoadXml(xmlString)
This solution will render the XML code on the HTML page while preserving line-breaks and indents.
HTML PRE tags in a div, and just echo out the string properly escaped?
Div gives you alignment / formatting, while the PRE should preserve whitespace