Archived entries for XSL

C# Xml Manipulation: Copy Node

Just loop through your xml structure using xPath, create a new node object, set the innertext property to value of the node to copy and then insert the value at the end of the current node set.

 
XmlDocument xdocLocalFile = new XmlDocument();
 
xdocLocalFile.Load("MyXmlFile.xml");
 
foreach (XmlNode xNodeCopy in
          xdocLocalFile.SelectNodes("/root/phunk"))
{
     XmlNode xNodeNew =
     xdocLocalFile.CreateNode(XmlNodeType.Element, "NewNodeName", null);
 
     xNodeNew.InnerText =
     xNodeCopy.SelectSingleNode("CurrentNodeName").FirstChild.Value;
 
     xNodeCopy.InsertAfter(xNodeNew, xNodeCopy.LastChild);
}
 

C# Xml Manipulation: Replace InnerText of Node

Just loop through your xml structure using xPath as usual then call the InnerText property to set the new value.

 
XmlDocument xdocLocalFile = new XmlDocument();
 
xdocLocalFile.Load("MyXmlFile.xml");
 
foreach (XmlNode xNodeReplace in
           xdocLocalFile.SelectNodes("root/phunk"))
{
           xNodeReplace.FirstChild.InnerText = "My new Value";
}
 

C# Xml Manipulation: Remove Node

To remove a node from an xml document just loop through all the nodes using xPath and then call the RemoveChild function like below.

 
XmlDocument xdocLocalFile = new XmlDocument();
 
xdocLocalFile.Load("MyXmlFile.xml");
 
foreach (XmlNode xNodeReplace in
     xdocLocalFile.SelectNodes("root/phunk/firstname"))
{
     xNodeReplace.ParentNode.ParentNode.RemoveChild(xNodeReplace.ParentNode);
}
 

C# Xml Manipulation: Create Node

First loop through your nodes and for each one create a new XmlNode giving the name of the new node and then supplying the inner text to it. Then just add it to the end of the nodes in its parent.

 
XmlDocument xdocLocalFile = new XmlDocument();
 
xdocLocalFile.Load("MyXmlFile.xml");
 
foreach (XmlNode xNodeReplace in
          xdocLocalFile.SelectNodes("root/phunk"))
{
	XmlNode xNodeNew =
        xdocLocalFile.CreateNode(XmlNodeType.Element, "NewColumnName", null);
	xNodeNew.InnerText = "Here is my new value";
 
	xNodeReplace.InsertAfter(xNodeNew, xNodeReplace.LastChild);
}
 

Limit the amount of characters in xsl select string

Okay, a nice simple one that I found today, might be useful. If you ever have to display content on your site and need to limit the amount of characters in it, such as a summary, then you can use something like the following to display what you need. This also adds '...' after the final characters.

Great if you are not in control of the data source such as twitter feeds.

 
<xsl:value-of select="concat(substring(text, 1, 100), '...')" />
 

Splitting XSL for-each data into 2 columns. Two Ways!

On my mission to find a way to split a for-each statements results into 2 columns I came across two ways. one to do horizontal, the other vertical. these are the examples. The vertical option was a modified version of the code on this page, thanks! http://overture21.com/forum/comments.php?DiscussionID=378

Display Horizontal

A      B

C      D

E      F

Code

 
<xsl:for-each select="root/minimapproducts/item">
<li style="width: 45%;float: left;">
<a>
<xsl:attribute name="href">
<xsl:value-of select="/root/gendermix/cat" />/<xsl:value-of select="@url"/>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="@name"/>
</xsl:attribute>
<xsl:value-of select="@name"/>
</a>
</li>
 
</xsl:for-each>
 

Vertial Layout

A      D

B      E

C      F

Code

 
<xsl:variable name="split" select="ceiling(count(/root/minimapproducts/item) div 2)" />
<ul class="mensLinks" style="width:45%; float:left;">
<xsl:for-each select="root/minimapproducts/item">
<xsl:if test="position() &amp;lt;= $split">
<li>
<a>
<xsl:attribute name="href">
<xsl:value-of select="/root/gendermix/cat" />/<xsl:value-of select="@url"/>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="@name"/>
</xsl:attribute>
<xsl:value-of select="@name"/>
</a>
</li>
 
</xsl:if>
</xsl:for-each>
</ul>
<ul class="mensLinks" style="width:45%; float:left;">
<xsl:for-each select="root/minimapproducts/item">
<xsl:if test="position() &amp;gt; $split">
<li>
<a>
<xsl:attribute name="href">
<xsl:value-of select="/root/gendermix/cat" />/<xsl:value-of select="@url"/>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="@name"/>
</xsl:attribute>
<xsl:value-of select="@name"/>
</a>
</li>
 
</xsl:if>
</xsl:for-each>
</ul>
 


Copyright © 2004–2009. All rights reserved.

RSS Feed. This blog is proudly powered by Wordpress and uses Modern Clix, a theme by Rodrigo Galindez.