XSLT with optional template for CSV header - c#

I am new to xslt and hoping to get answer for this.
I have a xslt file and use it to write a csv.
First time writing to csv, i would like to have a header.
But when I append for next content of csv, I would like to remove the header.
Below is the current xsl.
How do I make the xsl template to be optional to display?
<xsl:template name="Header" match="/">
<xsl:text>Id,Barcode,Name</xsl:text>
<xsl:apply-templates select="abc:Info/Panel" />
</xsl:template>
<xsl:template name="Panel" match="abc:Info/Panel">
<xsl:for-each select="Board/Component">
<xsl:value-of select="../../../../../#Id"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="../../../../../#Barcode"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="../../../../../#Name"/>
<xsl:text>,</xsl:text>
</xsl:for-each>
</xsl:template>

Add a xsl:param to the xslt like i.e. this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:abc="some-namespace"
version="1.0"
>
<xsl:param name="writeHeader" select="'0'"/>
<xsl:template match="/">
<xsl:if test="$writeHeader='1'">
<xsl:text>Id,Barcode,Name</xsl:text>
</xsl:if>
<xsl:apply-templates select="abc:Info/Panel" />
</xsl:template>
<xsl:template match="abc:Info/Panel">
<xsl:for-each select="Board/Component">
<xsl:value-of select="../../../../../#Id"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="../../../../../#Barcode"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="../../../../../#Name"/>
<xsl:text>,</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
And if you want to write the header, call your xsl-transformation with the param: writeHeader=1

Related

xsl transform generating xsl stylesheet in a .net environment

What xsl do I need to use myVar1 variable inside myVar2? I'm using an xsl transform to produce the new stylesheet.
<xsl:element name="variable"><xsl:attribute name="myVar1">test value</xsl:attribute> </xsl:element>
<xsl:element name="variable">
<xsl:attribute name="myVar2" />
<xsl:element name="xsl:value-of">
<xsl:attribute name="select">
/root/child[#myattr1='$myVar1']/#value <!--Help here-->
</xsl:attribute>
</xsl:element>
</xsl:element>
</xsl:template>
I think you mean
<xsl:attribute name="select">
<xsl:value-of select="concat('/root/child[#myattr1=',$myVar1,']/#value')"/>
</xsl:attribute>

replacement of multiple characters in a single element of xml using XSL

i am replacing &#10 with br tag to show new line while transforming xml using xsl. i want to replace blank spaces to its according code that may &nbsp or something else at the same time.sample code is below. please suggest for me what should i do.
while xml file may be as ------------
<?xml version="1.0" encoding="iso-8859-1"?><?xml-stylesheet type="text/xsl"
href="task.xsl"?><Nodes><sNode><Word><![CDATA[1
2.............3............4............5
3]]></Word></sNode></Nodes>
since blank spaces ommitted automatically so here ........ represents blank spaces.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="Nodes/sNode">
<tr>
<td>
<xsl:call-template name="replace-string-with-element">
<xsl:with-param name="text" select="Word"/>
<xsl:with-param name="replace" select="'
'"/>
<xsl:with-param name="with" select="'br'"/>
</xsl:call-template>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
<xsl:template name="replace-string-with-element">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:element name="{$with}"/>
<xsl:call-template name="replace-string-with-element">
<xsl:with-param name="text" select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
You could use xsl:character-map as follows:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn xs">
<xsl:character-map name="replaceChars">
<xsl:output-character character="
" string="br"/>
</xsl:character-map>
<xsl:output method="xml" version="1.0" encoding="UTF-8" use-character-maps="replaceChars" indent="yes"/>
<!-- Implement your templates -->
</xsl:stylesheet>
It is even possible to save all characters in a xsl:character-map in an external XSLT and use <xsl:import href="characterFile.xslt" />
To implement this in your stylesheet use the following XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:character-map name="replaceChars">
<xsl:output-character character="
" string="br"/>
</xsl:character-map>
<xsl:output method="html" use-character-maps="replaceChars"/>
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="Nodes/sNode">
<tr>
<td>
<xsl:value-of select="Word" />
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

How do I convert Richtext images from a standard WPF RichTextBox into HTML using XSLT?

How do I convert Richtext images from a standard WPF RichTextBox into HTML using XSLT?
Based on this answer, I have made the following XSLT file. So far, it converts Bold, Italic, Underlined, font family, font size, and font color. Now I still need it to convert images in the Richtext to HTML.
Image alignment does not have to be considered.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl x">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="x:Section[not(parent::x:Section)]">
<div>
<xsl:apply-templates select="node()"/>
</div>
</xsl:template>
<xsl:template match="x:Section">
<xsl:apply-templates select="node()"/>
</xsl:template>
<xsl:template match="x:Paragraph">
<p>
<xsl:apply-templates select="node()"/>
</p>
</xsl:template>
<xsl:template match="x:Run">
<xsl:variable name="style">
<xsl:if test="#FontStyle='Italic'">
<xsl:text>font-style:italic;</xsl:text>
</xsl:if>
<xsl:if test="#FontWeight='Bold'">
<xsl:text>font-weight:bold;</xsl:text>
</xsl:if>
<xsl:if test="contains(#TextDecorations, 'Underline')">
<xsl:text>text-decoration:underline;</xsl:text>
</xsl:if>
<xsl:if test="#FontSize != ''">
<xsl:text>font-size:</xsl:text>
<xsl:value-of select="#FontSize" />
<xsl:text>pt;</xsl:text>
</xsl:if>
<xsl:if test="#FontFamily != ''">
<xsl:text>font-family:</xsl:text>
<xsl:value-of select="#FontFamily" />
<xsl:text>;</xsl:text>
</xsl:if>
<xsl:if test="#Foreground != ''">
<xsl:text>color:#</xsl:text>
<xsl:value-of select="substring(#Foreground, 4)"/>
<xsl:text>;</xsl:text>
</xsl:if>
</xsl:variable>
<span>
<xsl:if test="normalize-space($style) != ''">
<xsl:attribute name="style">
<xsl:value-of select="normalize-space($style)"/>
</xsl:attribute>
</xsl:if>
<xsl:value-of select="text()"/>
</span>
</xsl:template>
</xsl:stylesheet>
You could add this template - include it above the final </xsl:stylesheet>
<xsl:template match="x:Image">
<img src="#Source" />
</xsl:template>

I want perform sum of product of two elements at differnt levels

I tried for following code but its not working. I have taken one string and write XSLT in it and load it XslCompiledTransform object.
<xsl:sequence select=
"sum(//Item/(cost * related_id/Item/quantity))"/>
Source XML:
<AML>
<Item>
<cost>
40
</cost>
<related_id>
<Item>
<quantity>2</quantity>
</Item>
</related_id>
</Item>
<Item>
<cost>
50
</cost>
<related_id>
<Item>
<quantity>10</quantity>
</Item>
</related_id>
</Item>
</AML>
As I said in the comments, xsl:sequence and that XPath syntax you're trying to use aren't available in XSLT 1.0 (which XslCompiledTransform uses), but you can achieve a sum of formulas by using a recursive template:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>
<xsl:template match="/">
<xsl:call-template name="SumSet">
<xsl:with-param name="items" select="/*/Item" />
</xsl:call-template>
</xsl:template>
<xsl:template name="SumSet">
<xsl:param name="items" />
<xsl:if test="$items">
<xsl:variable name="currentValue">
<xsl:apply-templates select="$items[1]" />
</xsl:variable>
<xsl:variable name="remainderSum">
<xsl:call-template name="SumSet">
<xsl:with-param name="items" select="$items[position() > 1]" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$currentValue + $remainderSum"/>
</xsl:if>
<xsl:if test="not($items)">0</xsl:if>
</xsl:template>
<xsl:template match="Item">
<xsl:value-of select="cost * related_id/Item/quantity"/>
</xsl:template>
</xsl:stylesheet>
When this is run on your input XML, the result is:
580
That would be the generic approach, but since you've mentioned that you're using XslCompiledTransform, you can use msxsl:node-set() to simplify this task a bit:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output method="text" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="values">
<xsl:apply-templates select="/*/Item" />
</xsl:variable>
<xsl:value-of select="sum(msxsl:node-set($values)/*)"/>
</xsl:template>
<xsl:template match="Item">
<itemValue>
<xsl:value-of select="cost * related_id/Item/quantity"/>
</itemValue>
</xsl:template>
</xsl:stylesheet>
This also produces the value 580 when run on your input XML.

query database via xsl file

I have an ecommerce cart which generates the XML at runtime. I have access to the XSL stylesheet which styles the actual XML.
I would like to display product category images in the menu. Is there an example of running a query within an xsl file that you could show me?
I cant see where its pulling the category data from?
can you help?
<?xml version="1.0" standalone="yes" ?>
<!-- ###################################################################################################### -->
<!-- Copyright AspDotNetStorefront.com, 1995-2009. All Rights Reserved. -->
<!-- http://www.aspdotnetstorefront.com -->
<!-- For details on this license please visit the product homepage at the URL above. -->
<!-- THE ABOVE NOTICE MUST REMAIN INTACT. -->
<!-- ###################################################################################################### -->
<package version="2.1" displayname="Categories" debug="false" includeentityhelper="true">
<PackageTransform>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aspdnsf="urn:aspdnsf" exclude-result-prefixes="aspdnsf">
<xsl:output method="html" omit-xml-declaration="yes"/>
<xsl:param name="CategoryID">
<xsl:choose>
<xsl:when test="/root/System/PageName = 'showmanufacturer.aspx' or /root/System/PageName = 'showsection.aspx' or /root/System/PageName = 'showdistributor.aspx' or /root/System/PageName = 'showvector.aspx' or /root/System/PageName = 'showgenre.aspx'">0</xsl:when>
<xsl:when test="/root/System/PageName = 'showcategory.aspx' and boolean(/root/QueryString/categoryid)">
<xsl:value-of select="/root/QueryString/categoryid"/>
</xsl:when>
<xsl:when test="(/root/System/PageName = 'showcategory.aspx' or /root/System/PageName = 'showproduct.aspx') and boolean(/root/Cookies/LastViewedEntityInstanceID) and /root/Cookies/LastViewedEntityName = 'Category'">
<xsl:value-of select="/root/Cookies/LastViewedEntityInstanceID"/>
</xsl:when>
<xsl:otherwise>
0
</xsl:otherwise>
</xsl:choose>
</xsl:param>
<xsl:param name="AncestorID">
<xsl:for-each select="/root/EntityHelpers/Category//Entity[EntityID = $CategoryID]">
<xsl:value-of select="ancestor::*/EntityID"/>
</xsl:for-each>
</xsl:param>
<xsl:param name="ParentID">
<xsl:for-each select="/root/EntityHelpers/Category//Entity[EntityID = $CategoryID]">
<xsl:value-of select="parent::*/EntityID"/>
</xsl:for-each>
</xsl:param>
<xsl:template match="/">
<xsl:element name="ul">
<xsl:attribute name="class">
<![CDATA[menuul]]>
</xsl:attribute>
<xsl:apply-templates select="/root/EntityHelpers/Category/Entity">
<xsl:with-param name="prefix" select="''"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>
<xsl:template match="Entity">
<xsl:param name="prefix"></xsl:param>
<xsl:param name="eName" select="aspdnsf:GetMLValue(Name)" />
<xsl:choose>
<xsl:when test="Published=1">
<li class="menuli">
<xsl:value-of select="$prefix" />
<!--<xsl:if test="number(ParentEntityID) != 0">
<span class="catMark">>></span>�
</xsl:if>-->
<a href="{concat('c-',EntityID,'-',SEName,'.aspx')}">
<xsl:if test="EntityID = $CategoryID or descendant::Entity/EntityID = $CategoryID">
<xsl:attribute name="class">MenuSelected</xsl:attribute>
</xsl:if>
<xsl:value-of select="$eName" disable-output-escaping="yes"/>
</a>
<xsl:if test="count(child::Entity)>0">
<ul class="submenuul">
<xsl:apply-templates select="Entity">
<xsl:with-param name="prefix" select="concat($prefix, '��')"/>
</xsl:apply-templates>
</ul>
</xsl:if>
</li>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
</PackageTransform>
</package>
If you are using ASP.NET you can use standard ASP.NET XML Control.
On page load assign to it your xml and xsl files like below:
protected void Page_Load(object sender, EventArgs e)
{
Xml1.DocumentSource = "~/App_Data/YourXmlFile.xml";
Xml1.TransformSource = "~/App_Data/YourXslStyleSheetFile.xsl";
}
You can also use DocumentContent property your xml is stored not in files but, i.e. in database.
See example here
The accepted answer is true to ASP.Net, but not to Aspdotnetstorefront. The XML file referenced in the question is only generated if debug is enabled and it should not be generated on any production environments (you will end up with write conflicts).
The XML behind the XSLT is generated for you at runtime. The category data (and other entity data) is included because you have includeentityhelper="true". EntityHelper caches category (and other entity) data to reduce DB queries. If you need data in addition to this, you should use the XMLPackage <query> node documented here (under 'SQL Queries'):
http://manual.aspdotnetstorefront.com/p-157-xml-packages.aspx
You'll also find the '' node used in most of the XMLPackages included in the default build.

Categories

Resources