Search from attributes use XPATH

XML程式設計, VC++程式設計 No Comments »

To search a attribute via XPATH. you can use Element[@Attr] to address it in XPATH.

So~~ to search some specific search as follow:

1. product@cost which is > 5000, you can write as follow.

product[@cost > ‘5000′]

2. product@name contains "yellow" (case-insensitivity).

contains(translate(product[@name], ‘ABC…Z’, ‘abc….z’), translate(’yellow’, ‘ABC…Z’, ‘abc….z’))

How to use KEY in xslt

XML程式設計, VC++程式設計 No Comments »

W3C

I think XML is a very simple storage medium that you can save and query in it. But all data is storage as a tree node in the XML. In this case, it can very simple to save hierarchy data structure. But how to handle it you have reference item?

Just use key in your XML code. For more detail please refer the sample as follow:

t1.xslt

x1.xml

Result

String compare using XSLT and XPATH

XML程式設計, VC++程式設計, 學習文件 No Comments »

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.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS 登入