如何获得getChildren HXT的第一个元素?

时间:2014-06-11 15:25:28

标签: xml haskell hxt

所以我遇到了HXT的问题,我不知道如何获得第一个元素:

<rdfs:subClassOf rdf:resource="http://www.xfront.com/owl/ontologies/camera/#PurchaseableItem"/>
<owl:disjointWith rdf:Description="Hello"/>
<owl:disjointWith rdf:Description="Hello1"/>
<owl:disjointWith rdf:Description="Hello2"/>
<owl:disjointWith rdf:Description="Hello3"/>
<owl:disjointWith rdf:Description="Hello4"/>

<owl:equivalentClass> 
    <owl:Class>
        <owl:intersectionOf rdf:parseType="Collection">
        <rdf:Description rdf:about="http://www.xfront.com/owl/ontologies/camera/#Body"/>
        <owl:Restriction>
            <owl:onProperty rdf:resource="http://www.xfront.com/owl/ontologies/camera/#shutter-speed"/>
            <owl:cardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger">0</owl:cardinality>
        </owl:Restriction>
        </owl:intersectionOf>
    </owl:Class>
</owl:equivalentClass>

我做了getChildren给了我答案(两个孩子),但我不想要所有这些信息!我只想要这些getChildren的第一行!这是:<rdfs:subClassOf.../><owl:equivalentClass>

我怎么能这样做? 幸运的是,

P.S:

顺便说一下,getChildren返回一个孩子列表吧?我做了诀窍来获取getChildren的第一个元素:getChildren >. (!! 0)并且它不起作用!给我一些索引太大的错误...

1 个答案:

答案 0 :(得分:2)

重复类似的问题,并附有解释答案here

import "hxt" Control.Arrow.ArrowTree (changeChildren, getChildren) 

getNthChild :: (ArrowTree a, Tree t) => Int -> a (t b) (t b)
getNthChild n = changeChildren (take 1 . drop n) >>> getChildren

更新:使用hxt-xpath更简单的替代方法

fname = "http://protege.cim3.net/file/pub/ontologies/camera/camera.owl" 

myArrow = readDocument [withValidate no,
                        withCheckNamespaces yes,
                        withSubstDTDEntities no, withHTTP []] fname 
          >>> getXPathTrees "/rdf:RDF/owl:Class[1]" 
          >>> getAttrValue "rdf:ID"

main = do
         results <- runX myArrow
         print results 

结果:

["Money"]

更新:使用getNthChild,过滤非元素子项

import qualified Text.XML.HXT.DOM.XmlNode as XN

getNthChild :: (ArrowTree a, Tree t, XN.XmlNode b) => Int -> a (t b) (t b)
getNthChild n = changeChildren (take 1 . drop n . filter XN.isElem) 
                >>> getChildren

然后

myArrow = readDocument [withValidate no,
                        withCheckNamespaces yes,
                        withSubstDTDEntities no, withHTTP []] fname 
          >>> getXPathTrees "/rdf:RDF" 
          >>> getNthChild 1               -- second child
          >>> getAttrValue "rdf:ID"

给出与上面相同的结果。