【问题标题】:Convert in R a XML with ASCII Entity Names to a basic XML在 R 中将具有 ASCII 实体名称的 XML 转换为基本 XML
【发布时间】:2021-05-30 15:35:42
【问题描述】:

我有以下 XML 文件:

<?xpacket begin="???" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.4-c006 80.159825, 2016/09/16-03:31:08        ">
   <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
      <rdf:Description rdf:about=""
            xmlns:xmp="http://ns.adobe.com/xap/1.0/"
            xmlns:pdfx="http://ns.adobe.com/pdfx/1.3/"
            xmlns:pdf="http://ns.adobe.com/pdf/1.3/"
            xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/"
            xmlns:pdfx_1_="ns.adobe.org/pdfx/1.3/">
         <xmp:CreateDate>2021-05-30T11:17:35+02:00</xmp:CreateDate>
         <xmp:CreatorTool>TeX</xmp:CreatorTool>
         <xmp:ModifyDate>2021-05-30T12:12:25+02:00</xmp:ModifyDate>
         <xmp:MetadataDate>2021-05-30T12:12:25+02:00</xmp:MetadataDate>
         <pdfx:PTEX.Fullbanner>This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019) kpathsea version 6.3.1</pdfx:PTEX.Fullbanner>
         <pdf:Producer>pdfTeX-1.40.20</pdf:Producer>
         <pdf:Trapped>Unknown</pdf:Trapped>
         <pdf:Keywords/>
         <dc:format>application/pdf</dc:format>
         <xmpMM:DocumentID>uuid:38d0617c-0385-5941-a87d-cc4a1e54bd76</xmpMM:DocumentID>
         <xmpMM:InstanceID>uuid:d056c61c-55c6-5f44-8c0e-fe6e911c2ed9</xmpMM:InstanceID>
         <pdfwe:dafra>&#xA;&#xA;&lt;?xml version="1.0"?&gt;&#xA;&#x9;&lt;dataframe name="expData" &#xA;&#x9;&#x9;xmlns="url"&#xA;&#x9;&#x9;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&#xA;&#x9;&#x9;xsi:schemaLocation="url"&gt;&#xA;&#x9;&#x9;&lt;column name="DATA" type="ratio"&gt;&#xA;&#x9;&#x9;&#x9;&lt;value&gt;14&lt;/value&gt;&#xA;&#x9;&#x9;&#x9;&lt;value&gt;18&lt;/value&gt;&#xA;&#x9;&#x9;&#x9;&lt;value&gt;21&lt;/value&gt;&#xA;&#x9;&#x9;&#x9;&lt;value&gt;35&lt;/value&gt;&#xA;&#x9;&#x9;&#x9;&lt;value&gt;44&lt;/value&gt;&#xA;&#x9;&#x9;&#x9;&lt;value&gt;50&lt;/value&gt;&#xA;&#x9;&#x9;&#x9;&lt;value&gt;3&lt;/value&gt;&#xA;&#x9;&#x9;&#x9;&lt;value&gt;5&lt;/value&gt;&#xA;&#x9;&#x9;&#x9;&lt;value&gt;7&lt;/value&gt;&#xA;&#x9;&#x9;&lt;/column&gt;&#xA;&#x9;&lt;/dataframe&gt;&#xA;&#xA;&#x9;&#x9;&#x9;</pdfx_1_:Dataframe>
      </rdf:Description>
   </rdf:RDF>
</x:xmpmeta>
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                           
<?xpacket end="w"?>

如您所见,命名空间pdfwe 的标记Dataframe 在其中包含另一个XML。我需要提取此 XML 并将其转换为没有 ASCII 实体名称的普通 XML,如下所示:

<?xml version="1.0"?>
    <dataframe name="expData" 
        xmlns="url"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="url">
        <column name="DATA" type="ratio">
            <value>14</value>
            <value>18</value>
            <value>21</value>
            <value>35</value>
            <value>44</value>
            <value>50</value>
            <value>3</value>
            <value>5</value>
            <value>7</value>
        </column>
    </dataframe>

为了提取 pdfwe:dafra 中的内容,我使用了 xml2 包的函数 xml_find_all(x, ".//pdfwe:dafra"),但没有得到我想要的结果。

为了转换实体名称,我使用了函数xml2::xml_text(xml2::read_xml(paste0("&lt;x&gt;", md, "&lt;/x&gt;"))),但我也没有得到我想要的结果。

提前致谢!

【问题讨论】:

    标签: r xml dataframe ascii entity


    【解决方案1】:

    解决方案是一个多步骤过程,提取数据库节点,转换为文本,清理然后使用read_xml() 函数转换回xml。

    library(xml2)
    
    page <- read_xml('<?xpacket begin="???" id="W5M0MpCehiHzreSzNTczkc9d"?>
    <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.4-c006 80.159825, 2016/09/16-03:31:08">
       .....')  #read in the entire file
    
    xml_ns(page)  #show namespaces
    
    #extract the database
    db <- xml_find_first(page, ".//pdfx_1_:Dataframe") 
    #convert to text and strip leading whitespace
    dbtext <- xml_text(db) %>% trimws()
    
    #read the text in and convert to xml
    xml_db <- read_xml(dbtext)
    xml_ns(xml_db)  #show namespaces
    
    #extract the requested information from database
    #shown here for demonstration purposes
    xml_db %>% xml_find_all(".//d1:column") %>% xml_find_all(".//d1:value") %>% xml_text()
    

    【讨论】:

    • 非常感谢!这就是我所需要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2011-03-19
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    相关资源
    最近更新 更多