0

I would like to select few attribute values within an attribute with multiple values to print. For this example

#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")))

I have the result:

[[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"

I need just few attribute values from @ana attributes, values 2 and 3, example to 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"

I have made several attempts amongst which the one that follows, but it's not working:

interpRef_ana <- for (i in 1:length(interpRef)) print(paste(xmlGetAttr(interpRef[[i]],"ana",[2:3])))

==== XML EXAMPLE ====

Each <ref> is within <interp>, and each @ana follows the same hierarchical structure from which the vocabulary comes from pre-defined taxonomies.

<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>

==== update ====

I have tried to use library string, theoriticaly it works, I can select the attribute values I need:

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"

But it just gives me the attribute values for one occurrence and not the list. So I have tried to add 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

I just repeat the same occurrences 8 times (= number of real occurrences)

In advance, thanks for your help.

Vanessa
  • 121
  • 12
  • Would you please post a small snip it of your XML? I usually go about this a bit differently than you have here. I have posted an answer below - it does not match your case - but you may find it helpful. I am happy to adjust if you post a snip-it of xml – Ian Wesley Apr 05 '17 at 22:46
  • I may have misread your question, but with a snip it of XML. I am sure I can help. – Ian Wesley Apr 05 '17 at 23:00
  • Thanks Ian. I have just added a sample after == XML ===. – Vanessa Apr 06 '17 at 10:08
  • I have tried a solution with `word {stringr}`, it works but it doesn't give me a list of all occurrences. – Vanessa Apr 07 '17 at 14:01

1 Answers1

0

I found the solution, maybe it will be helpful:

listInterp <- list()
 for (i in 1:length(interp)) {
  print ((cbind((y=(KTU = (xmlGetAttr(interp[[i]],"ana")))), (z=(verb.category = (xmlGetAttr(interpRef[[i]],"ana")))))))
  listInterp[[i]] <- (paste(cbind(word(word(y,-1)),(word(z, -3, -2))), collapse=": ")) #to select attribute values
  }
listInterp<-(lapply(listInterp,gsub,pattern="#",replacement="")) #to replace # by empty space
listInterp

#result
[[1]]
[1] "ktu1-3_ii_l5b-6a: ktu1-3_ii_l5b-6a_tmtḫṣ verb.competition"
[[2]]
[1] "ktu1-3_ii_l6b: ktu1-3_ii_l6b_tḫtṣb verb.competition"
[[3]]
[1] "ktu1-3_ii_l7: ktu1-3_ii_l7_tmḫṣ verb.emotion"
[[4]]
[1] "ktu1-3_ii_l8: ktu1-3_ii_l8_tṣmt verb.emotion"
[...]
Vanessa
  • 121
  • 12