XML - Help removing nodes being published - asp.net

Any help much appreciated on this.
I'd like to remove certain nodes from being published in a feed from an external xml file, which I've styled with xslt. Here is the feed:http://www.wcwg.info/feeds/localevents.aspx?a=00392&p=CM159EH&m=20.
The nodes I would like to remove are:
Local Events posted to WhereCanWeGo.com.00392CM15
9EH31/10/1007/11/1010001111111111111111111031
October
2010http://www.wherecanwego.com/events/signin.aspxww.wherecanwego.com/events/signin.aspx
Could anybody be kind enough to direct me how to remove these initial nodes (parameters)? They are the postcode, account number, feed URL etc.
I'm desperate to get this finished, but it's the final hurdle! Many thanks in advance for anyone who responds...
The stylesheet (fragment)
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="item">
<div class="local_events">
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="Venue"/>
<xsl:apply-templates select="Times"/>
<xsl:apply-templates select="Dates"/>
<xsl:apply-templates select="DetailsURL"/>
</div><div style="height:1px;border-bottom:1px dotted #cfcfcf;"></div>
</xsl:template>
<xsl:template match="title">
<h2><a class="title" target="_blank" rel="nofollow" href="{../DetailsURL}"><xsl:value-of select="."/></a></h2>
</xsl:template>
<xsl:template match="Venue">
<span>Location: </span>
<xsl:value-of select="."/>
<br />
</xsl:template>
<xsl:template match="Times">
<span>Details: </span>
<xsl:value-of select="."/>
<br />
</xsl:template>
<xsl:template match="Dates">
<span>Dates: </span>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="DetailsURL">
<a style="font-weight:normal;margin-left:0.5em;" target="_blank" rel="nofollow" href="{.}"><xsl:text>Full details...</xsl:text></a>
</xsl:template>

From your poorly formulated question and analyzing the feed, it seems that you want to get rid of all children of the top (LocalEvents) node that are not named item.
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/*[not(self::item)]"/>
</xsl:stylesheet>
when applied on the provided feed (http://www.wcwg.info/feeds/localevents.aspx?a=00392&p=CM159EH&m=20), produces the wanted result.
Do note: how the wanted elements are deleted (not processed, ignored) by an empty template matching them and overriding the identity rule that is used to copy the rest of the nodes "as-is".

If you already use an XSLT stylesheet that does not do exactly what you want then please post the stylesheet or a link to it.
[edit]
In your posted stylesheet change
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
to
<xsl:template match="/">
<xsl:apply-templates select="//item"/>
</xsl:template>

This has now been corrected to the following which now works, for anyone else in the same situation:
<xsl:template match="/">
<xsl:apply-templates select="/LocalEvents/item"/>
</xsl:template>
<xsl:template match="item">
<div class="local_events">
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="Venue"/>
<xsl:apply-templates select="Times"/>
<xsl:apply-templates select="Dates"/>
<xsl:apply-templates select="DetailsURL"/>
</div><div style="height:1px;border-bottom:1px dotted #cfcfcf;"></div>
</xsl:template>
<xsl:template match="title">
<h2><a class="title" target="_blank" rel="nofollow" href="{../DetailsURL}"><xsl:value-of select="."/></a></h2>
</xsl:template>
<xsl:template match="Venue">
<span>Location: </span>
<xsl:value-of select="."/>
<br />
</xsl:template>
<xsl:template match="Times">
<span>Details: </span>
<xsl:value-of select="."/>
<br />
</xsl:template>
<xsl:template match="Dates">
<span>Dates: </span>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="DetailsURL">
<a style="font-weight:normal;margin-left:0.5em;" target="_blank" rel="nofollow" href="{.}"><xsl:text>Full details...</xsl:text></a>
</xsl:template>
</xsl:stylesheet>

Other approuch. This stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:preserve-space elements="Times"/>
<xsl:template match="text()"/>
<xsl:template match="item">
<div class="local_events">
<xsl:apply-templates/>
</div>
<div style="height:1px;border-bottom:1px dotted #cfcfcf;"></div>
</xsl:template>
<xsl:template match="title">
<h2>
<a class="title" target="_blank" rel="nofollow" href="{../DetailsURL}">
<xsl:value-of select="."/>
</a>
</h2>
</xsl:template>
<xsl:template match="Venue">
<span>Location: </span>
<xsl:value-of select="."/>
<br />
</xsl:template>
<xsl:template match="Times">
<span>Details: </span>
<xsl:value-of select="."/>
<br />
</xsl:template>
<xsl:template match="Dates">
<span>Dates: </span>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="DetailsURL">
<a style="font-weight:normal;margin-left:0.5em;" target="_blank" rel="nofollow" href="{.}">
<xsl:text>Full details...</xsl:text>
</a>
</xsl:template>
</xsl:stylesheet>
Note: When doing a one to one transformation in the same hierarchy, it's enough to declare the rules for specific elements in input source and to overwrite the built-in rule for text node (output the string value) with an empty rule.

Related

Using an xslt file to turn xml file into svg?

I need to make an xslt that turns my xml file into an svg.
The can't load the desired image, but it's basically a bar graph with a title at the top that says "Fall 2018 enrollment" and the height of each bar in the graph is based on the enrollment element in the xml file (so it's not hard coded).
I've been fiddling around with it a bit but I really don't know what to do.
This is the xml file:
<?xml version="1.0" encoding="UTF-8"?>
<courses>
<course number="341" credits="4.0">
<title>Data Structures</title>
<section number="01" delivery="Classroom">
<enrollment>15</enrollment>
<room>EA244</room>
<instructor>
<first>Nicole</first>
<last>Anderson</last>
</instructor>
</section>
<section number="02" delivery="Online">
<enrollment>10</enrollment>
<instructor>
<first>Nicole</first>
<last>Anderson</last>
</instructor>
<instructor>
<first>Chi-Cheng</first>
<last>Lin</last>
</instructor>
</section>
<section number="03" delivery="Classroom">
<enrollment>12</enrollment>
<room>SH102</room>
<instructor>
<first>Mark</first>
<last>Funk</last>
</instructor>
</section>
</course>
<course number="368" credits="4.0">
<title>Introduction to Bioinformatics</title>
<section number="01" delivery="Classroom">
<enrollment>9</enrollment>
<room>AT102</room>
<instructor>
<first>Chi-Cheng</first>
<last>Lin</last>
</instructor>
<instructor>
<first>Mingrui</first>
<last>Zhang</last>
</instructor>
</section>
</course>
<course number="375" credits="4.0">
<title>Computer Systems</title>
<section number="01" delivery="ITV">
<enrollment>18</enrollment>
<room>EA244</room>
<instructor>
<first>Chi-Cheng</first>
<last>Lin</last>
</instructor>
</section>
</course>
<course number="385" credits="3.0">
<title>Applied Database Management Systems</title>
<section number="01" delivery="Classroom">
<enrollment>26</enrollment>
<room>ST108</room>
<instructor>
<first>Nicole</first>
<last>Anderson</last>
</instructor>
</section>
</course>
<course number="413" credits="3.0">
<title>Advanced Networking</title>
<section number="01" delivery="Online">
<enrollment>10</enrollment>
<instructor>
<first>Chi-Cheng</first>
<last>Lin</last>
</instructor>
</section>
</course>
</courses>

This is my attempt at an xslt.
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/2000/svg">
<xsl:output indent="yes" cdata-section-elements="style"/>
<xsl:param name="width" select="40"/><!--width of bars-->
<xsl:param name="space" select="10"/><!--space between bars-->
<xsl:variable name="max-y" select="//enrollment[not(//enrollement > .)[1]"/>
<xsl:template match="courses">
<svg>
<defs>
<style type="text/css"><![CDATA[
g.bar text {
font-family: Arial;
text-anchor: middle;
fill: white;
}
g.bar rect {
fill: blue;
}
]]>
</style>
</defs>
<g transform="translate(10,10)">
<xsl:apply-templates select="course"/>
</g>
</svg>
</xsl:template>
<xsl:template match="course">
<xsl:variable name="prev-course" select="preceding-sibling::course "/>
<g class="course" id="course-{position()}" transform="translate({
count($prev-course/section) * ($width + $space)
+ count($prev-course) * $space
})">
<xsl:apply-templates select="section" />
</g>
</xsl:template>
<xsl:template match="section">
<xsl:variable name="prev-section" select="preceding-sibling::section "/>
<g class="section" id="section-{position()}" transform="translate({
count($prev-section/enrollment) * ($width + $space)
+ count($prev-section) * $space
})">
<xsl:apply-templates select="enrollment" />
</g>
</xsl:template>
<xsl:template match="enrollment">
<xsl:variable name="idx" select="count(preceding-sibling::enrollment)" />
<xsl:variable name="pos" select="$idx * ($width + $space)" />
<g class="bar">
<rect x="{$pos}" y="{$max-y - .}" height="{.}" width="{$width}" />
<text x="{$pos + $width div 2.0}" y="{$max-y - $space}">
<xsl:value-of select="."/>
</text>
</g>
</xsl:template>
</xsl:stylesheet>
The line <xsl:variable name="max-y" select="//enrollment[not(//enrollement > .)[1]"/> lacks a closing brackets, I guess you want <xsl:variable name="max-y" select="//enrollment[not(//enrollement > .)][1]"/>.
I would probably use
<xsl:variable name="max-y">
<xsl:for-each select="//enrollment">
<xsl:sort select="." data-type="number" order="descending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
however.

XSLT 1.0 Substring then select distinct

I am quite new to xslt, so any help will be much appreciated. Below is my sample xml file.
<DocumentElement>
<Records>
<date>2014-07-01 00:00</date>
</Records>
<Records>
<date>2014-08-03 00:00</date>
</Records>
<Records>
<date>2013-08-03 00:00</date>
</Records>
<DocumentElement>
What I need is just to select distinct years from the dates.
Currently I have the below xslt which brings duplicate years.
<?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" xmlns:ms="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="years" select="DocumentElement/Records/date"/>
<ul>
<xsl:for-each select="$years">
<li>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="concat('?archive=',substring( ., 1, 4))"/>
</xsl:attribute>
<xsl:value-of select="substring( ., 1, 4)"/>
</xsl:element>
</li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
Which gives me the results below:
<ul>
<li>
2014
</li>
<li>
2014
</li>
<li>
2013
</li>
</ul>
But my expected result should be
<ul>
<li>
2014
</li>
<li>
2013
</li>
</ul>
I tried the following below but I get empty output
<?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" xmlns:ms="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="years" select="substring(DocumentElement/Records/date, 1, 4)"/>
<ul>
<xsl:for-each select="$years[not(.=preceding::*)]">
<li>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="concat('?archive=',substring( ., 1, 4))"/>
</xsl:attribute>
<xsl:value-of select="substring( ., 1, 4)"/>
</xsl:element>
</li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
Any help will be much appreciated. thanks.
Muenchian grouping is much more effective way of grouping in XSLT1.0 than using preceding/following/preceding-sibling/following-sibling axis. Try this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes"/>
<xsl:key name="Date" match="DocumentElement/Records/date" use="substring(.,1,4)"/>
<xsl:template match="/">
<ul>
<xsl:for-each select="DocumentElement/Records/date[generate-id() = generate-id(key('Date', substring(.,1,4))[1])]">
<li>
<a href="{concat('?archive=',substring(.,1,4))}">
<xsl:value-of select="substring( ., 1, 4)"/>
</a>
</li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>

Get last item in xslt

I have this piece of XSLT
<xsl:template match="/">
<xsl:variable name="boxes" select="$currentPage/boxes/descendant::nodeId [text() != '']" />
<xsl:if test="count($boxes) > 0">
<div id="boxContainer">
<ul class="noList" id="frontBoxes">
<xsl:for-each select="$boxes">
<xsl:variable name="node" select="umbraco.library:GetXmlNodeById(.)" />
<xsl:variable name="imageNodes" select="umbraco.library:Split($node/descendant::boxImage, ',')" />
<xsl:for-each select="$imageNodes/descendant::value">
<xsl:variable name="image" select="umbraco.library:GetMedia(.,0)" />
<li>
<a class="boxLink">
<xsl:attribute name="href">
<xsl:value-of select="umbraco.library:NiceUrl($node/descendant::boxLink)" />
</xsl:attribute>
<xsl:value-of select="$node/#nodeName" />
</a>
<img src="{Eksponent.CropUp:Url($image/umbracoFile, 'frontBoxes')}" width="{$image/umbracoWidth}" height="{$image/umbracoHeight}" alt="" />
<xsl:value-of select="$node/descendant::bodyText" />
</li>
</xsl:for-each>
</xsl:for-each>
</ul>
</div>
</xsl:if>
</xsl:template>
If runs through a maximum and minimum of for front page boxes with an image, text and a link.
How can I get the last item so that I can output class="last" in the last <li>
I have tried with <xsl:if test="position()"> inside the last for-each but that doesn't work as intended.
use
<xsl:choose>
<xsl:when test="position()=last()">
<!-- set class="list" -->
</xsl:when>
<xsl:otherwise>
<!-- without class="list" -->
</xsl:otherwise>
</xsl:choose>

how to create search control in tridion site?

I've created a tridion web site, now i want to add some search control like Google custom search, is there any option provide by tridion for the same?
my option :
i have to create a user contol and implement Google custom search in there then render it on my pages. or any other approach'd be better.
search control (contain text box and a search button) should be able to search text.
search results should be formatted as search engines do.
When you say Google, are you talking about the Google Search Appliance (GSA)?
Assuming that is the case: You seem to have done quite a lot of work with XSLT, so in my opinion the easiest way to do thes is have your usercontrol call the XML API for the GSA with the relevant query, and simply transform the response with XSLT. This prevents you from having to recompile the control when you need layout changes, and you can publish the XSLT from Tridion.
I use the following XSLT to generate the search results on this page http://medicine.yale.edu/search.aspx?q=test&x=0&y=0&site=YSM_School_of_Medicine
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" omit-xml-declaration="yes"/>
<xsl:param name="SITE_CATALOG"/>
<xsl:param name="SITE_NAME"/>
<xsl:param name="MAX_PAGES_IN_NAVIGATION">10</xsl:param>
<xsl:param name="TOTALNUMBER" select="/GSP/RES/M"/>
<xsl:param name="QUERY" select="/GSP/PARAM[#name='q']/#value"/>
<xsl:param name="SEARCHED_SITE_CATALOG" select="/GSP/PARAM[#name='site']/#value"/>
<xsl:template match="/">
<xsl:variable name="STARTNUMBER" select="/GSP/RES/#SN"/>
<xsl:variable name="ENDNUMBER" select="/GSP/RES/#EN"/>
<xsl:variable name="TIMER" select="/GSP/TM"/>
<!--begin search results -->
<div class="google-search-form bordered-box shaded-f5 padded-20">
<xsl:call-template name="WriteSearchForm"/>
</div>
<xsl:if test="/GSP/Spelling">
<p>Did you mean <a href="?q={/GSP/Spelling/Suggestion/#q}&site={/GSP/PARAM[#name='site']/#value}">
<xsl:value-of select="/GSP/Spelling/Suggestion/#q"/>
</a>?</p>
</xsl:if>
<div id="search-results-pagination">
<xsl:for-each select="/GSP/RES/R">
<xsl:choose>
<xsl:when test="#L='2'">
<div class="grouped-search-result">
<h4>
<a href="{U}">
<xsl:value-of select="T" disable-output-escaping="yes"/>
</a>
</h4>
<p class="search-result-body">
<xsl:value-of select="S" disable-output-escaping="yes"/>
</p>
<p class="gray95">
<span class="green">
<xsl:choose>
<xsl:when test="string-length(U) > 88">
<xsl:value-of select="substring(U, 0, 88)"/>...
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="U"/>
</xsl:otherwise>
</xsl:choose>
</span>
</p>
<p>
<a href="?as_sitesearch={HN/#U}&q={$QUERY}&site={$SITE_CATALOG}" class="more-results-link">
[ <span>
More results from <xsl:value-of select="HN/#U"/>
</span> ]
</a>
</p>
</div>
</xsl:when>
<xsl:otherwise>
<div class="pagination-element search-result">
<h4><a href="{U}">
<xsl:call-template name="WriteMimeTypeIndicator">
<xsl:with-param name="RESULT" select="."/>
</xsl:call-template>
<xsl:value-of select="T" disable-output-escaping="yes"/>
</a></h4>
<p class="search-result-body">
<xsl:value-of select="S" disable-output-escaping="yes"/>
</p>
<p>
<span class="green">
<xsl:choose>
<xsl:when test="string-length(U) > 92">
<xsl:value-of select="substring(U, 0, 92)"/>...
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="U"/>
</xsl:otherwise>
</xsl:choose>
</span> - <xsl:value-of select="HAS/C/#SZ"/>
</p>
</div>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</div>
<hr class="dashed" />
<xsl:call-template name="WritePageNavigation"/>
<!--<div id="pagination-controls" class="pagination-controls float-rt"></div>-->
<!-- end pagination-controls -->
<div class="results-footer-notes">
<p class="small-gray95">Note: This search service may return results that are not approved by Yale University, or results that may not reflect the official views of Yale University.</p>
</div>
<div class="clear"></div>
<div class="google-search-form bordered-box shaded-f5 padded-20 big-margin-top">
<xsl:call-template name="WriteSearchForm"/>
</div>
<!-- end search results -->
</xsl:template>
<xsl:template name="WritePageNavigation">
<xsl:variable name="RESULTS_ON_THIS_PAGE" select="count(/GSP/RES/R)"/>
<xsl:variable name="TOTAL_RESULT_COUNT" select="/GSP/RES/M"/>
<xsl:variable name="TOTAL_PAGES" select="ceiling($TOTAL_RESULT_COUNT div $RESULTS_ON_THIS_PAGE)"/>
<xsl:variable name="CURRENT_PAGE" select="floor((/GSP/RES/#SN) div $RESULTS_ON_THIS_PAGE) + 1"/>
<xsl:variable name="START_RESULT" select="/GSP/RES/#SN"/>
<!--
Results On This Page: <xsl:value-of select="$RESULTS_ON_THIS_PAGE"/>
<br/>
Total Results: <xsl:value-of select="$TOTAL_RESULT_COUNT"/>
<br/>
Total Pages: <xsl:value-of select="$TOTAL_PAGES"/>
<br/>
Current Page: <xsl:value-of select="$CURRENT_PAGE"/>
<br/>
Start Result: <xsl:value-of select="$START_RESULT"/>
<br/>
Start Navigation Page: <xsl:value-of select="$CURRENT_PAGE - ($MAX_PAGES_IN_NAVIGATION div 2)"/>
-->
<xsl:if test="$TOTAL_PAGES > 1">
<ul class="google-search-pagination">
<xsl:if test="$CURRENT_PAGE > 1">
<li class="pagination-previous">
Previous
</li>
</xsl:if>
<xsl:call-template name="WritePaging">
<xsl:with-param name="CURRENT_PAGE" select="$CURRENT_PAGE"/>
<xsl:with-param name="NEXT_PAGE_START" select="$RESULTS_ON_THIS_PAGE + 1"/>
<xsl:with-param name="TOTAL_RESULTS" select="$TOTAL_RESULT_COUNT"/>
<xsl:with-param name="RESULTS_ON_PAGE" select="$RESULTS_ON_THIS_PAGE"/>
<xsl:with-param name="START_PAGE">
<xsl:choose>
<xsl:when test="($CURRENT_PAGE - ($MAX_PAGES_IN_NAVIGATION div 2)) > 0">
<xsl:value-of select="$CURRENT_PAGE - ($MAX_PAGES_IN_NAVIGATION div 2)"/>
</xsl:when>
<xsl:otherwise>1</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
<xsl:with-param name="END_PAGE">
<xsl:choose>
<xsl:when test="($CURRENT_PAGE - ($MAX_PAGES_IN_NAVIGATION div 2)) > 0">
<xsl:value-of select="$MAX_PAGES_IN_NAVIGATION + $CURRENT_PAGE - floor($MAX_PAGES_IN_NAVIGATION div 2) - 1"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$MAX_PAGES_IN_NAVIGATION"/>
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>
<li class="pagination-next">
Next
</li>
</ul>
</xsl:if>
</xsl:template>
<xsl:template name="WritePaging">
<xsl:param name="NEXT_PAGE_START"/>
<xsl:param name="RESULTS_ON_PAGE"/>
<xsl:param name="TOTAL_RESULTS"/>
<xsl:param name="CURRENT_PAGE"/>
<xsl:param name="START_PAGE"/>
<xsl:param name="END_PAGE"/>
<!--
NEXT_PAGE_START: <xsl:value-of select="$NEXT_PAGE_START"/><br/>
RESULTS_ON_PAGE: <xsl:value-of select="$RESULTS_ON_PAGE"/><br/>
TOTAL_RESULTS: <xsl:value-of select="$TOTAL_RESULTS"/><br/>
CURRENT_PAGE: <xsl:value-of select="$CURRENT_PAGE"/><br/>
START_PAGE: <xsl:value-of select="$START_PAGE"/><br/>
END_PAGE: <xsl:value-of select="$END_PAGE"/><br/>
-->
<li>
<xsl:if test="$CURRENT_PAGE = $START_PAGE">
<xsl:attribute name="class">selected</xsl:attribute>
<a href="#">
<xsl:value-of select="$START_PAGE"/>
</a>
</xsl:if>
<xsl:if test="$CURRENT_PAGE != $START_PAGE">
<a href="?q={/GSP/PARAM[#name='q']/#value}&start={($START_PAGE - 1)*$RESULTS_ON_PAGE}&site={/GSP/PARAM[#name='site']/#value}">
<xsl:value-of select="$START_PAGE"/>
</a>
</xsl:if>
</li>
<xsl:if test="$START_PAGE < $END_PAGE">
<xsl:if test="$NEXT_PAGE_START < $TOTAL_RESULTS">
<xsl:call-template name="WritePaging">
<xsl:with-param name="NEXT_PAGE_START" select="$NEXT_PAGE_START + $RESULTS_ON_PAGE"/>
<xsl:with-param name="TOTAL_RESULTS" select="$TOTAL_RESULTS"/>
<xsl:with-param name="RESULTS_ON_PAGE" select="$RESULTS_ON_PAGE"/>
<xsl:with-param name="START_PAGE" select="$START_PAGE + 1"/>
<xsl:with-param name="CURRENT_PAGE" select="$CURRENT_PAGE"/>
<xsl:with-param name="END_PAGE" select="$END_PAGE"/>
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template name="WriteSearchForm">
<form name="gs" method="GET" action="#">
<p class="wrapper">
<input type="text" name="q" maxlength="256" value="{$QUERY}" class="google-search-keywords"/>
<select name="site" class="google-search-site">
<option value="{$SITE_CATALOG}">
<xsl:if test="$SEARCHED_SITE_CATALOG = $SITE_CATALOG">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
<xsl:value-of select="$SITE_NAME" disable-output-escaping="yes"/>
</option>
<option value="Yale_University">---------------</option>
<option value="YSM_School_of_Medicine">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'YSM_School_of_Medicine'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
School of Medicine
</option>
<option value="Yale_Medical_Group">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'Yale_Medical_Group'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Yale Medical Group
</option>
<option value="Yale_New_Haven_Hospital">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'Yale_New_Haven_Hospital'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Yale-New Haven Hospital
</option>
<option value="Medical_Center">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'Medical_Center'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Medical Center (all)
</option>
<option value="Medical_Library">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'Medical_Library'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Medical Library
</option>
<option value="Yale_University">---------------</option>
<option value="Yale_Libraries">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'Yale_Libraries'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Yale Libraries
</option>
<option value="Finance_and_Administration">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'Finance_and_Administration'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Finance & Admin
</option>
<option value="Human_Resources">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'Human_Resources'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Human Resources
</option>
<option value="ITS">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'ITS'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Information Technology
</option>
<option value="Yale_University">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'Yale_University'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Yale University
</option>
</select>
<button type="submit" value="submit" class="google-search-button theme-background">
<img src="/files/images/button_search_icon_translucent_sm.png" />Google Search
</button>
</p>
<p class="no-space-bottom float-rt" style="margin-right:50px;">
<a href="http://www.yale.edu/its/web/search/tips.html" class="search-tips">
<img src="/files/images/icon_question.gif" alt="" style="vertical-align:middle" /> Search Tips
</a>
</p>
<p class="no-space-bottom gray95">
Results <span class="green">
<xsl:value-of select="/GSP/RES/#SN"/>
</span> - <span class="green">
<xsl:value-of select="/GSP/RES/#EN"/>
</span> of about <span class="green"><xsl:value-of select="$TOTALNUMBER"/></span> for <strong><xsl:value-of select="$QUERY"/></strong>
</p>
</form>
</xsl:template>
<xsl:template name="WriteMimeTypeIndicator">
<xsl:param name="RESULT"/>
<xsl:choose>
<xsl:when test="not($RESULT/#MIME)"></xsl:when>
<xsl:when test="$RESULT/#MIME = 'application/msword'">[DOC] </xsl:when>
<xsl:when test="$RESULT/#MIME = 'application/octet-stream'">TEST OPTIONS</xsl:when>
<xsl:when test="$RESULT/#MIME = 'application/pdf'">[PDF] </xsl:when>
<xsl:otherwise>TODO:<xsl:value-of select="$RESULT/#MIME"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

Display XML on an ASP.NET page

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

Resources