2013-03-04 58 views
-1

嗨,我有一段XML代碼。檢查項目級別

<toc-div> 
       <toc-item num="1."> 
        <toc-title>Introduction</toc-title> 
        <toc-pg>2.001</toc-pg> 
       </toc-item> 
       <toc-item num="(a)"> 
        <toc-title>Transitional arrangements</toc-title> 
        <toc-pg>2.003</toc-pg> 
       </toc-item> 
       <toc-item num="(b)"> 
        <toc-title>Classifying by numbers</toc-title> 
        <toc-pg>2.006</toc-pg> 
       </toc-item> 
       <toc-item num="2."> 
        <toc-title>Incorporation</toc-title> 
        <toc-pg>2.009</toc-pg> 
       </toc-item> 
</toc-div> 

在這裏,我想一個XSLT給我輸出1,如果TOC項目有許多事我想這是2。請讓我知道如何做到這一點。我知道我可以使用條件,但我想知道如果節點包含一個數字,如何形成該語句。

感謝

+0

你能證明你的預計產量在這種情況下?謝謝! – 2013-03-04 08:20:59

+0

感謝哥們的回覆,如果toc-item是一個數字,我想要一個名爲chapter的變量,其值爲1,如果toc-item的值不是數字,我想章節值爲2. – 2013-03-04 08:23:24

回答

0

像這樣的事情?

<xsl:template match="toc-item"> 
    <xsl:variable name="num" select="number(translate(@num, '.', ''))" /> 
    <xsl:variable name="chapter" select="1 + ($num != $num)" /> 

    .... 
</xsl:template> 

當這個XSLT使用:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="toc-item"> 
    <xsl:variable name="num" select="number(translate(@num, '.', ''))" /> 
    <!-- NaN != NaN evaluates to true(). --> 
    <xsl:variable name="chapter" select="1 + ($num != $num)" /> 

    <xsl:value-of select="concat($chapter, '&#xA;')" /> 
    </xsl:template> 
</xsl:stylesheet> 

並在您的樣品輸入運行,這將產生:

1 
2 
2 
1 
+0

感謝朋友這解決了我的問題。但是請你告訴我這裏發生了什麼。謝謝 – 2013-03-04 09:49:55

+0

設置'num'變量的行使用translate()函數從'@ num'中刪除句點,然後使用'number()'函數將該值轉換爲數字。如果它不能是數字,它將具有值'NaN'。下一行將'1'添加到布爾值'($ num!= $ num)'中。如果'$ num'是一個數字,那麼這個布爾值將是'0'(因此結果將是1 + 0 = 1)。如果'$ num'不是一個數字,那麼這個布爾值將是'1'(因此結果將是1 + 1 = 2)。 – JLRishe 2013-03-04 09:56:55

+0

非常感謝你 – 2013-03-04 10:22:33