【问题标题】:How to write a regex to find HTML tags outside CDATA tags in an XML document如何编写正则表达式来查找 XML 文档中 CDATA 标记之外的 HTML 标记
【发布时间】:2015-06-30 21:50:08
【问题描述】:

我正在尝试导入一个 ONIX (XML) 文件,该文件由于描述性文本中的 HTML 标记而出现导入错误。在这个特定文件中,一些描述性文本包含在 CDATA 标记中,但似乎有些不是。

如何编写一个正则表达式来查找未包含在 CDATA 标记中的 HTML 标记?

我正在使用 VB.NET 应用程序将数据导入 SQL Server 数据库,但是此时我正在尝试在 Notepad++ 中编写正则表达式,以了解可能的情况。我可以稍后将正则表达式合并到 VB 代码中。

下面是一些可以正确导入的 XML 示例:

<OtherText>
  <TextTypeCode>01</TextTypeCode>
  <TextFormat>02</TextFormat>
  <Text><![CDATA[More than simply a series of chapters on the theology of John's Gospel, <em>Jesus Is the Christ</em> relates each of John's teachings to his declared aim, expressed in John 20: 30-31: "Jesus did many other signs before his disciples, which have not been written in this book; but these have been written that you may believe that Jesus is the Christ, the Son of God, and that believing you may have life in his name." Indeed, each chapter in Morris's book takes up some facet or aspect of John's expressed aim.<br/><br/>For an age still asking the question "Who is Jesus?" Leon Morris argues convincingly that John's entire Gospel was written to show that the human Jesus is the Christ, or Messiah, as well as the Son of God. But it is Morris's firm conviction that John's purpose was evangelical as well as theological -- that is, John wrote his book so that readers might believe in Christ and as a result have eternal life.]]></Text>
</OtherText>

这是无法正确导入的 XML:

<OtherText>
  <TextTypeCode>01</TextTypeCode>
  <TextFormat>02</TextFormat>
  <Text>More than simply a series of chapters on the theology of John's Gospel, <em>Jesus Is the Christ</em> relates each of John's teachings to his declared aim, expressed in John 20: 30-31: "Jesus did many other signs before his disciples, which have not been written in this book; but these have been written that you may believe that Jesus is the Christ, the Son of God, and that believing you may have life in his name." Indeed, each chapter in Morris's book takes up some facet or aspect of John's expressed aim.<br/><br/>For an age still asking the question "Who is Jesus?" Leon Morris argues convincingly that John's entire Gospel was written to show that the human Jesus is the Christ, or Messiah, as well as the Son of God. But it is Morris's firm conviction that John's purpose was evangelical as well as theological -- that is, John wrote his book so that readers might believe in Christ and as a result have eternal life.</Text>
</OtherText>

现在,

<TextFormat>02</TextFormat> 

表示标签的内容是HTML,所以我可以处理。当我有未正确标记的标签时,问题就出现了。我需要找到这些,以便更正它们。

【问题讨论】:

  • Regex 可能不是这项工作的正确工具。您需要的是一个 XML / HTML 解析器,它将容忍格式(我不清楚它是否格式不正确,或者只是对模式的验证失败)并让您重新编码适当的部分。跨度>
  • 请发布一个您打算匹配正则表达式的 xml 行示例。
  • 基本上这就是我要写的。 ONIX 标准允许某些字段在其中包含 HTML,因此我根据它们的属性查找这些字段并插入 CDATA 标记。通常完成此操作后,我可以将 XML 导入数据集,一切正常,但偶尔我会遇到包含不符合条件的标签的文件,因此会出现导入错误。
  • 不符合什么条件?你的意思是你有一些现有的代码?如果是这样,请将其包含在您的问题中,并附上一个明确的例子来说明它在哪里不起作用。人们更有可能帮助您改进代码,而不是为您编写代码。
  • 我的建议是放弃这种思路,因为正则表达式通常不适合处理 XML 的递归结构。

标签: html regex xml notepad++


【解决方案1】:

这个正则表达式可以帮助你到达某个地方:

<\w+>(?!<![CDATA[)

我在您在 Sublime Text 中提供的示例上运行它,它只匹配没有 CDATA 内容的 HTML 标记。

【讨论】:

  • 谢谢你——我不得不转义方括号才能让它在 Notepad++ 中工作,就像这样:&lt;\w+&gt;(?!&lt;\[CDATA\[) 然后它可以正常工作,但是它也能找到所有的 XML 标记!我将如何修改它以仅查找 标签?
  • 如果我正确理解您的问题,您可以将 \w+ 替换为 em。
  • 谢谢 - 我自己应该知道的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-29
  • 1970-01-01
  • 2015-02-08
  • 2013-08-12
  • 2018-05-24
  • 2010-12-10
  • 1970-01-01
相关资源
最近更新 更多