我有一个包含img标签集大小与XSL
<xml>
<img src="/path/to/file.jpg" orginalwidth="150" />
</xml>
我想有一个XML:
<img src="/paht/to/file.jpg" size=SIZE />
其中size是orginalsize的最小和100px的
我有一个包含img标签集大小与XSL
<xml>
<img src="/path/to/file.jpg" orginalwidth="150" />
</xml>
我想有一个XML:
<img src="/paht/to/file.jpg" size=SIZE />
其中size是orginalsize的最小和100px的
这种转变:
<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>
所提供的XML文档进行时:
<xml>
<img src="/path/to/file.jpg" orginalwidth="150" />
</xml>
产生通缉的结果:
<xml>
<img src="/path/to/file.jpg" size="100px"/>
</xml>
当在下面的XML文档施加:
<xml>
<img src="/path/to/file.jpg" orginalwidth="99" />
</xml>
结果是再次有用和正确一个:
<xml>
<img src="/path/to/file.jpg" size="99px"/>
</xml>
说明:
在XPath 1.0任何布尔值作为数字使用时,从true()
转换为1
和从false()
到0
。
因此,表达式:
.*not(. > $pmaxSize) + $pmaxSize*(. > $pmaxSize)
评估为.
如果.
小于或等于$pmaxSize
并$pmaxSize
otherwize。
.
是将当前节点的值解释为数字。
+1使用字符串数字的更好和紧凑的表达式 – 2010-10-07 16:08:59
偏离主题。 'xquery'问题越来越多。也许你想重温他们。 – 2010-10-07 16:13:33
是否有可能找到原始大小的图像没有额外的参数(原始大小)。我的意思是总是以正常尺寸显示图像,除非其大于最大值? – liysd 2010-10-07 17:50:43
使用XSLT 1.0(XSLT 2.0可能会给你其他选择,但不知道你是否可以使用它):
<img src="{@src}">
<xsl:choose>
<xsl:when test="@originalsize < 100">
<xsl:attribute name="size"><xsl:value-of select="@originalsize"/></xsl:attribute>
<xsl:otherwise>
<xsl:attribute name="size">100</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</img>
select不能在xsl:属性中 – liysd 2010-10-07 14:10:31
无论何时可以使用文字结果元素属性值templaes,请使用它。
<img src="{@src}" size="{substring('100',
1 div (@orginalwidth > 100))
}{substring(@orginalwidth,
1 div not(@orginalwidth > 100))
}px"/>
编辑:最小不是最大的,对不起。
好问题(+1)。查看我的答案,获取完整简短的解决方案并解释其工作原理。 – 2010-10-07 15:35:21