Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6968961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:31:12+00:00 2026-05-27T16:31:12+00:00

i read on Andrew Birkett’s blog Applicative arrows for XML &&& return to pure

  • 0

i read on Andrew Birkett’s blog Applicative arrows for XML &&& return to pure that we could mix arrows and applicative functors.

I tried it by my own but i don’t have what i expect.
i would like this result:

[Scenario {scenario = "11111", origin = "333", alarm = "Sonde1"},
 Scenario {scenario = "22222", origin = "444", alarm = "Sonde2"}]

but i get this instead:

[Scenario {scenario = "11111", origin = "333", alarm = "Sonde1"},
 Scenario {scenario = "11111", origin = "333", alarm = "Sonde2"},
 Scenario {scenario = "11111", origin = "444", alarm = "Sonde1"},
 Scenario {scenario = "11111", origin = "444", alarm = "Sonde2"},
 Scenario {scenario = "22222", origin = "333", alarm = "Sonde1"},
 Scenario {scenario = "22222", origin = "333", alarm = "Sonde2"},
 Scenario {scenario = "22222", origin = "444", alarm = "Sonde1"},
 Scenario {scenario = "22222", origin = "444", alarm = "Sonde2"}]

i think there is a twist in my code but i don’t know where to search.

Below is my code if anyone can suggest some help.

{-# LANGUAGE Arrows, NoMonomorphismRestriction #-}

import Text.XML.HXT.Core
import Control.Applicative
import Text.XML.HXT.Arrow.ReadDocument
import Data.Maybe
import Text.XML.HXT.XPath.Arrows
import Text.Printf


data Scenario = Scenario
  { scenario, origin, alarm    :: String
  }
  deriving (Show, Eq)


xml= "<DATAS LANG='en'>\
    \ <SCENARIO ID='11111'>\
    \   <ORIGIN ID='333'>\
    \       <SCENARIO_S ERR='0'></SCENARIO_S>\
    \       <SCENARIO_S ERR='2'></SCENARIO_S>\
    \       <ALARM_M NAME='Sonde1'></ALARM_M>\
    \   </ORIGIN>\
    \ </SCENARIO>\
    \ <SCENARIO ID='22222'>\
    \   <ORIGIN ID='444'>\
    \       <SCENARIO_S ERR='10'></SCENARIO_S>\
    \       <SCENARIO_S ERR='12'></SCENARIO_S>\
    \       <ALARM_M NAME='Sonde2'></ALARM_M>\
    \   </ORIGIN>\
    \ </SCENARIO>\
    \</DATAS>"

parseXML string = readString [ withValidate no
                         , withRemoveWS yes  -- throw away formating WS
                         ] string


parseVal tag name = WrapArrow $ getXPathTrees (printf "/DATAS/%s" tag) >>>  getAttrValue name

parseDatas = unwrapArrow $ Scenario <$> parseVal "SCENARIO"      "ID"
                                 <*> parseVal "SCENARIO/ORIGIN"        "ID"
                                 <*> parseVal "SCENARIO/ORIGIN/ALARM_M"        "NAME"

testarr1= runX (parseXML xml >>> parseDatas)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:31:13+00:00Added an answer on May 27, 2026 at 4:31 pm

    As pointed out by rampion, the problem is how the list monad works with applicative. Take a look at this:

    λ *Main > (+) <$> [1,2,3] <*> [1,2,3]
    [2,3,4,3,4,5,4,5,6]
    

    The result is the carthesian product of (+) applied to [1,2,3] and [1,2,3]: the result list has 9 elements.

    In your code, parseVal "SCENARIO" "ID" will return a list of 2 elements, and so will parseVal "SCENARIO/ORIGIN" "ID" and parseVal "SCENARIO/ORIGIN/ALARM_M" "NAME". Therefore, the result will have 8 elements.

    Instead, this is how I would change your code:

    --- parse a generic tag
    parseVal tag name = WrapArrow $ getXPathTrees (printf "%s" tag) >>>  getAttrValue name
    
    --- parse a "SCENARIO" xml element
    parseScenario = unwrapArrow $ Scenario
            <$> (WrapArrow $ getAttrValue "ID")
            <*> (parseVal "SCENARIO/ORIGIN" "ID")
            <*> (parseVal "SCENARIO/ORIGIN/ALARM_M" "NAME")
    
    --- parse the XML, extract a list of SCENARIOS and, for each, apply parseScenario
    testarr1= runX (parseXML xml >>> getXPathTrees (printf "/DATAS/SCENARIO" ) >>> parseScenario)
    

    The result is as desired:

    λ *Main > testarr1 
    [Scenario {scenario = "11111", origin = "333", alarm = "Sonde1"},Scenario {scenario = "22222", origin = "444", alarm = "Sonde2"}]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Read in some blog that GC in Android happens on main(UI) thread, this may
Read Carefully The question here is not that does PHP parser processes comments or
I read this question's answers that explain the order of the LINQ to objects
I read that it's possible to make quicksort run at O(nlogn) the algorithm says
I've read about Small-Object Allocation in Modern C++ Design. Andrei Alexandrescu argues that the
I'm trying to create a C++ program that allows me to read from a
I urgently need to know how to read the following XML file: <uclassify xmlns=http://api.uclassify.com/1/ResponseSchema
is it possible to store a variable through javascript so that I can read
I've regularly read that the framework is just too large for one developer to
I need to improve memory performance on my application and I could see that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.