Yoanribeiro
Yoanribeiro

Reputation: 145

How to extract a specific branch of a XML tree in HXT

Giving the example:

<pous>
  <pou>
    <interface>
      ...
    </interface>
    <body>
      <FBD>
        ...
      </FBD>
    </body>
  </pou>
  <pou>
    <interface>
      ...
    </interface>
    <body>
      <SFC>
        ...
      </SFC>
    </body>
  </pou>
</pous>

I know how to obtain all the "pou" using the function atTag:

atTag:: (ArrowXml a) => String -> a XmlTree XmlTree
atTag tag = deep (isElem >>> hasName tag)

But how to I extract only the "pou" with the the SFC tag? Is there a clean way to do it?

Thank you in advance!

Upvotes: 2

Views: 205

Answers (2)

bheklilr
bheklilr

Reputation: 54058

You want to use filterA for this example

application
    = processChildren
    $ getChildren                    -- Gets the children of "pous"
    >>> filterA hasSFC
    where hasSFC =  hasName "pou"    -- Include all "pou" elements
                >>> getChildren      -- that have children
                >>> getChildren      -- that have children
                >>> hasName "SFC"    -- that have a "SFC" element

main = do
    runX $
        readDocument [] "input.xml" >>>
        application                 >>>
        writeDocument [withIndent yes] "output.xml"

Upvotes: 1

malpka
malpka

Reputation: 85

Try using Haskell XPath and:

/*/pou[body[SFC]]

Further reference "XPath find all elements with specific child node"

Upvotes: 0

Related Questions