Search code examples
xmlxqueryalphabetized

XQuery XML Alphabetizer ignoring "The" as first word in name while sorting


This is kind of a run-on from an older question I had here where Michael Kay supplied a great XQuery solution to sorting my xml file. However I am now hoping to use the same code and have it ignore "The" as a first word in a name while sorting but not remove it from the file.
Here is the Code provided by Michael:

    <items>{
      for $i in //item order by $i/name return $i
    }</items>

I have been googling the death out of this for a long time. I canno even figure out what the proper term is when "The" is used as the first word in a title or name. It must have a name lol. Thank you for any help yet again team. Appreciate all the help you guys provide.


Solution

  • To ignore “The” in your title, just add a new variable with the adjusted title, and use this in your order by clause:

    <items>{
        for $i in //item 
        let $name-sort-key := 
            if (starts-with($i/name, "The ")) then
                substring-after($i/name, "The ")
            else
                $i/name
        order by $name-sort-key
        return 
            $i
    }</items>