xsl transform generating xsl stylesheet in a .net environment - c#

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>

Related

XSLT with optional template for CSV header

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

How to check line feed(&#10) after</Underline>Tag and need to replace with to <br/> tag in xslt code

I have XML file need to convert into HTML using XSLT Code
Please Refer XML File :
<?xml version="1.0" encoding="UTF-8"?><Lesson><Title>Merged Words_G1L5</Title><IntroBlock><ParaBlock><RichText>Materials: Exercise books and pencils</RichText></ParaBlock></IntroBlock><Topic><Title>Underlines – 15 minutes</Title><ParaBlock><CustomNote><SimpleBlock><RichText>
<Underline>ACHIEVE</Underline>
Fill in the Blank: go, wear, quiet, bus, I
1. the pupils are _____.
2. My parents are _____.
3. This school is a _____.
4. ____ am very tall.
5. They _____ sandals.</RichText></SimpleBlock></CustomNote></ParaBlock></Topic></Lesson>
Normally line feed(&#10) is converted into <br/> tag from my existing code
Existing Code :
1. When Check Line Feed and replace with <br/> tag:
<xsl:template match="text()">
<xsl:param name="text" select="."/>
<xsl:variable name="starttext" select="substring-before(concat($text,'
'),'
')" />
<xsl:variable name="nexttext" select="substring-after($text,'
')"/>
<xsl:if test="normalize-space($starttext)">
<xsl:value-of select="$starttext"/>
<xsl:if test="normalize-space($nexttext)">
<br />
</xsl:if>
</xsl:if>
<xsl:if test="contains($text,'
')">
<xsl:apply-templates select=".">
<xsl:with-param name="text" select="$nexttext"/>
</xsl:apply-templates>
</xsl:if>
</xsl:template>
Existing Code: 2. Related To Underline:
<xsl:template match="Underline">
<xsl:if test="text() or *">
<u>
<xsl:apply-templates/>
</u>
</xsl:if>
</xsl:template>
Existing Code : 3. Related To Rich Text:
<xsl:template match="RichText">
<xsl:if test="text() or *">
<p>
<xsl:apply-templates/>
</p>
</xsl:if>
</xsl:template>
The above mention is my existing code.
I need a solution for How to check Line Feed(&#10) when is come after </Underline> tag and </UnderLine> Tag under the <RichText>, Because normal scenario is my code working fine, but is causing only when </UnderLine> tag come under the <RichText>
anyone, please suggest how to I Check Line Feed(&#10) after </UnderLine> tag in my Code
?????.
Current Issue:
Current Issue
Required Output:
Required Output
You are outputting a <br /> under the condition of:
<xsl:if test="normalize-space($starttext)">
This condition is not true when the text starts with a line-feed - as it does in your example. If you want to preserve the opening line-feed, remove the condition.

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