符合 <boolean>
Xml-RPC的规范 我需要转变我的 xs:boolean
从 true|false
至 1|0
。
我用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转换时是否有一种不那么脆弱的方法来控制布尔值的渲染方式。
使用:
number(boolean(.))
通过标准XPath函数的定义 number()
它产生的确切 {0, 1}
分别应用时 {true(), false()}
谨防! 如果你使用这种结构 字符串,结果会 对于'假'和'真'都是如此,因为,对于字符串参数, 当且仅当其长度为非零时,boolean()为true。
所以,如果你想转换 字符串而不是布尔,然后使用这个表达式:
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>