Haskell HXT parse condition

276 views Asked by At

i'm trying to parse this xml:

...
<member>
  <name>id</name>
  <value>
    <string>1</string>
  </value>
</member>
<member>
  <name>description</name>
  <value>
    <string>sdfsdfsdf</string>
  </value>
</member>
...

how to parse only the "<member>" tags with a subordinate "name"-tag = "id"?

i tried:

getroot = multi ( ( getChildren >>> hasName "name" >>> hasText "id") `guards` (isElem >>> hasName "member"  ) ) 

main = do
print <- runX (parseXML "test2.xml" >>> getroot >>> putXmlTree "-")
1

There are 1 answers

1
Joachim Breitner On

When you use the filter hasName "name", you get the <name> tag. That node itself is not a text node, so hasText "id" fails. Here a modification that seems to work: (I also had to change the type of the argument of hasText, maybe a different version of HXT)

import Text.XML.HXT.Core

getroot = multi ( ( getChildren >>> hasName "name" >>> getChildren >>> hasText (=="id")) 
                   `guards` (isElem >>> hasName "member"  ) ) 

main = do
    runX (readDocument [] "test2.xml" >>> getroot >>> putXmlTree "-")

I’m not an expert of HXT so it might be that there is a much better way to do what you want.