Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 更改XML中的节点名称_R_Xml - Fatal编程技术网

R 更改XML中的节点名称

R 更改XML中的节点名称,r,xml,R,Xml,我有两种类型的XML:myxml和myxml2将它们转换为DF。这两个XML的节点不同,一个以test开头,另一个以test2开头。见下文: ##myxml <?xml version="1.0" encoding="ISO-8859-1" ?> <test:TASS xmlns="http://www.vvv.com/schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLo

我有两种类型的
XML
myxml
myxml2
将它们转换为
DF
。这两个
XML
的节点不同,一个以
test
开头,另一个以
test2
开头。见下文:

##myxml

<?xml version="1.0" encoding="ISO-8859-1" ?>


<test:TASS xmlns="http://www.vvv.com/schemas"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.vvv.com/schemas http://www.vvv.com/schemas/testV2_02_03.xsd"  xmlns:test="http://www.vvv.com/schemas" >
                <test:billing>
                    <test:proceduresummary>
                        <test:guidenumber>X2030</test:guidenumber>
                            <test:diagnosis>
                                <test:table>ICD-10</test:table>
                                <test:diagnosiscod>J441</test:diagnosiscod>
                                <test:description>CHRONIC OBSTRUCTIVE PULMONARY DISEASE WITH (ACUTE) EXACERBATION</test:description>
                            </test:diagnosis>
                            <test:procedure>
                                <test:procedure>
                                    <test:description>HOSPITAL</test:description>
                                </test:procedure>
                                <test:amount>12</test:amount>
                            </test:procedure>
                    </test:proceduresummary>
                    </test:billing>
</test:TASS>
##myxml
X2030
ICD-10
J441
慢性阻塞性肺疾病(急性)加重
医院
12
要转换为DF的代码(xml=test)

#需要(tidyverse)
#require(xml2)
#setwd(“D:/”)
#myxml%jsonlite::fromJSON()
现在我的XML=test2

##myxml2

<?xml version="1.0" encoding="ISO-8859-1" ?>


<test2:TASS xmlns="http://www.vvv.com/schemas"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.vvv.com/schemas http://www.vvv.com/schemas/testV2_02_03.xsd"  xmlns:test2="http://www.vvv.com/schemas" >
                <test2:billing>
                    <test2:proceduresummary>
                        <test2:guidenumber>Z4088</test2:guidenumber>
                            <test2:diagnosis>
                                <test2:table>ICD-10</test2:table>
                                <test2:diagnosiscod>G93</test2:diagnosiscod>
                                <test2:description>DISORDER OF BRAIN, UNSPECIFIED</test2:description>
                            </test2:diagnosis>
                            <test2:procedure>
                                <test2:procedure>
                                    <test2:description>HOSPITAL</test2:description>
                                </test2:procedure>
                                <test2:amount>15</test2:amount>
                            </test2:procedure>
                    </test2:proceduresummary>
                </test2:billing>
</test2:TASS>
##myxml2
Z4088
ICD-10
G93
脑功能紊乱,未明
医院
15
要转换为DF的代码(xml=test2)

#setwd(“D:/”)
#myxml2%jsonlite::fromJSON()

我需要创建更改这些节点名称的代码。例如,我考虑使用“xml\u find\u all”之前的函数交换所有节点,因此导入所有节点将被称为“bd”,而不是“test”或“test2”。有可能吗?

因为第二个文件与第一个文件相同,只是“test:”被转换为“test2:”。一种选择是将所有xPath搜索存储为向量,然后使用
sub
函数进行替换

在这个文件中定义了名称空间。更直接的方法是重命名名称空间并重用代码,如下所示。这个方法适用于这个文件,我不能说这是完全通用的方法

library(xml2)

page<-read_xml('<?xml version="1.0" encoding="ISO-8859-1" ?>
<test2:TASS xmlns="http://www.vvv.com/schemas"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://www.vvv.com/schemas http://www.vvv.com/schemas/testV2_02_03.xsd"  
xmlns:test2="http://www.vvv.com/schemas" >
                <test2:billing>
                    <test2:proceduresummary>
                        <test2:guidenumber>Z4088</test2:guidenumber>
                            <test2:diagnosis>
                                <test2:table>ICD-10</test2:table>
                                <test2:diagnosiscod>G93</test2:diagnosiscod>
                                <test2:description>DISORDER OF BRAIN, UNSPECIFIED</test2:description>
                            </test2:diagnosis>
                            <test2:procedure>
                                <test2:procedure>
                                    <test2:description>HOSPITAL</test2:description>
                                </test2:procedure>
                                <test2:amount>15</test2:amount>
                            </test2:procedure>
                    </test2:proceduresummary>
                </test2:billing>
</test2:TASS>')
#xml_ns_strip(page)

#display the name spaces
xml_ns(page)
# d1    <-> http://www.vvv.com/schemas
# test2 <-> http://www.vvv.com/schemas
# xsi   <-> http://www.w3.org/2001/XMLSchema-instance

#rename test2 to become test to reuse existing code
ns<-xml_ns_rename(xml_ns(page), test2 = "test")
t2<-page %>% xml_find_all(".//test:billing", ns) #also works

#demonstration purposes (not needed in production code)
    t1<-page %>% xml_find_all(".//test2:billing")  # works
    t3<-page %>% xml_find_all(".//test:billing")   # fails

    identical(t1, t2)
#[1] TRUE
#end of demo
库(xml2)

页面从XML的角度来看,
test
test2
名称空间是等效的,因为它们都表示相同的URI
http://www.vvv.com/schemas
@choroba但是,当我使用xml查找所有(“.//test2:billing”)时,我必须确定路径(test或test2)。否则我会出错,我不知道R,但在其他语言中,您首先注册名称空间,然后可以引用节点,而不管名称空间在文档中使用什么前缀。如果R不一样,我会非常惊讶。您的答案很好,但我需要在这部分“t1%xml\u find\u all(“.//test2:billing”)”之前进行更改。有可能吗?对不起,我不明白这个问题。您需要更改xml文本还是只更改查询部分?
xml\n\u rename
行是添加的关键行。我编辑了上面的脚本以澄清演示部分。太好了,它成功了!。在我的工作中,我将处理这两种名称空间类型(test和test2)。我将尝试自动化代码,以确定何时从“test2”更改为“test”,何时不更改(test-to-test)。我以为我用了if_else,但没用。我会继续尝试,如果你知道任何建议,欢迎!谢谢不幸的是,我的XML文件没有太多的标准。我有另一个问题,由于您有很多知识,我希望您在以下位置检查我的问题:
#setwd("D:/")
#myxml2<- read_xml("test2.xml")
#myxml2<-myxml2 %>% xml_find_all(".//test2:billing")
#billing2<-xml2::as_list(myxml2) %>% jsonlite::toJSON() %>% jsonlite::fromJSON()
library(xml2)

page<-read_xml('<?xml version="1.0" encoding="ISO-8859-1" ?>
<test2:TASS xmlns="http://www.vvv.com/schemas"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://www.vvv.com/schemas http://www.vvv.com/schemas/testV2_02_03.xsd"  
xmlns:test2="http://www.vvv.com/schemas" >
                <test2:billing>
                    <test2:proceduresummary>
                        <test2:guidenumber>Z4088</test2:guidenumber>
                            <test2:diagnosis>
                                <test2:table>ICD-10</test2:table>
                                <test2:diagnosiscod>G93</test2:diagnosiscod>
                                <test2:description>DISORDER OF BRAIN, UNSPECIFIED</test2:description>
                            </test2:diagnosis>
                            <test2:procedure>
                                <test2:procedure>
                                    <test2:description>HOSPITAL</test2:description>
                                </test2:procedure>
                                <test2:amount>15</test2:amount>
                            </test2:procedure>
                    </test2:proceduresummary>
                </test2:billing>
</test2:TASS>')
#xml_ns_strip(page)

#display the name spaces
xml_ns(page)
# d1    <-> http://www.vvv.com/schemas
# test2 <-> http://www.vvv.com/schemas
# xsi   <-> http://www.w3.org/2001/XMLSchema-instance

#rename test2 to become test to reuse existing code
ns<-xml_ns_rename(xml_ns(page), test2 = "test")
t2<-page %>% xml_find_all(".//test:billing", ns) #also works

#demonstration purposes (not needed in production code)
    t1<-page %>% xml_find_all(".//test2:billing")  # works
    t3<-page %>% xml_find_all(".//test:billing")   # fails

    identical(t1, t2)
#[1] TRUE
#end of demo
if ("test2" %in%  names(xml_ns(page))) {
  #print(TRUE)
  ns<-xml_ns_rename(xml_ns(page), test2 = "test")
} else {
  #print(FALSE)
  ns<- xml_ns(page)
}

#this should now work for both cases.
page %>% xml_find_all(".//test:billing", ns)