我正在尝试解析具有以下结构的 XML 文件
<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>
我尝试选择具有 name="Context"
的节点标签,
在此标记中选择标记为 ID 的元素,
<xs:element minOccurs="0" name="ID" nillable="true" type="xs:string"/>
并将值“111111”添加到此元素 ID
。
任何关于完成此任务的指示/答案都会非常有帮助。提前致谢。
最佳答案
你可以做
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>
关于xml - 在 R 中解析 xml 文件及其元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37095844/