Note that there are some explanatory texts on larger screens.

plurals
  1. POHXT: Select a node by position with HXT in Haskell?
    primarykey
    data
    text
    <p>I’m trying to parse some XML files with Haskell. For this job I’m using <a href="http://www.haskell.org/haskellwiki/HXT" rel="nofollow">HXT</a> to get some knowledge about arrows in real world applications. So I’m quite new to the arrow topics.</p> <p>In XPath (and <a href="http://hackage.haskell.org/packages/archive/HaXml/1.24/doc/html/Text-XML-HaXml-Combinators.html#v%3aposition" rel="nofollow">HaXml</a>) it’s possible to select a node by position, let’s say: <code>/root/a[2]/b</code></p> <p>I can’t figure out how to do something like that with HXT, even after reading the documentation again and again.</p> <p>Here is some sample code I’m working with:</p> <pre><code>module Main where import Text.XML.HXT.Core testXml :: String testXml = unlines [ "&lt;?xml version=\"1.0\"?&gt;" , "&lt;root&gt;" , " &lt;a&gt;" , " &lt;b&gt;first element&lt;/b&gt;" , " &lt;b&gt;second element&lt;/b&gt;" , " &lt;/a&gt;" , " &lt;a&gt;" , " &lt;b&gt;third element&lt;/b&gt;" , " &lt;/a&gt;" , " &lt;a&gt;" , " &lt;b&gt;fourth element&lt;/b&gt;" , " &lt;b&gt;enough...&lt;/b&gt;" , " &lt;/a&gt;" , "&lt;/root&gt;" ] selector :: ArrowXml a =&gt; a XmlTree String selector = getChildren /&gt; isElem &gt;&gt;&gt; hasName "a" -- how to select second &lt;a&gt;? /&gt; isElem &gt;&gt;&gt; hasName "b" /&gt; getText main :: IO () main = do let doc = readString [] testXml nodes &lt;- runX $ doc &gt;&gt;&gt; selector mapM_ putStrLn nodes </code></pre> <p>The desired output would be:</p> <pre><code>third element </code></pre> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload