« 2006-04巴里島五日遊 | 回到主頁面 | links for 2006-04-19 »

April 19, 2006

String compare using XSLT and XPATH


I have summerized some note about the string comparison via XSLT and XPATH.

  1. Find specific string with "=":
    Find specific Item with text "testITEM".

    <xsl:apply-templates select="//*[Item[text() = 'testITEM']]">
    </xsl:apply-templates>
  2. Find specific string with "contains":
    Find specific Item which contain "testITEM".

    <xsl:apply-templates select="//*[contains(Item,'testITEM']">
    </xsl:apply-templates>
  3. Find specific string  case-insensitivity "=":
    Find specific Item with text "testITEM" or "TESTITEM" or "TestItem" ... ETC.

    There is no way to compare it without transform, you need transform it to upper case(or small case) first.

    <xsl:variable name="UperCase">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
    <xsl:variable name="LowerCase">abcdefghijklmnopqrstuvwxyz</xsl:variable>

    <xsl:apply-templates select="//*[Item[translate(text(), $UperCase, $LowerCase) = translate('testITEM', $UperCase, $LowerCase)]]">
    </xsl:apply-templates>
  4. Compare orderings with great-than:
    Find specific date which great than "20060419":

    If the date is ISO format (YYYY-MM-DD) it is easy to compare with follow code. ( >= is the same case.)

    <xsl:apply-templates select="//*[date[translate(text(), '-', '') > translate('2006-04-19', '-', '')]]">
    </xsl:apply-templates>
  5. Compare orderings with less-than:
    Find specific date which less than "20060419":

    It should be note, "<" can not in XPATH. Please use "&lt;" to replace it. "<=" is the same code.

    <xsl:apply-templates select="//*[date[translate(text(), '-', '') < translate('2006-04-19', '-', '')]]">
    </xsl:apply-templates>

That is all, for more detail you can find some useful feedback in XSLT Questions and Answers.



學習文件 / VC6相關問題 / XML, XSLT, XPATH, 所有與XML相關的

由 Evan 發表於 April 19, 2006
引用
本文的引用網址:


以下是前來引用的連結 'String compare using XSLT and XPATH' 來自 Blog E
迴響