How To Escape XSL Parameters In Sharepoint Designer Site Pages



TODO:

Have you ever had data from Sharepoint display as for instance '&' rather than '&'

 

SOLUTION:

The solution is simple, just escape the XSL Parameter as follows:

<xsl:value-of select="@MyDataParam" disable-output-escaping="yes" />

 

NOTES:

There are no notes on this topic.

Properties Marked As Obsolete() Are Not Serialized



TODO:

Have you ever marked a property as Obsolete(), and noticed that it is empty whe DeSerialzie occurs.

 

SOLUTION:

It appears that Obsolete() is the same as XmlIgnore().  I found this article interesting in explaining why this happens: connect.microsoft.com

 

NOTES:

There are no notes on this topic.

How To Skip The First X Nodes During foreach Loop In XSLT



TODO:

Have you ever wanted to skip the first X nodes during a foreach loop?  In my case, I was taking a flat CSV file and turning it into a generic XML document.  I was then using XSLT to transform that document into a meaningful XML document representing Customer.  The first 2 rows of my CSV file, were header and file information only, therfore I did not want to process them.  Rather than hard code the rows to skip in a pre-processor for the flat file, I can just transform the data to my final document, and then skip X rows in XSLT.  I was using XSLT anyhow to create my customer document, so it was just as easy to put the logic there.

 

SOLUTION:

 

<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <Customers>
      <xsl:variable name="myRootNode" select="."/>
      <xsl:for-each select="$myRootNode/MyNodes/Node">
        <xsl:variable name="myNode" select="."/>
        <xsl:if test="position() > 2">
           <Customer>
             Do Your Other XSLT Stuff Here ...
           </Customer>
        </xsl:if>
      </xsl:for-each>
    </Customers>
  </xsl:template>
</xsl:stylesheet>

 

NOTES:

This will skip the first 2 nodes I encounter, which represents the first 2 header rows that I had in my document.

How to Transform XML Using XSLT And Deserialize The Result Using C#



TODO:

Have you ever wanted to load an XML Document, transform it using XSLT, and conver the result into a Class object?  This is handly so that you could take XML in various formats, use XSLT to transform it, and load it into Classes that your system knows about.

 

SOLUTION:

Dog myDog = new Dog();
XmlSerializer xmlSerial = new XmlSerializer(typeof(Dog));   //create the xmlserializer for class Dog
XmlDocument myXMLDocument = new XmlDocument();              //create the xml document
StringBuilder output = new StringBuilder();                 //create the string builder for the output
XslCompiledTransform myXslTrans = new XslCompiledTransform();  //create the xsl obj for transformation
XmlWriterSettings writerSettings = new XmlWriterSettings();    //create the writer settings object, as we want to omit XML declaration
writerSettings.OmitXmlDeclaration = true;                      //now set the omit flag to true
myXMLDocument.Load(@"c:\DogInput.xml");

//load xslt file
myXslTrans.Load(xsltfile);

//create an xmlwriter of out data
using (XmlWriter transformedData = XmlWriter.Create(output, writerSettings))
{
     //transform the document
     myXslTrans.Transform(myXMLDocument, transformedData);

     //now turn the string builder into an obj
     using (StringReader xmlString = new StringReader(output.ToString()))
     {
          myDog = (Dog)(xmlSerial.Deserialize(xmlString));
     }
}

 

NOTES:

Dog is a class i created to illustrate this task, you will put your classname in place of it.  The variable "xsltfile" is the path to your XSLT file.

How To Copy All Nodes Of An XML Document And Add An Atrribute To Each Node



TODO:

Have you ever wanted to copy all nodes of an xml document, and add an attribute to each node?

 

SOLUTION:

 

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
  <xsl:template match="*">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:attribute name="dirty">true</xsl:attribute>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

 

 

NOTES:

I had a problem, in that I had an Object that had over 50+ properties.  When I took in XML and deserialized it, I did not know what nodes were supplied.  I wanted to allow folks to pass in just the parts of the document they wanted to update, but had no notion of "dirty".  This will add a "dirty" attribute to all nodes, which allows me to easily grab only the dirty ones, and update my database accordingly.

How To Copy All Nodes And Change The Value Of Certain Nodes



TODO:

Have you ever wanted to copy an XML document, but change certain node data during the copy?

 

SOLUTION:

 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
 
  <!-- copy your input verbatim -->
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
  </xsl:template>

  <!-- Now copy book id now appending the prefix to the data -->
  <xsl:template match="/Books/Book/BookID">
    <BookID>SomeValue-<xsl:apply-templates select="@*|node()"/></BookID>
  </xsl:template>

</xsl:stylesheet>

 

 

NOTES:

There are no notes on this topic.

How To Format Money / Decimal In XSLT



TODO:

Have you ever wanted to make a nice money string from a number in XSLT?

 

SOLUTION:

 

<xsl:variable name="MyMoney" select='format-number( round(100*$root/DollarString) div 100 ,"##0.00" )' />

 

NOTES:

This creates a nice formatted string in a variable, that can then be used later.

How To Test For NaN In XSLT



TODO:

Have you ever wanted to test for NaN in XSLT?

 

SOLUTION:

<xsl:variable name="My_Variable">
     <xsl:choose>
          <xsl:when test="string(number($root/mynode))='NaN'"></xsl:when>
     <xsl:otherwise>
           <xsl:value-of select="number(string($root/mynode))" />
     </xsl:otherwise>
     </xsl:choose>
</xsl:variable>

 

NOTES:

This snippet will test for NaN.  If found, it assigns empty string to the variable, and if it is not found it just uses the node value.  Then you just use this variable later and all is well.

How To Compare Node To A String In XSLT



TODO:

Have you ever wanted to compare a node value to a string and use a new value when there is a match?

 

SOLUTION:

 

<xsl:variable name="newValue">
	<xsl:choose>
		<xsl:when test="string($myRoot/MyNode) = 'Some Value'">123456</xsl:when>
              	<xsl:otherwise>
                	<xsl:value-of select="string($myRoot/MyNode)"/>
              	</xsl:otherwise>
	</xsl:choose>
</xsl:variable>          

<MyDataNode>
	<xsl:value-of select="$newValue"/>
</MyDataNode>

 

NOTES:

The above checks the value of $myRoot.MyNode and assigns a value if there is a match.  If there is no match, the "otherwise" is reached and I use the value in the node.  I then use that value for MyDataNode.