Text.XML.Cursor - 在类型中丢失 [英] Text.XML.Cursor - Lost in the types
问题描述
对于这个问题,我会从我正在使用的xml文件中粘贴一个示例节,然后讨论我正在尝试使用它。
我有带有节的xml文件,如下所示:
< mb model =460rev =dba>
< dmiblock block =基板信息>
< dmiattr name =Manufacturervalue =MSI/>
< dmiattr name =Product Namevalue =H61M-P25(MS-7680)/>
< / dmiblock>
< dmiblock block =Memory Device>
< dmiattr name =Bank Locatorvalue =A1_BANK0/>
< dmiattr name =Sizevalue =4096 MB/>
< / dmiblock>
< dmiblock block =Memory Device>
< dmiattr name =Bank Locatorvalue =A1_BANK1/>
< dmiattr name =Sizevalue =No Module Installed/>
< / dmiblock>
< dmiblock block =Memory Device>
< dmiattr name =Bank Locatorvalue =A1_BANK2/>
< dmiattr name =Sizevalue =No Module Installed/>
< / dmiblock>
< dmiblock block =Memory Device>
< dmiattr name =Bank Locatorvalue =A1_BANK3/>
< dmiattr name =Sizevalue =No Module Installed/>
< / dmiblock>
< cpublock number =0>
< cpuattr name =model namevalue =Intel(R)Pentium(R)CPU G850 @ 2.90GHz/>
< / cpublock>
< cpublock number =1>
< cpuattr name =model namevalue =Intel(R)Pentium(R)CPU G850 @ 2.90GHz/>
< / cpublock>
< pciblock block =NIC>
< pciattr string =Intel Corporation 82574L Gigabit Network Connectionnumber =2/>
< / pciblock>
< blockblock block =model>
< blockattr value =8GB SATA SSD T 3number =1/>
< blockattr value =WDC WD5003ABYX-0number =1/>
< / blockblock>
< / mb>
我试图根据属性 model
来自 mb
节点
和值$ c $
mb
节点
的第一个子元素中的第二个元素的 model =460
和 value =H61M-P25(MS-7680)
。在匹配后,我想在列表中放入 rev =dba
,并继续搜索其他中具有相同属性的其他节, mb
节点
。
我还没有走得很远,在类型上绊倒。我使用这个工作。
import Text.XML
import Text.XML.Cursor
将合格的Data.Text导入为T
getProfiles: :AdviseConf - > IO() - AdviseResult
getProfiles(AdviseConf model mb)= do
doc< - Text.XML.readFile def xmlFile
let cursor = fromDocument doc
_< - Prelude.writeFiletest.txt$
show $
T.concat $
cursor $ //
元素mb> =>
attributeIsmodel460> =>
元素dmiattr> =>
attributeIsvalueH61M-P25(MS-7680)
& // content
我知道最终的函数 content
是错误的,但我不知道哪个是正确的。我试图从匹配的节点捕获所有 rev
属性。
- 我认为您正在寻找
attribute
function。 通过使用
> =>元素dmiattr
你说的是当前元素的名称是 dmiattr
',我想你可能意指& / elementdmiattr
。但是因为你需要父级的@rev属性,所以你可能需要使用 check
函数。 li>
For this problem, I will paste a sample stanza from the xml file I am working with, then discuss what I am trying to do with it.
I have an xml file with stanzas that look like this:
<mb model="460" rev="dba">
<dmiblock block="Base Board Information">
<dmiattr name="Manufacturer" value="MSI"/>
<dmiattr name="Product Name" value="H61M-P25 (MS-7680)"/>
</dmiblock>
<dmiblock block="Memory Device">
<dmiattr name="Bank Locator" value="A1_BANK0"/>
<dmiattr name="Size" value="4096 MB"/>
</dmiblock>
<dmiblock block="Memory Device">
<dmiattr name="Bank Locator" value="A1_BANK1"/>
<dmiattr name="Size" value="No Module Installed"/>
</dmiblock>
<dmiblock block="Memory Device">
<dmiattr name="Bank Locator" value="A1_BANK2"/>
<dmiattr name="Size" value="No Module Installed"/>
</dmiblock>
<dmiblock block="Memory Device">
<dmiattr name="Bank Locator" value="A1_BANK3"/>
<dmiattr name="Size" value="No Module Installed"/>
</dmiblock>
<cpublock number="0">
<cpuattr name="model name" value="Intel(R) Pentium(R) CPU G850 @ 2.90GHz"/>
</cpublock>
<cpublock number="1">
<cpuattr name="model name" value="Intel(R) Pentium(R) CPU G850 @ 2.90GHz"/>
</cpublock>
<pciblock block="NIC">
<pciattr string="Intel Corporation 82574L Gigabit Network Connection" number="2" />
</pciblock>
<blockblock block="model">
<blockattr value="8GB SATA SSD T 3" number="1" />
<blockattr value="WDC WD5003ABYX-0" number="1" />
</blockblock>
</mb>
I am trying to find stanzas based on the value of the attributes model
from the mb
node
and the value
attribute from the second element in the first child of the mb
node
. So, in the stanza above, I would be trying to match against model="460"
and value="H61M-P25 (MS-7680)"
. Having matched against that, I'd want to put rev="dba"
in a list and continue searching for other stanzas with the same attributes in other mb
nodes
.
I haven't gotten very far at all, I keep getting tripped on the types. I am using this to work from.
import Text.XML
import Text.XML.Cursor
import qualified Data.Text as T
getProfiles :: AdviseConf -> IO () -- AdviseResult
getProfiles (AdviseConf model mb) = do
doc <- Text.XML.readFile def xmlFile
let cursor = fromDocument doc
_ <- Prelude.writeFile "test.txt" $
show $
T.concat $
cursor $//
element "mb" >=>
attributeIs "model" "460" >=>
element "dmiattr" >=>
attributeIs "value" "H61M-P25 (MS-7680)"
&// content
I know the final function content
is wrong, but I dont know which would be right. I'm trying to capture all the rev
attributes from the nodes that match.
So, when I play with the above, I either end up with an empty list, junk characters, or a type error. Any help would be appreciated.
- I think you're looking for the
attribute
function. - By using
>=> element "dmiattr"
you're saying "the current element's name isdmiattr
'. I think you might mean&/ element "dmiattr"
. But since you want the @rev attribute of the parent, you probably need to use thecheck
function instead.
这篇关于Text.XML.Cursor - 在类型中丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!