如何在HXT中提取XML树的特定分支

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

举个例子:

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

我知道如何使用函数 atTag:

获取所有 "pou"
atTag:: (ArrowXml a) => String -> a XmlTree XmlTree
atTag tag = deep (isElem >>> hasName tag)

但是如何只提取带有 SFC 标签的 "pou"?有干净的方法吗?

提前致谢!

尝试使用 Haskell XPath 和:

/*/pou[body[SFC]]

进一步参考 "XPath find all elements with specific child node"

你想在这个例子中使用 filterA

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"