Fil*_*urt 11 xslt casting boolean xml-rpc
要使用符合<boolean> XML-RPC的规范,我需要改变我xs:boolean的true|false到1|0.
我用xsl解决了这个问题:选择
<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>
Run Code Online (Sandbox Code Playgroud)
但是想知道在用xslt 1.0转换时是否有一种不那么脆弱的方法来控制布尔值的渲染方式.
Dim*_*hev 15
用途:
number(boolean(.))
Run Code Online (Sandbox Code Playgroud)
通过标准XPath函数的定义,number()它{0, 1}分别在应用时准确生成{true(), false()}
谨防!如果对字符串使用此构造,则对于'false'和'true',结果都为true,因为对于字符串参数,当且仅当其长度为非零时,boolean()为true.
所以,如果你想转换字符串,而不是布尔值,那么使用这个表达式:
number(not(. = 'false'))
Run Code Online (Sandbox Code Playgroud)
以下是基于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>
Run Code Online (Sandbox Code Playgroud)
当此转换应用于以下XML文档时:
<t>
<x>true</x>
<y>false</y>
</t>
Run Code Online (Sandbox Code Playgroud)
产生了想要的正确结果:
<t>
<x>1</x>
<y>0</y>
</t>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6540 次 |
| 最近记录: |