ymtszw/elm-xml-decode/1.0.1

NOTE: this package is not compatible with Elm 0.19.1

Xml.Decode.Pipeline

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

Leverages basic (|>) operator by requiredPath, optionalPath and possiblePath, allowing DSL style decoder composition.

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


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


Xml.Decode.run pipelineDecoder
    """
    <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 uses Basic.|> so you do not have to import non-standard infix operator.

Pipeline Node Locaters

requiredPath : List String -> ListDecoder a -> Decoder (a -> b) -> Decoder b

Decodes value at required XML path.

optionalPath : List String -> ListDecoder a -> a -> Decoder (a -> b) -> Decoder b

Tries to decode value at optional XML path. Uses default value if the node is missing.

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


decoderWithDefault : Decoder String
decoderWithDefault =
    succeed identity
        |> optionalPath [ "optional", "path" ] (single string) "default"


Xml.Decode.run decoderWithDefault "<root><optional><path>string</path></optional></root>"
--> Ok "string"


Xml.Decode.run decoderWithDefault "<root></root>"
--> Ok "default"
possiblePath : List String -> ListDecoder a -> Decoder (Maybe a -> b) -> Decoder b

Decodes value at possible XML path into Maybe value.

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


maybeDecoder : Decoder (Maybe String)
maybeDecoder =
    succeed identity
        |> possiblePath [ "possible", "path" ] (single string)


Xml.Decode.run maybeDecoder "<root><possible><path>string</path></possible></root>"
--> Ok (Just "string")


Xml.Decode.run maybeDecoder "<root></root>"
--> Ok Nothing

If you want to apply default value when the node is missing, use optionalWith.