Tuesday, April 20, 2010

XSLT in Any situation

XSLT: The Basics


Here are some basics XSLT commands that will be useful for the next step.

Setting a variable


Variables can only be set once. For loops consider recursive functions (a.k.a. templates).
MyVariable=5
<xsl:variable name="MyVariable">5</xsl:variable>


Reading a variable


<xsl:value-of select="@MyVariable" />

Read element


Read an element called Element from a node Node2 inside a node Node1
<xsl:value-of select="/Root/Node1/Node2/Element" />
Remember to close the tag

Setting a variable with the value of an element


<xsl:variable name="ValueOfElement">
<xsl:value-of select="/Root/Node1/Node2/Element" />
</xsl:variable>


Read attribute


Read an attribute called Attribute from a node Node4 inside a node Node3
<xsl:value-of select="/Root/Node3/Node4/@Attribute" />

Setting a variable with the value of an attribute


<xsl:variable name="ValueOfAttribute">
<xsl:value-of select="/Root/Node3/Node4/@Attribute" />
</xsl:variable>



If condition


Remember that XSLT is used to construct a XML document so the return of a value is not mandatory. The easiest way to think of it is like a print command, not like a function.
if(MyVariable>10) print MyVariable (> is a reserved char so we use &gt; instead)
<xsl:if test="@MyVariable &gt; 10">
<xsl:value-of select="@MyVariable" />
</xsl:if>


When the comparition is not against variables or numbers (string comparition for instance) the method is very similar, just delimit the string with ' '.
if(MyVariable="test") print "this is a test"
<xsl:if test="@MyVariable='10'">
this is a test
</xsl:if>


If, Elseif, Else


Does not exist an equivalent approach. We use a kind of switch instead.

if(MyVariable=10) print 1;
else if (MyVariable=10) print 10;
else print 100;


<xsl:choose>
<xsl:when test="@MyVariable &lt; 10">1</xsl:when> <!-- if --!>
<xsl:when test="@MyVariable = 10">10</xsl:when> <!-- else if --!>
<xsl:otherwise>100</xsl:otherwise> <!-- else --!>
</xsl:choose>




<Any> and <AnyAttribute>



A schema can be created without knowing the name of fields to fill. The menu Add Element has the option <Any>, and the same applies for Add Attribute (<AnyAttribute>). Like this, you can add virtually any node or attribute even after compilation. Be sure to define 0 at minimum occurrencies or it will fail while you don't have values to fill.


How to Map to <Any>


Create a Script Functoid of type "Inline XSLT". On the code area write the new tag and the desired value.

<NewField>
42
</NewField>


<NewField>
<xsl:value-of select="@MyVariable" />
</NewField>


<NewField>
<xsl:choose>
<xsl:when test="@MyVariable='X'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</NewField>


To give a namespace instead of writing directly the element, write the tag element:
<xsl:element name="ns2:ElementName">

and so on...

How to Map to <AnyAttribute>


Create a Script Functoid of type "Inline XSLT". On the code area write the new tag and the desired value between xsl:attribute tags.


<xsl:attribute name="NewAttribute">
<xsl:value-of select="$MyVariable" />
</xsl:attribute>

or any other case.

The Scripting Fuctoid is not linked to source schema, but be careful on linking Scripting Functoid to the destination schema. Point to the desired parent node.