【发布时间】:2017-08-31 09:47:53
【问题描述】:
我想在具有多个值的属性中选择几个属性值print。对于这个例子
#in R
interpRef <- getNodeSet(doc,"//ns:ref[contains(@ana, 'whatAction')]", ns)
interpRef_ana <- for (i in 1:length(interpRef)) print(paste(xmlGetAttr(interpRef[[i]],"ana")))
我有结果:
[[1]]
<ref ana="whatAction #ktu1-3_ii_l6b_tḫtṣb #verb.competition #contend">Action belongs to verb competition subcategory contend
<stage ana="whatResult #result #defeate_ofOpposition"/></ref>
[[2]]
<ref ana="whatAction #ktu1-3_ii_l7_tmḫṣ #verb.emotion #humiliation">Action belongs to verb emotion, subcategory humiliation
<stage ana="whatResult #result #defeate_ofOpposition"/></ref>
[[3]]
<ref ana="whatAction #ktu1-3_ii_l8_tṣmt #verb.emotion #humiliation">Action belongs to verb emotion, subcategory humiliation</ref>
#print
[1] "whatAction #ktu1-3_ii_l6b_tḫtṣb #verb.competition #contend"
[1] "whatAction #ktu1-3_ii_l7_tmḫṣ #verb.emotion #humiliation"
[1] "whatAction #ktu1-3_ii_l8_tṣmt #verb.emotion #humiliation"
我只需要 @ana 属性中的几个属性值,值 2 和 3,例如 print:
[1] "#ktu1-3_ii_l6b_tḫtṣb #contend"
[1] "#ktu1-3_ii_l7_tmḫṣ #humiliation"
[1] "#ktu1-3_ii_l8_tṣmt #humiliation"
我已经做了几次尝试,其中下面的一个,但它不起作用:
interpRef_ana <- for (i in 1:length(interpRef)) print(paste(xmlGetAttr(interpRef[[i]],"ana",[2:3])))
==== XML 示例 ====
每个<ref> 都在<interp> 内,每个@ana 都遵循相同的层次结构,词汇来自预定义的分类法。
<interp xml:id="ktu1-3_ii_l6b_int" ana="#ktu1-3_ii_l6b" corresp="#ktu1-3_ii_6b">
<desc>
<ref ana="whatAction #ktu1-3_ii_l6b_tḫtṣb #verb.competition #contend"
>Action belongs to verb competition subcategory contend
<stage ana="whatResult #result #defeate_ofOpposition" />
</ref>
<castList>
<castItem>
<persName type="character" ana="#whatCharacter #Character #ANT #Female">
<state ana="#whatRole #active" />ʾAnatu
</persName>
</castItem>
</castList>
<view>
<placeName ana="#whatContext #battle">battle
<location ana="#whatSphere #outside" />
</placeName>
</view>
<stage ana="#whatBehavior">
<span ana="#toDestroy #five_dD #rage">Voluntary
intentionality, to destroy of her free will, with rage
(level five).</span>
<span ana="#AffectEntity_and_other">The result of action has
an impact on ʾAnatu and others</span>
</stage>
</desc>
</interp>
<interp xml:id="ktu1-3_ii_l7_int" ana="#ktu1-3_ii_l7" corresp="#ktu1-3_ii_l7">
<desc>
<ref ana="whatAction #ktu1-3_ii_l7_tmḫṣ #verb.emotion #humiliation"
>Action belongs to verb emotion, subcategory humuliation
<stage ana="whatResult #result #defeate_ofOpposition" />
</ref>
<castList>
<castItem>
<persName type="character" ana="#whatCharacter #Character #ANT #Female">
<state ana="#whatRole #active" />ʾAnatu
</persName>
<persName type="character" cert="low" ana="#Character #UNK #Unknown">
<state ana="#behav #passive" />People from the West
</persName>
</castItem>
</castList>
<view>
<placeName ana="#whatContext #battle">battle
<location ana="#whatSphere #outside" />outside her household
</placeName>
</view>
<stage ana="#whatBehavior">
<span ana="#toDestroy #free #five_dD">Voluntary
intentionality, to destroy of her free will, with rage
(level five)Five.</span>
<span ana="#affectEntity_and_other">The result of action has
an impact on ʾAnatu and others</span>
</stage>
</desc>
</interp>
==== 更新====
我尝试使用库string,理论上可以,我可以选择我需要的属性值:
x <- for (i in 1:length(interp)) print((cbind((y=(KTU = (xmlGetAttr(interp[[i]],"ana")))), (z=(verb.category = (xmlGetAttr(interpRef[[i]],"ana")))))))
x1 <- print (cbind(word(word(y,-1)),(word(z, -3, -2))))
x1
> x <- for (i in 1:length(interp)) print((cbind((y=(KTU = (xmlGetAttr(interp[[i]],"ana")))), (z=(verb.category = (xmlGetAttr(interpRef[[i]],"ana")))))))
[,1] [,2]
[1,] "#ktu1-3_ii_l5b-6a" "whatAction #ktu1-3_ii_l5b-6a_tmtḫṣ #verb.competition #contend"
[,1] [,2]
[1,] "#ktu1-3_ii_l6b" "whatAction #ktu1-3_ii_l6b_tḫtṣb #verb.competition #contend"
[,1] [,2]
[1,] "#ktu1-3_ii_l7" "whatAction #ktu1-3_ii_l7_tmḫṣ #verb.emotion #humiliation"
[,1] [,2]
[1,] "#ktu1-3_ii_l8" "whatAction #ktu1-3_ii_l8_tṣmt #verb.emotion #humiliation"
[,1] [,2]
[1,] "ktu1-3_ii_l11b_12a" "whatAction #ktu1-3_ii_l11b-12a_ʿtkt #put_together #action"
[,1] [,2]
[1,] "#ktu1-3_ii_l12b_13a" "whatAction #ktu1-3_ii_l12b-13a_šnst #put_together #action"
[,1] [,2]
[1,] "#ktu1-3_ii_l13b_14a" "whatAction #ktu1-3_ii_l13b-14a_tġlt #action #movement"
[,1] [,2]
[1,] "#ktu1-3_ii_l15b_16a" "whatAction #ktu1-3_ii_l5b_6a_tmtḫṣ #confrontation #action"
> x
NULL
> x1 <- print (cbind(word(word(y,-1)),(word(z, -3, -2))))
[,1] [,2]
[1,] "#ktu1-3_ii_l15b_16a" "#ktu1-3_ii_l5b_6a_tmtḫṣ #confrontation"
> x1
[,1] [,2]
[1,] "#ktu1-3_ii_l15b_16a" "#ktu1-3_ii_l5b_6a_tmtḫṣ #confrontation"
但它只给了我一次出现的属性值,而不是列表。所以我尝试添加for (i in 1:length(interp)):
x1 <- for (i in 1:length(interp)) print (cbind(word(word(y,-1)),(word(z, -3, -2))))
> x1 <- for (i in 1:length(interp)) print (cbind(word(word(y,-1)),(word(z, -3, -2))))
[,1] [,2]
[1,] "#ktu1-3_ii_l15b_16a" "#ktu1-3_ii_l5b_6a_tmtḫṣ #confrontation"
[,1] [,2]
[1,] "#ktu1-3_ii_l15b_16a" "#ktu1-3_ii_l5b_6a_tmtḫṣ #confrontation"
[,1] [,2]
[1,] "#ktu1-3_ii_l15b_16a" "#ktu1-3_ii_l5b_6a_tmtḫṣ #confrontation"
[,1] [,2]
[1,] "#ktu1-3_ii_l15b_16a" "#ktu1-3_ii_l5b_6a_tmtḫṣ #confrontation"
[,1] [,2]
[1,] "#ktu1-3_ii_l15b_16a" "#ktu1-3_ii_l5b_6a_tmtḫṣ #confrontation"
[,1] [,2]
[1,] "#ktu1-3_ii_l15b_16a" "#ktu1-3_ii_l5b_6a_tmtḫṣ #confrontation"
[,1] [,2]
[1,] "#ktu1-3_ii_l15b_16a" "#ktu1-3_ii_l5b_6a_tmtḫṣ #confrontation"
[,1] [,2]
[1,] "#ktu1-3_ii_l15b_16a" "#ktu1-3_ii_l5b_6a_tmtḫṣ #confrontation"
> x1
我只是重复相同的事件 8 次(= 实际出现的次数)
提前感谢您的帮助。
【问题讨论】:
-
能否请您张贴您的 XML 的小片段?我通常会和你在这里做的有点不同。我在下面发布了一个答案-它与您的情况不符-但您可能会发现它很有帮助。如果您发布 xml 的片段,我很乐意进行调整
-
我可能误读了您的问题,但使用了 XML 的片段。我相信我可以提供帮助。
-
谢谢伊恩。我刚刚在 == XML === 之后添加了一个示例。
-
我尝试了
word {stringr}的解决方案,它有效,但它没有给我所有出现的列表。