xml - set size of image with xsl -
i have xml contains img tag
<xml> <img src="/path/to/file.jpg" orginalwidth="150" /> </xml>
i want have:
<img src="/paht/to/file.jpg" size=size />
where size minimum of orginalsize , 100px
this transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:param name="pmaxsize" select="100"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="@orginalwidth"> <xsl:attribute name="size"> <xsl:value-of select=".*not(. > $pmaxsize) + $pmaxsize*(. > $pmaxsize)"/> <xsl:text>px</xsl:text> </xsl:attribute> </xsl:template> </xsl:stylesheet>
when performed on provided xml document:
<xml> <img src="/path/to/file.jpg" orginalwidth="150" /> </xml>
produces wanted result:
<xml> <img src="/path/to/file.jpg" size="100px"/> </xml>
when applied on following xml document:
<xml> <img src="/path/to/file.jpg" orginalwidth="99" /> </xml>
the result again wanted , correct one:
<xml> <img src="/path/to/file.jpg" size="99px"/> </xml>
explanation:
in xpath 1.0 boolean value, when used number converted true()
1
, false()
0
.
therefore, expression:
.*not(. > $pmaxsize) + $pmaxsize*(. > $pmaxsize)
evaluates .
if .
less or equal $pmaxsize
, $pmaxsize
otherwize.
.
value of current node interpreted number.
Comments
Post a Comment