Reputation: 337
I am trying to parse a XML file that has the following structure
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://12345hc.com/xsd">
<xs:complexType name="Context">
<xs:sequence>
<xs:element minOccurs="0" name="aNum" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="aId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="bURI" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="facility" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="fSessionId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="ID" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="pwrd" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="profileID" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="sToken" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="sId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="tId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="uNum" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="webURI" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="xZRT" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
I trying to select the node having name="Context"
tag,
within this tag select the element labeled as ID,
<xs:element minOccurs="0" name="ID" nillable="true" type="xs:string"/>
and add value "111111" to this element ID
.
Any pointers/answers on accomplishing this will be very helpful. Thanks in advance.
Upvotes: 2
Views: 73
Reputation: 54237
You could do
txt <- '<xs:schema attributeFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://12345hc.com/xsd">
<xs:complexType name="Context">
<xs:sequence>
<xs:element minOccurs="0" name="aNum" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="aId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="bURI" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="facility" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="fSessionId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="ID" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="pwrd" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="profileID" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="sToken" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="sId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="tId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="uNum" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="webURI" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="xZRT" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>'
library(XML)
xml <- xmlParse(txt, asText=TRUE)
ns <- getNodeSet(xml, '//*[@name="Context"]/xs:sequence/xs:element')
id <- which(sapply(ns, xmlGetAttr, "name") == "ID")
xmlValue(ns[[id]]) <- "11111"
xml
# <?xml version="1.0"?>
# <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://12345hc.com/xsd">
# <xs:complexType name="Context">
# <xs:sequence>
# <xs:element minOccurs="0" name="aNum" nillable="true" type="xs:string"/>
# <xs:element minOccurs="0" name="aId" nillable="true" type="xs:string"/>
# <xs:element minOccurs="0" name="bURI" nillable="true" type="xs:string"/>
# <xs:element minOccurs="0" name="facility" nillable="true" type="xs:string"/>
# <xs:element minOccurs="0" name="fSessionId" nillable="true" type="xs:string"/>
# <xs:element minOccurs="0" name="ID" nillable="true" type="xs:string">11111</xs:element>
# <xs:element minOccurs="0" name="pwrd" nillable="true" type="xs:string"/>
# <xs:element minOccurs="0" name="profileID" nillable="true" type="xs:string"/>
# <xs:element minOccurs="0" name="sToken" nillable="true" type="xs:string"/>
# <xs:element minOccurs="0" name="sId" nillable="true" type="xs:string"/>
# <xs:element minOccurs="0" name="tId" nillable="true" type="xs:string"/>
# <xs:element minOccurs="0" name="uNum" nillable="true" type="xs:string"/>
# <xs:element minOccurs="0" name="webURI" nillable="true" type="xs:string"/>
# <xs:element minOccurs="0" name="xZRT" nillable="true" type="xs:string"/>
# </xs:sequence>
# </xs:complexType>
# </xs:schema>
Upvotes: 2