2010-11-20 79 views
2

我有类型xs定义的元素的XML架构:布尔是这样的:比较XSLT中的xs:布尔值吗?

<xs:element name="useful" 
       minOccurs="1" maxOccurs="1" 
       type="xs:boolean"/> 

我试图用一个选择/时/否则XSL阻止特定输出一些基于价值是,是这样的:

<xsl:choose> 
    <xsl:when test="@useful = 0">No</xsl:when> 
    <xsl:otherwise>Yes</xsl:otherwise> 
</xsl:choose> 

我试过为每相比变化我能想到的(使用真(),假(),1,0,去掉@,使用// @),它总是打印出'是'。我在这里做错了什么?

EDIT(由马丁Honnen要求):

这里是一个的使用XML的示例:

<?xml version="1.0"?> 
<functions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:noNamespaceSchemaLocation="functions.xsd"> 
    <function> 
     <name>func1</name> 
     <useful>0</useful> 
    </function> 

    <function> 
     <name>func2</name> 
     <useful>1</useful> 
    </function> 
</functions> 

这会导致使用XSL问题:模板我想,所以我使用XSL :for-each循环遍历每个函数,并尝试输出特定于每个有用标记的内容。 xsl:模板只能用于xslt的顶层,对吗?

完整的XSLT文件

<?xml version='1.0' ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <html><body> 
     <h2>Functions</h2> 
      <table border="1"> 
       <tr> 
        <th>Functions</th> 
        <th>Useful</th> 
       </tr> 
       <xsl:for-each select="functions/function"> 
        <tr> 
        <td> 
         <xsl:value-of select="name"/> 
        </td> 
        <td> 
         <xsl:choose> 
          <xsl:when test="@useful = 0">No</xsl:when> 
          <xsl:otherwise>Yes</xsl:otherwise> 
         </xsl:choose> 
        </td> 
        </tr> 
       </xsl:for-each> 
      </table> 
     </body></html> 
     </xsl:template> 
</xsl:stylesheet> 
+0

好问题,+1。查看我的答案获得完整的解决方案。 – 2010-11-20 17:25:55

回答

3

我觉得架构定义一个元素,但是你正在测试一个属性的值 - 这可能是你的主要问题。

<xsl:choose> 
    <xsl:when test="@useful = 0">No</xsl:when> 
    <xsl:otherwise>Yes</xsl:otherwise> 
</xsl:choose> 

我已经想尽变化我能想到的 对于比较(使用 true()false()10, 去除@,使用//@),它总是 打印出“是'。我在这里做错了什么?

很可能当前节点不是具有名为useful的属性的元素。

以下是如何在XSLT 1中工作。0与useful元素类型与由模式定义:

<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="useful/text()[. = 'true' or .='1']"> 
    Yes 
</xsl:template> 

<xsl:template match="useful/text()[. = 'false' or .='0']"> 
    No 
</xsl:template> 
</xsl:stylesheet> 

当该变换被应用到下面的XML文档

<t> 
    <useful>true</useful> 
    <useful>false</useful> 
    <useful>1</useful> 
    <useful>0</useful> 
</t> 

有用,正确的结果产生

<t> 
    <useful> 
    Yes 
</useful> 
    <useful> 
    No 
</useful> 
    <useful> 
    Yes 
</useful> 
    <useful> 
    No 
</useful> 
</t> 

在XS LT 2.0(模式感知处理器),就需要导入架构(使用<xsl:import-schema>),那么你可以简单地使用:

('no', 'yes')[number(useful)+1] 

在当前节点是useful元素的父。

1

您的架构定义了一个名为“有用”的元素,但该模式是不相关的水平,除非你试图使用模式感知XSLT 2.0。

在XPath中执行@useful选择上下文节点名称的属性,该属性与您拥有的模式没有任何意义,因为它定义了一个元素。 所以我们展示你的实例XML的XSLT是处理的话,我们可以给你一些XSLT代码:

<xsl:template match="useful"> 
    <xsl:choose> 
    <xsl:when test=". = 'false' or . = 0">No</xsl:when> 
    <xsl:otherwise>Yes</xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

随着感知模式的XSLT 2.0,你可以做

<xsl:template match="useful"> 
    <xsl:value-of select="if (data(.)) then 'Yes' else 'No'"/> 
</xsl:template>