在通过 R 中的 flatxml 包扁平化之前删除 XML 中的节点

Removing a node in an XML before flattening via the flatxml package in R

提问人:BleepBloop 提问时间:7/26/2022 最后编辑:PhilBleepBloop 更新时间:7/27/2022 访问量:48

问:

我有多个 XML 想要从中提取数据,这些数据是使用机器学习库从 PDF 创建的。

在 R 中使用 flatxml 包时,我在导入 XML 时遇到了问题,每当节点内有标记时,它就会在节点中的所有数据被切断。因此,在下面的示例 XML 中,所有内容都从fxml_importXMLFlat()<ref><p></ref><p>

.主要是无定形的(非结晶的......”

直到被切断。</p>

<text xml:lang="en">
<body>
<div xmlns="http://www.tei-c.org/ns/1.0">
<head n="1.">Introduction</head>
<p>
Thermal properties of confectionary products, including the melting temperature (Tm) of crystalline components and glass transition temperature (Tg) of amorphous components, as well as the crystalline to amorphous ratio, significantly impact system texture and stability
<ref type="bibr" target="#b23">(Levine and Slade, 1986)</ref>
. Predominantly amorphous (non-crystalline, disordered solid) candies are formed by heating ingredients to a set temperature and then quickly cooling the resultant supersatured sugar solution to below the temperature range in which recrystallization of sugars can occur, between Tg and Tm of the material. 
</p>
</body>
</text>

为了解决这个问题,我计划最初将文件作为 XML 导入 R,删除标签,然后通过 flatxml 扁平化。<ref>

我尝试使用XML包使用以下代码查找和删除标签:<ref>

xml1 <- read_xml("https://file.io/vAiDRi5s68Gm")

ref <- xml_find_all(xml1, "//ref")

rm(ref)

它在 ref 对象中不返回任何内容。当我在读入后查看 xml 时,它看起来也不像任何标签。<ref>

我也试过了,但似乎也没有找到标签。 xml1 <- xmltoList("https://file.io/vAiDRi5s68Gm")<ref>

我也尝试过,但出现以下错误: xml1 <- xmlToDateFrame("https://file.io/vAiDRi5s68Gm")

错误 (, i, names(nodes[[i]]), value = c(text = “\n\t\t”, : 列的重复下标[<-.data.frame*tmp*

据我了解,这是因为XML文件是超级嵌套的。

我的目标是从数百个 XML 中提取数据,因此我需要一些可以应用于所有 XML 的东西,而不仅仅是一个特定的 XML 文件。任何想法将不胜感激!

r xml xml 解析 xml2

评论

0赞 Yitzhak Khabinsky 7/26/2022
XSLT呢?
0赞 BleepBloop 7/26/2022
我对 R 和 XML 相当陌生,而且我从未使用过 XSLT,所以我不知道如何使用它来删除 <ref> 标签。您的任何建议将不胜感激。
0赞 Yitzhak Khabinsky 7/26/2022
XML 格式不正确。该元素没有结束标记。<div>
0赞 BleepBloop 7/26/2022
哎呀,实际的 XML 中有一个,我只是复制了其中的一部分,忘记在这里包含结束标记,但如果您单击上面代码中的链接,您将看到 </div>
0赞 Yitzhak Khabinsky 7/27/2022
在提出 XSLT 问题时,您需要提供一个最小的可重现示例:(1) 输入 XML。(2) 您的逻辑,以及尝试实现它的 XSLT。(3) 所需的输出,基于上面 #1 中的示例 XML。(4) XSLT 处理器及其符合 XSLT 标准:1.0、2.0 或 3.0。

答:

-1赞 Yitzhak Khabinsky 7/26/2022 #1

这是用于删除所有 ref 元素的 XSLT。

输入 XML

<text xml:lang="en">
    <body>
        <div xmlns="http://www.tei-c.org/ns/1.0">
            <head n="1.">Introduction</head>
            <p>Thermal properties of confectionary products, including the melting temperature (Tm) of crystalline components and glass transition temperature (Tg) of amorphous components, as well as the crystalline to amorphous ratio, significantly impact system texture and stability
                <ref type="bibr" target="#b23">(Levine and Slade, 1986)</ref>. Predominantly amorphous (non-crystalline, disordered solid) candies are formed by heating ingredients to a set temperature and then quickly cooling the resultant supersatured sugar solution to below the temperature range in which recrystallization of sugars can occur, between Tg and Tm of the material.</p>
        </div>
    </body>
</text>

XSLT (英语:XSLT)

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://www.tei-c.org/ns/1.0">
   <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
   <xsl:strip-space elements="*"/>

   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>

    <!-- remove ref elements -->
   <xsl:template match="ns1:ref"/>
</xsl:stylesheet>

输出 XML

<text xml:lang="en">
  <body>
    <div xmlns="http://www.tei-c.org/ns/1.0">
      <head n="1.">Introduction</head>
      <p>Thermal properties of confectionary products, including the melting temperature (Tm) of crystalline components and glass transition temperature (Tg) of amorphous components, as well as the crystalline to amorphous ratio, significantly impact system texture and stability
                . Predominantly amorphous (non-crystalline, disordered solid) candies are formed by heating ingredients to a set temperature and then quickly cooling the resultant supersatured sugar solution to below the temperature range in which recrystallization of sugars can occur, between Tg and Tm of the material.
      </p>
    </div>
  </body>
</text>

评论

0赞 BleepBloop 7/27/2022
嗯,试图应用它,我得到以下 2 个错误:“xslt 错误:运行时错误”文件 C:/.....xsl 第 7 行元素复制“和”xslt 错误:必须在向元素添加任何子节点之前添加属性节点”
0赞 Yitzhak Khabinsky 7/27/2022
我在您的原始问题下方添加了一条评论。请提供##1-4
0赞 Yitzhak Khabinsky 7/27/2022
我通过使用示例 XML 片段作为输入 XML 来更新答案。我向 XSLT 添加了命名空间处理。甚至添加了输出 XML - XSLT 转换的结果。