ymtszw/elm-xml-decode/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Xml.Decode.Extra

Xml decoder module sharing the spirit of Json.Decode.Extra.

Uses (|:) infix operator that allows writing decoders in sequential style like so:

import Xml.Decode exposing (Decoder, succeed, path, single, list, string, int)


applicativeDecoder : Decoder ( String, List Int )
applicativeDecoder =
    succeed (,)
        |: path [ "path", "to", "string", "value" ] (single string)
        |: path [ "path", "to", "int", "values" ] (list int)


Xml.Decode.run applicativeDecoder
    """
    <root>
        <path>
            <to>
                <string>
                    <value>SomeString</value>
                </string>
                <int>
                    <values>1</values>
                    <values>2</values>
                </int>
            </to>
        </path>
    </root>
    """
--> Ok ( "SomeString", [ 1, 2 ] )

Benefit of this style is it leverages standard decoders from Xml.Decode, so you only have to import (|:).

Also, this style can be considered "Applicative" style of sequential application. (|:) is essentially Applicative's (<*>).

Sequential Decoding

andMap : Decoder a -> Decoder (a -> b) -> Decoder b

Equivalent to Json.Decode.Extra.andMap, allows writing XML decoders in sequential style.

import Xml.Decode exposing (succeed, string)


Xml.Decode.run (andMap string (succeed identity)) "<root>string</root>"
--> Ok "string"
(|:) : Decoder (a -> b) -> Decoder a -> Decoder b

Equivalent to Json.Decode.Extra.(|:), infix version of andMap.