Similar type of data not matching the same case - xpath

I've the below XML
<?xml version="1.0" encoding="UTF-8"?>
<body>
<para><content-style font-style="bold">1/7 7.</content-style> This is First</para>
<para><content-style font-style="bold">1/8 8.</content-style> This is second<content-style format="superscript">6</content-style></para>
</body>
Here when i'm trying to apply template using my XSLT, though the above 2 paras are of same format, first is working and the second is not.
The expected output is as below.
<div class="para"><a name="P1-7"></a><span class="phrase">1/7</span> 7. This is First
</div>
<div class="para"><a name="P1-8"></a><span class="phrase">1/8</span> 8. This is second <span class="format-superscript">6</span>
</div>
and the current output that i get is
<div class="para"><a name="P1-7"></a><span class="phrase">1/7</span> 7. This is First
</div>
<para><span class="font-style-bold">1/8 8.</span> This is second<span class="format-superscript">6</span></para>
please let me know where am i going wrong and how to fix it.
Below is my XSL
<xsl:template match="para[content-style[matches(., '(\w+)/(\w+)')]][1]">
<div class="para">
<xsl:choose>
<xsl:when test="contains(substring-after(substring-after(./content-style/text(),'/'),'/'),' ')">
<xsl:analyze-string select="substring-before(.,' ')" regex="(\w+)/(\w+)/(\w+)">
<xsl:matching-substring>
<a name="{concat('P',regex-group(1),'-',regex-group(2),'-',regex-group(3))}"/>
<span class="phrase">
<xsl:value-of select="."/>
</span>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:analyze-string select="." regex="(\w+)/(\w+)">
<xsl:matching-substring>
<a name="{concat('P',regex-group(1),'-',regex-group(2))}"/>
<span class="phrase">
<xsl:value-of select="."/>
</span>
<xsl:text>     </xsl:text>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:non-matching-substring>
</xsl:analyze-string>
<xsl:choose>
<xsl:when test="./#format">
<span class="format-{#format}">
<xsl:value-of select="substring-after(.,' ')"/>
</span>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="fontStyle">
<xsl:value-of select="concat('font-style-',#font-style)"/>
</xsl:variable>
<span class="{$fontStyle}">
<xsl:value-of select="substring-after(.,' ')"/>
</span>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:analyze-string select="." regex="(\w+)/(\w+)/(\w+)">
<xsl:matching-substring>
<a name="{concat('P',regex-group(1),'-',regex-group(2),'-',regex-group(3))}"/>
<span class="phrase">
<xsl:value-of select="."/>
</span>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:analyze-string select="." regex="(\w+)/(\w+)">
<xsl:matching-substring>
<a name="{concat('P',regex-group(1),'-',regex-group(2))}"/>
<span class="phrase">
<xsl:value-of select="."/>
</span>
<xsl:text>     </xsl:text>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:otherwise>
</xsl:choose>
</div>
</xsl:template>
<xsl:template match="content-style">
<xsl:apply-templates select="./node()[1][self::page]" mode="first"/>
<xsl:choose>
<xsl:when test="./#format">
<span class="format-{#format}">
<xsl:apply-templates/>
</span>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="fontStyle">
<xsl:value-of select="concat('font-style-',#font-style)"/>
</xsl:variable>
<span class="{$fontStyle}">
<xsl:apply-templates/>
</span>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Working DEmo
Thanks

If you don't want the first paragraph only, don't request it.
para[content-style[matches(., '(\w+)/(\w+)')]][1]
^
|
here
Remove the quantifier and insert it into another expression as indicated:
contains(substring-after(substring-after(./content-style[1]/text(),'/'),'/'),' ')
^
|
here

Related

XSLT for wrapping immediate following-siblings of certain condition

I have the following source XML
<root>
<i type="r"/>
<i type="s"/>
<i type="r"/>
<i type="r"/>
<i type="s"/>
<i type="r"/>
<i type="s"/>
</root>
My goal is to wrap together all occurrences with all immediate following-siblings (1..n) of type="r".
No wrapping for:
type="r" nodes w/o preceding type="s" node
type="s" nodes w/o immediate following type="r" node
Expected output:
<i type="r"/>
<wrap>
<i type="s"/>
<i type="r"/>
<i type="r"/>
</wrap>
<wrap>
<i type="s"/>
<i type="r"/>
</wrap>
<i type="s"/>
I have been trying to resolve the problem with the following XSLT snippet:
<xsl:for-each select="./i">
<xsl:choose>
<xsl:when test="current()[./#type='r' and count(preceding-sibling::i[#type='s']) = 0]">
<!-- Processing w/o wrap -->
<xsl:apply-templates select="current()" mode="fill"/>
</xsl:when>
<xsl:when test="current()[./#type='s' and following-sibling::i[#type='s']]">
<!-- Processing w/o wrap -->
<xsl:apply-templates select="current()" mode="fill"/>
</xsl:when>
<xsl:when test="current()[./#type='r' and count(preceding-sibling::i[#type='s']) > 0]">
<!-- Do nothing -->
</xsl:when>
<xsl:when test="current()[./#type='s' and following-sibling::i[1][#type='r']]">
<wrap>
<xsl:apply-templates select="current() | //i[#type='r' and preceding-sibling::i[#type='s']" mode="fill"/>
</wrap>
</xsl:when>
</xsl:choose>
</xsl:for-each>
I'm constantly failing in defining the right context inside .
Need to note that subsequent templates are processing all nodes. These templates have been omitted due to being irrelevant to the problem itself.
Consider the following example:
XML
<root>
<i type="r">1</i>
<i type="s">2</i>
<i type="r">3</i>
<i type="r">4</i>
<i type="s">5</i>
<i type="r">6</i>
<i type="s">7</i>
</root>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="r" match="i[#type='r']" use="generate-id(preceding-sibling::i[#type='s'][1])" />
<xsl:template match="/root">
<xsl:copy>
<xsl:copy-of select="key('r', '')"/>
<xsl:for-each select="i[#type='s']">
<xsl:variable name="r" select="key('r', generate-id())" />
<xsl:choose>
<xsl:when test="$r">
<wrap>
<xsl:copy-of select=". | $r"/>
</wrap>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<root>
<i type="r">1</i>
<wrap>
<i type="s">2</i>
<i type="r">3</i>
<i type="r">4</i>
</wrap>
<wrap>
<i type="s">5</i>
<i type="r">6</i>
</wrap>
<i type="s">7</i>
</root>
Here is a two key approach.
<xsl:key name="r" match="i[#type = 'r']" use="generate-id(preceding-sibling::i[#type = 's'][1])"/>
<xsl:key name="s" match="i[#type = 's']" use="generate-id(following-sibling::i[#type = 'r'][1])"/>
<xsl:template match="i[#type = 's']">
<xsl:variable name="id" select="generate-id(.)"/>
<xsl:choose>
<!-- Does this s have following r's?-->
<xsl:when test="key('r', $id)">
<xsl:element name="wrap">
<xsl:copy-of select="."/>
<!-- Output the r's that immedately follow this s. -->
<xsl:for-each select="key('r', $id)">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="i[#type = 'r']">
<xsl:variable name="id" select="generate-id(.)"/>
<!-- Only output r if it does not have a preceding s. -->
<xsl:if test="not(key('s', $id))">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>
<!-- Create a template to walk through the nodes on the document. -->
<xsl:template match="node()">
<xsl:apply-templates select="node()"/>
</xsl:template>

How can be get CDATA data by xslt?

I am able to parse xml to json by
http://www.armbruster-it.org/index.php/12-it/pl-sql/12-oracle-xml-and-json-goodies
But when any CDATA come it fail . Can you tell me the rule of xslt so that i can copy all data that is written in CDATA as it is in json .
> select
> packagename.itstar_xml_util.xml2json(xmltype('<ArticleDetail><![CDATA[<P
> class=MsoNormal style="MARGIN: 0in 0in 0pt"></P>]]></ArticleDetail>'))
> FROM DUAL;
This is data
<ArticleDetail><![CDATA[<P class=MsoNormal style="MARGIN: 0in 0in 0pt"></P>]]></ArticleDetail>
The xslt is
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="no" omit-xml-declaration="yes" method="text" encoding="UTF-8" media-type="text/x-json"/>
<xsl:strip-space elements="*"/>
<!--contant-->
<xsl:variable name="d">0123456789</xsl:variable>
<!-- ignore document text -->
<xsl:template match="text()[preceding-sibling::node() or following-sibling::node()]"/>
<!-- string -->
<xsl:template match="text()">
<xsl:call-template name="escape-string">
<xsl:with-param name="s" select="."/>
</xsl:call-template>
</xsl:template>
<!-- Main template for escaping strings; used by above template and for object-properties
Responsibilities: placed quotes around string, and chain up to next filter, escape-bs-string -->
<xsl:template name="escape-string">
<xsl:param name="s"/>
<xsl:text>"</xsl:text>
<xsl:call-template name="escape-bs-string">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
<xsl:text>"</xsl:text>
</xsl:template>
<!-- Escape the backslash (\) before everything else. -->
<xsl:template name="escape-bs-string">
<xsl:param name="s"/>
<xsl:choose>
<xsl:when test="contains($s,''\'')">
<xsl:call-template name="escape-quot-string">
<xsl:with-param name="s" select="concat(substring-before($s,''\''),''\\'')"/>
</xsl:call-template>
<xsl:call-template name="escape-bs-string">
<xsl:with-param name="s" select="substring-after($s,''\'')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="escape-quot-string">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Escape the double quote ("). -->
<xsl:template name="escape-quot-string">
<xsl:param name="s"/>
<xsl:choose>
<xsl:when test="contains($s,'';'')">
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="concat(substring-before($s,'';''),''"'')"/>
</xsl:call-template>
<xsl:call-template name="escape-quot-string">
<xsl:with-param name="s" select="substring-after($s,'';'')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Replace tab, line feed and/or carriage return by its matching escape code. Can''t escape backslash
or double quote here, because they don''t replace characters (; becomes \t), but they prefix
characters (\ becomes \\). Besides, backslash should be seperate anyway, because it should be
processed first. This function can''t do that. -->
<xsl:template name="encode-string">
<xsl:param name="s"/>
<xsl:choose>
<!-- tab -->
<xsl:when test="contains($s,'';'')">
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="concat(substring-before($s,'';''),''\t'',substring-after($s,'';''))"/>
</xsl:call-template>
</xsl:when>
<!-- line feed -->
<xsl:when test="contains($s,'';'')">
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="concat(substring-before($s,'';''),''\n'',substring-after($s,'';''))"/>
</xsl:call-template>
</xsl:when>
<!-- carriage return -->
<xsl:when test="contains($s,'';'')">
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="concat(substring-before($s,'';''),''\r'',substring-after($s,'';''))"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$s"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- number (no support for javascript mantise) -->
<xsl:template match="text()[not(string(number())=''NaN'')]">
<xsl:value-of select="."/>
</xsl:template>
<!-- boolean, case-insensitive -->
<xsl:template match="text()[translate(.,''TRUE'',''true'')=''true'']">true</xsl:template>
<xsl:template match="text()[translate(.,''FALSE'',''false'')=''false'']">false</xsl:template>
<!-- item:null -->
<xsl:template match="*[count(child::node())=0]">
<xsl:call-template name="escape-string">
<xsl:with-param name="s" select="local-name()"/>
</xsl:call-template>
<xsl:text>:null</xsl:text>
<xsl:if test="following-sibling::*">,</xsl:if>
<xsl:if test="not(following-sibling::*)">}</xsl:if> <!-- MBR 30.01.2010: added this line as it appeared to be missing from stylesheet -->
</xsl:template>
<!-- object -->
<xsl:template match="*" name="base">
<xsl:if test="not(preceding-sibling::*)">{</xsl:if>
<xsl:call-template name="escape-string">
<xsl:with-param name="s" select="name()"/>
</xsl:call-template>
<xsl:text>:</xsl:text>
<xsl:apply-templates select="child::node()"/>
<xsl:if test="following-sibling::*">,</xsl:if>
<xsl:if test="not(following-sibling::*)">}</xsl:if>
</xsl:template>
<!-- array -->
<xsl:template match="*[count(../*[name(../*)=name(.)])=count(../*) and count(../*)>1]">
<xsl:if test="not(preceding-sibling::*)">[</xsl:if>
<xsl:choose>
<xsl:when test="not(child::node())">
<xsl:text>null</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="child::node()"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="following-sibling::*">,</xsl:if>
<xsl:if test="not(following-sibling::*)">]</xsl:if>
</xsl:template>
<!-- convert root element to an anonymous container -->
<xsl:template match="/">
<xsl:apply-templates select="node()"/>
</xsl:template>
</xsl:stylesheet>

XSLT dynamic sort using variables

I am trying to do dynamic sort based on values passed from the dropdown list. I am setting them as variables and using them in sort.
I am using XSLT version 1.0.
Below is the code:
test.php file -
<?php
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('testxslt');
$xml = new SimpleXMLElement( "<?xml version='1.0' ?>\n".
"<?xml-stylesheet type='text/xsl' href='xml_stylesort.xsl' ?>\n".
"<items></items>");
$_POST['sort_by']='price_desc';
//for sorting
if(isset($_POST['sort_by']) && !empty($_POST['sort_by'])){
$arr = explode('_', $_POST['sort_by']);
if($arr[1] == "asc") $sortorder = 'ascending';
if($arr[1] == "desc") $sortorder = 'descending';
// xml_appendNode($objXmlDom, $objDocNode, "sortby",$arr[0]);
$xml->addChild( "sortby",$arr[0]);
$xml->addChild( "sortorder",$sortorder);
// xml_appendTextNode($objXmlDom, $objDocNode, "sortorder", $sortorder);
if($arr[0] == "price") $type = 'number';
else $type = 'text';
$xml->addChild( "sorttype", $type);
// xml_appendTextNode($objXmlDom, $objDocNode, "sorttype", $type);
}
$sql = mysql_query("select * from items") or die(mysql_error());
while($row=mysql_fetch_object($sql))
{
$item = $xml->addChild('item');
$item->addAttribute('id', $row->id);
$item->addChild('name', $row->name);
$item->addChild('qty', $row->qty);
$item->addChild('price', $row->price);
}
Header('Content-type: text/xml');
print($xml->asXML());
?>
xslt file:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="sortby">
<xsl:choose>
<xsl:when test="sortby != ''">
<xsl:value-of select="items/sortby" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'price'" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="sorttype">
<xsl:choose>
<xsl:when test="sorttype != ''">
<xsl:value-of select="items/sorttype" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'text'" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="sortorder">
<xsl:choose>
<xsl:when test="sortorder != ''">
<xsl:value-of select="items/sortorder" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'ascending'" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:template match="/">
<html>
<body>
<h2>Item List</h2>
<div style="margin:20px;">
<label>Sort by: | <xsl:value-of select="items/sortby" /> | <xsl:value-of select="items/sorttype" /> | <xsl:value-of select="items/sortorder" /> </label>
<form id="sortform" method="post">
<select name="sort_by" onchange="javascript:document.getElementById('sortform').submit();">
<option> Select </option>
<option value="price_asc">Price ASC</option>
<option value="price_desc">Price DESC</option>
<option value="name_asc">Name ASC</option>
<option value="name_desc">Name DESC</option>
</select>
</form>
</div>
<table border="1">
<tr bgcolor="#9acd32">
<th>Item</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
<xsl:for-each select="items/item">
<xsl:sort data-type="{$sorttype}" select="*[name()=$sortby]" order="{$sortorder}" />
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="qty"/></td>
<td><xsl:value-of select="qty * price"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I think the problem you are having is with the setting of your three variables at the start of your XSLT
<xsl:variable name="sortby">
<xsl:choose>
<xsl:when test="sortby != ''">
<xsl:value-of select="items/sortby" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'price'" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
The condition for the xsl:when is not correct. It is looking for a sortby element under the document node, which doesn't exist, as the root element is actually items.
It should be the following:
<xsl:when test="items/sortby != ''">
Similarly for the other two variables.
As an aside, it looks like you are writing XML with an xsl:stylesheet processing instruction to do the transformation on the client. If you are PHP you should perhaps doing the transformation of the XML on the server, then just writing out the HTML result. That way you could get PHP to pass in the sort values as parameters, rather than appending them to the XML.
It's not possible to reproduce the problem using your code. The one thing that I see is that you are trying to sort by:
select="#*[name()=$sortby]"
but your product nodes have no attributes. Perhaps you mean to sort by:
select="*[name()=$sortby]"
?

XSL sort by title

I don't see the solution and hope someone can help me. I want to sort the result by title. But I don't know where and how to put the sort into the XSL file.
Can somebody help me?
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="TotalResults" />
<xsl:template match="NumberOfResults" />
<xsl:template name="DisplayString">
<xsl:param name="str" />
<xsl:if test='string-length($str) > 0'>
<xsl:value-of select="$str" />
</xsl:if>
</xsl:template>
<xsl:template name="HitHighlighting">
<xsl:param name="hh" />
<xsl:apply-templates select="$hh"/>
</xsl:template>
<xsl:template name="ResultPreviewToolTip">
<xsl:param name="contentclass" />
<xsl:param name="picturethumbnailurl" />
<xsl:param name="url" />
<xsl:param name="title" />
<xsl:param name="hithighlightedsummary" />
<xsl:param name="description" />
<xsl:param name="version" />
<xsl:choose>
<xsl:when test="$contentclass[. = 'STS_ListItem_PictureLibrary'] and $picturethumbnailurl[. != '']">
<div>
<a href="{$url}" title="{$title}">
<img src="{$picturethumbnailurl}" alt="" />
</a>
</div>
</xsl:when>
<xsl:when test="contains( $url, 'jpg' ) or contains( $url, 'jpeg' ) or contains( $url, 'gif' ) or contains( $url, 'JPG' ) or contains( $url, 'JPEG' ) or contains( $url, 'GIF' )">
<div>
<img src="/_layouts/AssetUploader.aspx?Size=Medium&ImageUrl={$url}" alt="" />
</div>
</xsl:when>
<xsl:otherwise>
<div>
<xsl:choose>
<xsl:when test="$hithighlightedsummary[. != '']">
<b>Preview:</b>
<br/>
<xsl:call-template name="HitHighlighting">
<xsl:with-param name="hh" select="$hithighlightedsummary" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$description[. != '']">
<b>Preview:</b>
<br/>
<xsl:value-of select="$description"/>
</xsl:when>
<xsl:otherwise>
No preview available
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="DisplayString">
<xsl:with-param name="str" select="$version" />
<xsl:with-param name="text" select="'Version: '" />
<xsl:with-param name="stringcolor" select="'#808080'" />
</xsl:call-template>
</div >
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="Result">
<xsl:variable name="tdClass">
<xsl:if test="(position() mod 2 = 0)">
<xsl:value-of select="'even'" />
</xsl:if>
<xsl:if test="(position() mod 2 = 1)">
<xsl:value-of select="'odd'" />
</xsl:if>
</xsl:variable>
<tr>
<td class="{$tdClass}">
<a href="#" class="tooltip">
<img>
<xsl:attribute name="src">
<xsl:value-of select="imageurl"/>
</xsl:attribute>
</img>
<span>
<xsl:call-template name="ResultPreviewToolTip">
<xsl:with-param name="contentclass" select="contentclass" />
<xsl:with-param name="description" select="description" />
<xsl:with-param name="hithighlightedsummary" select="hithighlightedsummary" />
<xsl:with-param name="picturethumbnailurl" select="picturethumbnailurl" />
<xsl:with-param name="title" select="title" />
<xsl:with-param name="url" select="url" />
<xsl:with-param name="version" select="version" />
</xsl:call-template>
</span>
</a>
</td>
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="url" disable-output-escaping="yes" />
</xsl:attribute>
<xsl:value-of select="filename"/>
</a>
</td>
<td class="{$tdClass}">
<a>
<xsl:attribute name="href">
<xsl:value-of select="url" disable-output-escaping="yes" />
</xsl:attribute>
<xsl:choose>
<xsl:when test="doctitle != ''">
<xsl:value-of select="doctitle"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="title"/>
</xsl:otherwise>
</xsl:choose>
</a>
</td>
<td class="{$tdClass}">
<xsl:choose>
<xsl:when test="docauthor != ''">
<xsl:value-of select="docauthor"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="author"/>
</xsl:otherwise>
</xsl:choose>
</td>
<td class="{$tdClass}">
<xsl:value-of select="revisiondate" />
</td>
<td class="{$tdClass}">
<xsl:value-of select="doclanguage"/>
</td>
<td class="{$tdClass}">
<a>
<xsl:attribute name="href">
<xsl:value-of select="sitename" disable-output-escaping="yes" />
</xsl:attribute>
<img src="/_layouts/images/breadcrumbbutton.png" style="border-style: none" />
</a>
<xsl:call-template name="ShowVersionHistory" />
</td>
</tr>
</xsl:template>
<xsl:template name="ShowVersionHistory">
<!-- First, encode Url -->
<xsl:variable name="EncodedUrl">
<xsl:value-of disable-output-escaping="yes" select="ddwrt:UrlEncode(url)" />
</xsl:variable>
<!-- does only work for office docuemnts -->
<xsl:if test="string-length(serverredirectedurl) > 0">
<!-- get web url from office web app link -->
<xsl:variable name="WebUrl">
<xsl:value-of select="substring-before(serverredirectedurl, '_layouts')"/>
</xsl:variable>
<!-- create link -->
<xsl:variable name="FinalLink">
<xsl:value-of select="$WebUrl"/>
<xsl:text>_layouts/Versions.aspx?FileName=</xsl:text>
<xsl:value-of select="$EncodedUrl"/>
</xsl:variable>
<a href="{$FinalLink}" target="_blank" Title="Version History">
<img src="/_layouts/images/versions.gif" style="border-style: none" />
</a>
</xsl:if>
</xsl:template>
<xsl:template match="/">
<table class="searchresult">
<tr>
<th width="18"></th>
<th>Filename</th>
<th>Title</th>
<th>Author</th>
<th>Revision Date</th>
<th>Language</th>
<th>Actions</th>
</tr>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="TotalResults" />
<xsl:template match="NumberOfResults" />
<xsl:template name="DisplayString">
<xsl:param name="str" />
<xsl:if test='string-length($str) > 0'>
<xsl:value-of select="$str" />
</xsl:if>
</xsl:template>
<xsl:template name="HitHighlighting">
<xsl:param name="hh" />
<xsl:apply-templates select="$hh"/>
</xsl:template>
<xsl:template name="ResultPreviewToolTip">
<xsl:param name="contentclass" />
<xsl:param name="picturethumbnailurl" />
<xsl:param name="url" />
<xsl:param name="title" />
<xsl:param name="hithighlightedsummary" />
<xsl:param name="description" />
<xsl:param name="version" />
<xsl:choose>
<xsl:when test="$contentclass[. = 'STS_ListItem_PictureLibrary'] and $picturethumbnailurl[. != '']">
<div>
<a href="{$url}" title="{$title}">
<img src="{$picturethumbnailurl}" alt="" />
</a>
</div>
</xsl:when>
<xsl:when test="contains( $url, 'jpg' ) or contains( $url, 'jpeg' ) or contains( $url, 'gif' ) or contains( $url, 'JPG' ) or contains( $url, 'JPEG' ) or contains( $url, 'GIF' )">
<div>
<img src="/_layouts/AssetUploader.aspx?Size=Medium&ImageUrl={$url}" alt="" />
</div>
</xsl:when>
<xsl:otherwise>
<div>
<xsl:choose>
<xsl:when test="$hithighlightedsummary[. != '']">
<b>Preview:</b>
<br/>
<xsl:call-template name="HitHighlighting">
<xsl:with-param name="hh" select="$hithighlightedsummary" />
</xsl:call-template>
</xsl:when>
<xsl:when test="$description[. != '']">
<b>Preview:</b>
<br/>
<xsl:value-of select="$description"/>
</xsl:when>
<xsl:otherwise>
No preview available
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="DisplayString">
<xsl:with-param name="str" select="$version" />
<xsl:with-param name="text" select="'Version: '" />
<xsl:with-param name="stringcolor" select="'#808080'" />
</xsl:call-template>
</div >
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="Result">
<xsl:variable name="tdClass">
<xsl:if test="(position() mod 2 = 0)">
<xsl:value-of select="'even'" />
</xsl:if>
<xsl:if test="(position() mod 2 = 1)">
<xsl:value-of select="'odd'" />
</xsl:if>
</xsl:variable>
<tr>
<td class="{$tdClass}">
<a href="#" class="tooltip">
<img>
<xsl:attribute name="src">
<xsl:value-of select="imageurl"/>
</xsl:attribute>
</img>
<span>
<xsl:call-template name="ResultPreviewToolTip">
<xsl:with-param name="contentclass" select="contentclass" />
<xsl:with-param name="description" select="description" />
<xsl:with-param name="hithighlightedsummary" select="hithighlightedsummary" />
<xsl:with-param name="picturethumbnailurl" select="picturethumbnailurl" />
<xsl:with-param name="title" select="title" />
<xsl:with-param name="url" select="url" />
<xsl:with-param name="version" select="version" />
</xsl:call-template>
</span>
</a>
</td>
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="url" disable-output-escaping="yes" />
</xsl:attribute>
<xsl:value-of select="filename"/>
</a>
</td>
<td class="{$tdClass}">
<a>
<xsl:attribute name="href">
<xsl:value-of select="url" disable-output-escaping="yes" />
</xsl:attribute>
<xsl:choose>
<xsl:when test="doctitle != ''">
<xsl:value-of select="doctitle"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="title"/>
</xsl:otherwise>
</xsl:choose>
</a>
</td>
<td class="{$tdClass}">
<xsl:choose>
<xsl:when test="docauthor != ''">
<xsl:value-of select="docauthor"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="author"/>
</xsl:otherwise>
</xsl:choose>
</td>
<td class="{$tdClass}">
<xsl:value-of select="revisiondate" />
</td>
<td class="{$tdClass}">
<xsl:value-of select="doclanguage"/>
</td>
<td class="{$tdClass}">
<a>
<xsl:attribute name="href">
<xsl:value-of select="sitename" disable-output-escaping="yes" />
</xsl:attribute>
<img src="/_layouts/images/breadcrumbbutton.png" style="border-style: none" />
</a>
<xsl:call-template name="ShowVersionHistory" />
</td>
</tr>
</xsl:template>
<xsl:template name="ShowVersionHistory">
<!-- First, encode Url -->
<xsl:variable name="EncodedUrl">
<xsl:value-of disable-output-escaping="yes" select="ddwrt:UrlEncode(url)" />
</xsl:variable>
<!-- does only work for office docuemnts -->
<xsl:if test="string-length(serverredirectedurl) > 0">
<!-- get web url from office web app link -->
<xsl:variable name="WebUrl">
<xsl:value-of select="substring-before(serverredirectedurl, '_layouts')"/>
</xsl:variable>
<!-- create link -->
<xsl:variable name="FinalLink">
<xsl:value-of select="$WebUrl"/>
<xsl:text>_layouts/Versions.aspx?FileName=</xsl:text>
<xsl:value-of select="$EncodedUrl"/>
</xsl:variable>
<a href="{$FinalLink}" target="_blank" Title="Version History">
<img src="/_layouts/images/versions.gif" style="border-style: none" />
</a>
</xsl:if>
</xsl:template>
<xsl:template match="/">
<table class="searchresult">
<tr>
<th width="18"></th>
<th>Filename</th>
<th>Title</th>
<th>Author</th>
<th>Revision Date</th>
<th>Language</th>
<th>Actions</th>
</tr>
<xsl:apply-templates />
</table>
</xsl:template>
</xsl:stylesheet>
You have to add <xsl:sort> (see: w3schools: xsl:sort Element).
It should be added to your xsl:apply-templates element (or if you would use xsl:for-each, then there).

How do I test if image exists for a field in xslt?

I have a form where a field is an image which is uploaded, which works fine. I now want to check to see if an image exists for that field and if not display a default no image
I have for now
<xsl:if test="fields/field_item_main_image/data/#original = ''"><span class="noimage"></span></xsl:if><xsl:copy-of select="fields/field_item_main_image/data" />
The span for no image simply provides a no image png file to be shown inplace of an image when none exists, but this is not working as the default image is not being shown, even though the field has no image assigned to it.
Looking at the source code the result is
<data></data>
What am I doing wrong?
ADDITIONAL INFO
I'd like to add more info but really not sure what you want me to add to help me... let me know what is needed and I will see if I can get that info for you to help me.
The relevant class style is
.noimage {display:block;width:100px;height:100px;background-image: url(../images/no-image-available.png);}
This is related to SobiPro a Joomla Compontent
Here is the form code
<div>
<xsl:for-each select="entry/fields/*">
<xsl:if test="( name() != 'save_button' ) and ( name() != 'cancel_button' )">
<xsl:variable name="fieldId">
<xsl:value-of select="data/*/#id" />
</xsl:variable>
<div id="{$fieldId}Container">
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="position() mod 2">spFormRowEven</xsl:when>
<xsl:otherwise>spFormRowOdd</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:if test="string-length( fee )">
<div class="spFormPaymentInfo">
<input name="{$fieldId}Payment" id="{$fieldId}Payment" value="" type="checkbox" class="SPPaymentBox" onclick="SP_ActivatePayment( this )"/>
<label for="{$fieldId}Payment">
<xsl:value-of select="fee_msg"></xsl:value-of><br/>
</label>
<div style="margin-left:20px;">
<xsl:value-of select="php:function( 'SobiPro::Txt', 'TP.PAYMENT_ADD' )" />
</div>
</div>
</xsl:if>
<div class="spFormRowLeft">
<label for="{$fieldId}">
<xsl:choose>
<xsl:when test="string-length( description )">
<xsl:variable name="desc">
<xsl:value-of select="description" />
</xsl:variable>
<xsl:variable name="label">
<xsl:value-of select="label" />
</xsl:variable>
<xsl:value-of select="php:function( 'SobiPro::Tooltip', $desc, $label )" disable-output-escaping="yes"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="label"/>
</xsl:otherwise>
</xsl:choose>
</label>
</div>
<div class="spFormRowRight">
<xsl:choose>
<xsl:when test="data/#escaped">
<xsl:value-of select="data" disable-output-escaping="yes"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="data/*" disable-output-escaping="yes"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text> </xsl:text><xsl:value-of select="#suffix"/>
</div>
</div>
</xsl:if>
</xsl:for-each>
</div>
As part of the input one of my fields is an image field, it is that field I am wanting to check to see if a user has uploaded an image to that field, if they have then show that image, if not then show a default no image.
Does any of this help?
GW
If the default image for that field does not change, then I would do all of this on the client side.
Take a look at this answer to do that.

Resources