I'm working on XSLT 1.0 version I have declared an int variable that have to increase when one (if condition) condition in true.
So can anyone tell me how to increament that variable or any soluton to that?
e.g.
<xsl:variable name="count" select="-1" />
is a variable declared. Then after that I'm checking one condition
<xsl:if test="$serviceP=$new">
// if condition id true the i have to increment the variable value (i.e count).
//and again have compare that count varibale value in if condition
<xsl:if test="$count < 2">
// runs when condition is true.
</xsl:if>
</xsl:if>
thanks in advance.
XSLT is a functional language and as such has immutable variables -- once assigned their contents cannot be changed.
You need to stop thinking procedurally. Explain your problem and many people will show you how to solve this problem without incrementing any variable.
For example, the solution can be as simple as this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
Latest Service:
<xsl:apply-templates select="service[#new='true'][1]"/>
New Services:
<xsl:apply-templates select="service[#new='true'][position() >1]"/>
</xsl:template>
</xsl:stylesheet>
On more complex situations, you may write a template that calls itself recursively, passing a specific parameter with value 1-greater than the value it has in the current template call.
Related
I run this XSL script thru Saxon-js. It updates a cost field on the main input XHTML using the XML received in the transform call using the stylesheetParams. All good. The problem is that no syntax checking is done on the param-XML (you can see what it looks like in the commented-out line). It is on the XHTML and the transform will generate an error but not on the param-XML. It just allows it to enter and then the key-function just doesn't update the XHTML. Is there a way to do the checking for properly formed XML parameter in the same transform call, or do I have to use 2 transform calls: call transform on the param-XSL to syntax-check, then call this main transform to update the XHTML?
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:output method="html" omit-xml-declaration="yes" encoding="UTF-8" include-content-type="no"/>
<xsl:param name="cost-data"/>
<!-- <supplier><product><key>3</key><pcost uptype="1"><key>21341</key><cost>12.99</cost></pcost></product></supplier> -->
<!-- </xsl:param> -->
<xsl:key name="cost" match="product/pcost[#uptype = 1]/cost" use="'cost' || ancestor::product/key"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="td[#name][key('cost', #name, fn:parse-xml($cost-data))]/text()">{key('cost', ../#name, fn:parse-xml($cost-data))}%</xsl:template>
<xsl:template match="/" name="xsl:initial-template">
<xsl:next-match/>
</xsl:template>
</xsl:stylesheet>
If your stylesheet code is
<xsl:param name="cost-data">
<supplier><product><key>3</key><pcost uptype="1"><key>21341</key>
<cost>12.99</cost></pcost></product></supplier>
</xsl:param>
then that's well-formed XML and no error should be reported.
However, fn:parse-xml($cost-data) is wrong. The value of the parameter is a node tree, not a string, and fn:parse-xml() expects lexical XML in a string. The effect of calling fn:parse-xml() on this node tree will be to first atomize the node, producing the untyped atomic value "32134112.99", and then attempt to parse this string "32134112.99" as lexical XML, which should fail.
To avoid confusion like this it's good practice to always declare the expected type of your parameters, for example as="xs:string" or as="document-node()".
If you want the default value of $cost-data to be a string containing lexical XML, try
<xsl:param name="cost-data" as="xs:string"><![CDATA[
<supplier><product><key>3</key><pcost uptype="1"><key>21341</key>
<cost>12.99</cost></pcost></product></supplier>
]]></xsl:param>
all on one line without whitespace (or if you need whitespace for readability, put it before a ">" delimiter).
Note: the literal answer to your question is: No. Saxon-JS doesn't perform this checking. The XML parser performs it, long before Saxon-JS gets to see the data.
Hi I'm using xsltproc to parse *.xsl file,
I have to store the value of
<xsl:apply-templates select="Property[#key='Direction']"/>
in to a variable,
I tried
<xsl:variable name="mName" select="Property[#key='Direction']"/>
but it does not work.
Any suggestion?
All you need to do is this...
<xsl:variable name="mName">
<xsl:apply-templates select="Property[#key='Direction']"/>
</xsl:variable>
Do bear in mind though, if your template is outputting elements (as opposed to just a string value, for example), then in XSLT 1.0 you may need to use the node-set extension function to access the elements.
I have some XML this looks like this:
<region class="TableInfo">
text
</region>
<region>
text
</region>
I want to write XSL that only preserves that part without the class="TableInfo".
I've tried a number of different ways, including:
<xsl:for-each select="region[class!='TableInfo']">
</xsl:for-each>
and
<xsl:for-each select="region">
<xsl:if test="not(class='TableInfo')">
</xsl:if>
</xsl:for-each>
and several variations thereof. it seems like it's somehow evaluating as a value rather than a string, because when I set it up as an != test, all the content gets deleted, and when I set it up as a not(), nothing gets deleted. any help?
thanks!
<xsl:for-each select="region[not(#class='TableInfo')]">
</xsl:for-each>
You forgot the # on class, so you were trying to check for class elements instead of the attributes. And apparently the != is not working as well, so I swapped in the not() function instead.
From a stylistic point, I would also suggest looking into using templates that match the region elements so you can use apply-templates instead of a for-each.
The identity rule is your friend (and of course, you need to specify the attribute class, not a "class" element):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*"><xsl:apply-templates/></xsl:template>
<xsl:template match="region[#class='TableInfo']"/>
</xsl:stylesheet>
When this transformation is applied on the provided XML (fragment wrapped into a single top element to make it a well-formed XML document):
<region>
text
</region>
Salve, folks! I have a choice field in my sharepoint page with choices like this:
(1) Go
(2) Warning
(3) Stop
Now, I want that to appear in the list as an icon instead of text. I have a working jquery script for that, but it takes to long to search through all the list for the contained text, and it would be better to use xsl anyway because it renders before it is displayed.
So how can I accomplish this in xsl? Here is as far as I have gotten, as I am only learning xsl:
<xsl:stylesheet
xmlns:x="http://www.w3.org/2001/XMLSchema"
xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
version="1.0"
exclude-result-prefixes="xsl msxsl ddwrt"
xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
xmlns:asp="http://schemas.microsoft.com/ASPNET/20"
xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:SharePoint="Microsoft.SharePoint.WebControls"
xmlns:ddwrt2="urn:frontpage:internal">
<!-- Convert the Scope Field into an icon -->
<xsl:template match="FieldRef[#Name='Scope']">
<xsl:param name="thisNode" select="."/>
<xsl:choose>
<xsl:when test="$thisNode/#Scope='(1) Go'">
<td class="statusRating1"></td>
</xsl:when>
<xsl:when test="$thisNode/#Scope='(2) Warning'">
<td class="statusRating2"></td>
</xsl:when>
<xsl:when test="$thisNode/#Scope='(3) Stop'">
<td class="statusRating3"></td>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$thisNode/#Scope" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Here is the css I want to apply:
.statusRating1{background-image: url("/_layouts/custom/images/go.png"); }
.statusRating2{background-image: url("/_layouts/custom/images/warning.png"); }
.statusRating3{background-image: url("/_layouts/custom/images/stop.png"); }
Now, I've tried this with and without mode="Choice_body" or mode="MultiChoice_body and even Text_body, and have also tried adding <xsl:apply-templates />
but it never even seems to hook. The column is definitely named "Scope". Maybe I just have to add the right mode?
In firebug, I can see that the class is never added.
[update] I have noticed that in other places where I have used the template in this fashion, that the template never "took" unless it had the correct mode defined. However, I've googled the world over and can't find the right mode to use for a choice field. I even created a question for that, here. Also, the use of thisNode is from Microsoft's examples, where you can modify field types very easily (except in the case of this here choice field).
In order to define Custom Rendering for a SPFieldChoice field in template for mode attribute should be used value body
Template for modes with names Choice_body MultiChoice_body are not defined.
So, in your case template would look like this:
<xsl:template match="FieldRef[#Name='Scope']" mode="body">
Template mode attributes defined for rendering SharePoint fields are not documented, but you could find this information in %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\LAYOUTS\XSL\fldtypes.xsl. See implementation of template PrintField for details.
Hope this helps,
Vadim
The fact that you have written a template isn't enough for this template ever to be executed.
And if it is selected for execution by the code of the XSLT built-in (default) templates, these templates don't know about any parameter named $thisNode, and don't pass such parameter to your template.
This means that the value of the$thisNode parameter when the template is initiated is the empty string -- therefore none of the xsl:when test conditions is satisfied and thus the xsl:otherwise is chosen.
Solution:
Either:
Have an explicit xsl:apply-templates in your code, that selects the nodes to be matched by the tempate, or:
Delete the <xsl:param> and replace in the code every occurence of $thisNode with ..
Question
Using XSLT 1.0, given a string with arbitrary characters how can I get back a string that meets the following rules.
First character must be one of these: a-z, A-Z, colon, or underscore
All other characters must be any of those above or 0-9, period, or hyphen
If any character does not meet the above rules, replace it with an underscore
Background
In an XSLT I'm translating some attributes into elements, but I need to be sure the attribute doesn't contain any values that can't be used in an element name. I don't care much about the integrity of the attribute being converted to the name as long as it's being converted predictably. I also don't need to compensate for every valid character in an element name (there's a bunch).
The problem I was having was with the attributes having spaces coming in, which the translate function can easily convert to underscores:
translate(#name,' ','_')
But soon after I found some of the attributes using slashes, so I have to add that now too. This will quickly get out of hand. I want to be able to define a whitelist of allowed characters, and replace any non-allowed characters with an underscore, but translate works as by replacing from a blacklist.
You could write a recursive template to do this, working through the characters in the string one by one, testing them and changing them if necessary. Something like:
<xsl:template name="normalizeName">
<xsl:param name="name" />
<xsl:param name="isFirst" select="true()" />
<xsl:if test="$name != ''">
<xsl:variable name="first" select="substring($name, 1, 1)" />
<xsl:variable name="rest" select="substring($name, 2)" />
<xsl:choose>
<xsl:when test="contains('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:_', $first) or
(not($first) and contains('0123456789.-', $first))">
<xsl:value-of select="$first" />
</xsl:when>
<xsl:otherwise>
<xsl:text>_</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="normalizeName">
<xsl:with-param name="name" select="$rest" />
<xsl:with-param name="isFirst" select="false()" />
</xsl:call-template>
</xsl:if>
</xsl:template>
However, there is shorter way of doing this if you're prepared for some hackery. First declare some variables:
<xsl:variable name="underscores"
select="'_______________________________________________________'" />
<xsl:variable name="initialNameChars"
select="'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:_'" />
<xsl:variable name="nameChars"
select="concat($initialNameChars, '0123456789.-')" />
Now the technique is to take the name and identify the characters that aren't legal by replacing all the characters in the name that are legal with nothing. You can do this with the translate() function. Once you've got the set of illegal characters that appear in the string, you can replace them with underscores using the translate() function again. Here's the template:
<xsl:template name="normalizeName">
<xsl:param name="name" />
<xsl:variable name="first" select="substring($name, 1, 1)" />
<xsl:variable name="rest" select="substring($name, 2)" />
<xsl:variable name="illegalFirst"
select="translate($first, $initialNameChars, '')" />
<xsl:variable name="illegalRest"
select="translate($rest, $nameChars, '')" />
<xsl:value-of select="concat(translate($first, $illegalFirst, $underscores),
translate($rest, $illegalRest, $underscores))" />
</xsl:template>
The only thing you have to watch out for is that the string of underscores needs to be long enough to cover all the illegal characters that might appear within a single name. Making it the same length as the longest name you're likely to encounter will do the trick (though probably you could get away with it being a lot shorter).
UPDATE:
I wanted to add to this answer. In order to generate required length underscore string you can use this template.
<!--Generate string with given number of replacement-->
<xsl:template name="gen-replacement">
<xsl:param name="n"/>
<xsl:if test="$n > 0">
<xsl:call-template name="gen-replacement">
<xsl:with-param name="n" select="$n - 1"/>
</xsl:call-template>
<xsl:text>_</xsl:text>
</xsl:if>
</xsl:template>
And call it when you need to generate underscores:
<xsl:variable name="replacement"><xsl:call-template name="gen-replacement"><xsl:with-param name="n" select="string-length($value)"/></xsl:call-template></xsl:variable>
As far as Im aware XSLT 1.0 doesnt have a builtin for this. XSLT 2.0 allows you to use regexes, though Im sure your all too aware of that.
If, on the off chance your using the MS parser, you can write .NET extension libraries that you can leverage in your XSLT and I wrote about this some months ago here.
If your using something like Saxon, I am pretty certain they also provide ways of coding your own extensions, and they may indeed have an extension of their own already, but Im unfamiliar with that engine.
Hope this helps.
As another alternative there is a string function that might work for you in the XSLT standard library. http://xsltsl.sourceforge.net/string.html#template.str:string-match