This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: 2xml files -> one xml file and search question


Michael,

>the varable search text is declared and i have no problems to search
>either for name or for street
>but how can i search for name + street ? (i have an inputfield and the
>search_text i get via the browser
>with <form action="sort.xml" method="get> and then <input type="submit"
>/>. when i enter name and street it looks like 
>http://.../my.xml?search_text=name+one+%2B+street+one      (thats what i
>want so this is not the problem <g>)
>and the variable search_text has the value "name+one+%2B+street+one" but
>how can i make 2 strings of it
>search_text1 (which contains name+one) and search_text2 (which contains
>street+one) ?
>somehow i should be able to search for %2B (which is +) and then extract
>both strings and write them into different
> variables... anyone did sth like that before ? how can i do that ?

You can use substring-before() and substring-after() to split the search
text string into the two lines:

<xsl:variable name="search_text1"
              select="substring-before($search_text, '+%2B+')" />
<xsl:variable name="search_text2"
              select="substring-after($search_text, '+%2B+')" />

You could then check whether both searches are satisfied using:

<xsl:for-each select="adress[child::*=$search_text1 and 
                             child::*=$search_text2]">
  <xsl:value-of select="name" />
  <xsl:value-of select="street" />
  ...
</xsl:for-each>

>and the other problem is if my input isnt exactly the same thing as in
>the xml file i dont get a result
>(for example if my input would be just "name" or "Name" or "nam" then i
>dont get any results.. how can i get all entrys which contains "name" or
>"nam" then ? is sth like name* or nam* as input possible ?? how can i
>tell this
>to my <xsl:for-each select=""> or to <xsl:if test=""> ??)
>can xsl(t) do this for me ? 

Doing case-insensitive matching is fairly easy: the translate() function
can be used to translate all uppercase letters to lowercase letters, and
the comparison can be done on the result:

<xsl:variable name="lc" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uc" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

<xsl:for-each select="adress[child::*[translate(., $uc, $lc) = 
                                      translate($search_text, $uc, $lc)]]">
  ...
</xsl:for-each>

XSLT doesn't handle regular expressions, so you have to code them yourself.
 At this point, the things you're testing for become too much for XPaths
and you have to iterate over each of the adress elements and test them
individually.  For example, the following template tests whether the search
text ends with a '*', and if it does sees whether there's a child element
that has content that starts with the search text (before the '*').  If the
search text doesn't end with a '*' then it just checks whether there's a
child element that has that value:

<xsl:for-each select="adress">
  <xsl:variable name="match">
    <xsl:choose>
      <xsl:when test="substring($search_text, string-length($search_text) -
1, 1) = '*'">
        <xsl:if test="child::*[starts-with(., substring($search_text, 1,
string-length($search_text) - 1))]">matches</xsl:if>
      </xsl:when>
      <xsl:when test="child::* = $search_text">matches</xsl:when>
    </xsl:choose>
  </xsl:variable>
  <xsl:if test="string($match)">
    <xsl:value-of select="name" />
    <xsl:value-of select="street" />
  </xsl:if>
</xsl:for-each>

I hope that this gives you some ideas about how to go about constructing
your stylesheet.

Cheers,

Jeni

Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]