如何控制XSLT布尔渲染 [英] How to control boolean rendering in xslt
问题描述
要在&LT符合;布尔> XML-RPC的
规范一>我需要改变我的 XS:布尔
从真|虚假
到 1 | 0
。
To conform with the <boolean>
spec of Xml-RPC I need to transform my xs:boolean
from true|false
to 1|0
.
我解决了这个使用XSL:选择
I solved this using xsl:choose
<xsl:template match="Foo">
<member>
<name>Baz</name>
<value>
<boolean>
<xsl:choose>
<xsl:when test=".='true'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</boolean>
</value>
</member>
</xsl:template>
但不知道是否有控制值是如何布尔当XSLT 1.0变换呈现的不易碎的方式。
but was wondering if there is a less brittle way of controlling how boolean values are rendered when transformed with xslt 1.0.
推荐答案
使用
number(boolean(.))
由标准的XPath函数的定义的 <$c$c>number()$c$c>它产生的确切 {0,1}
时应用,分别对 {真(),假()}
By the definition of the standard XPath function number()
it produces exactly {0, 1}
when applied, respectively, on {true(), false()}
的当心!的如果你使用这种结构上的字符串的,其结果将是为假与真,因为真正的,对于字符串参数,布尔()为真当且仅当它的长度不为零。
Beware! If you use this construction on strings, the result will be true for both 'false' and 'true', because, for string parameters, boolean() is true if and only if its length is non-zero.
所以,如果你要转换的字符串的,不是布尔值,然后用这个前pression:
So, if you want to convert strings, not booleans, then use this expression:
number(not(. = 'false'))
下面是一个基于XSLT的验证
<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="text()">
<xsl:value-of select="number(not(. = 'false'))"/>
</xsl:template>
</xsl:stylesheet>
当施加在下面的XML文档这种转变
<t>
<x>true</x>
<y>false</y>
</t>
通缉,正确的结果产生
<t>
<x>1</x>
<y>0</y>
</t>
这篇关于如何控制XSLT布尔渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!