2010-11-20 78 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>