1

Hi I have an XML which looks like

<Movies>
    <Movie>
        <Title>$#*! My Dad Says</Title>
        <Year>2010</Year>
        <OtherTitles>
            <OtherTitleName>Beep My Dad Says</OtherTitleName>
            <Year>2010</Year>
            <Country>USA</Country>
            <TitleType>alternative title</TitleType>
            <OtherTitleName>Shit My Dad Says</OtherTitleName>
            <Year>2010</Year>
            <Country>USA</Country>
            <TitleType>uncensored intended title</TitleType>
            <OtherTitleName>Shit! My Dad Says</OtherTitleName>
            <Year>2013</Year>
            <Country>Germany</Country>
            <TitleType>imdb display title</TitleType>
        </OtherTitles>
    </Movie>
</Movies>

I am trying to get its OtherTitleName and year for each of the movie to display it like this -

       Beep My Dad Says, 2010   
       Shit! My Dad says, 2010

I have tried to use concatenation but I get the error item expected, sequence found.

How can I get each item's title and year while looping through it?

EDIT: I have tried the Xquery like this.

   for $n in Movies//Movie//OtherTitles

       let $d := ($n/OtherTitleName/text(),$n/Year/text())

         return $d
     // let $d := concat($n/OtherTitleName/text(),$n/Year/text()) Gives me error
user2775042
  • 509
  • 2
  • 6
  • 21
  • How did you try exactly? – har07 Apr 14 '16 at 11:43
  • BTW, there's no need to put "EDIT:" before edits; the cultural preference here is for a question to read and flow as well as possible -- anyone who wants to know the edit history can ask StackOverflow and get a series of diffs showing how it changed over time. – Charles Duffy Apr 15 '16 at 13:02

2 Answers2

2

You can return concatenation of each OtherTitleName element with the corresponding Year element right next to it using XPath as follow :

//OtherTitleName/concat(.,following-sibling::Year[1])
har07
  • 88,338
  • 12
  • 84
  • 137
1

You can use the following code to get the above result.

let $OtherTitleName := $xml/Movie/OtherTitles/OtherTitleName/text()
let $Year := $xml/Movie/OtherTitles/Year/text()
for $each at $pos in $OtherTitleName 
return concat($each, ", ", $Year[$pos])
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
shreyas35
  • 33
  • 6
  • 1
    FYI -- use the `{}` button (or a four-space indent on each line, which it creates) to mark a block as code; that way, unlike with backticks, it's eligible for syntax highlighting. – Charles Duffy Apr 15 '16 at 12:58