首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否有任何方法可以使用数据编织2.0或Mule 4在HTML文档中找到特定的标题

是否有任何方法可以使用数据编织2.0或Mule 4在HTML文档中找到特定的标题
EN

Stack Overflow用户
提问于 2022-07-03 10:15:41
回答 2查看 39关注 0票数 -1

我有一个文档,其中我必须检查文档是否有一个名为"Name“的标题。如何使用数据编织来检查这个问题?

文件示例:

代码语言:javascript
复制
<h2 id="name">Name</h2>
<p>This Anypoint Template should serve as a foundation for setting an online sync of accounts from a Salesforce instance to many destination systems, using the Publish-subscribe pattern. Every time there is a new account or a change in an already existing one, the integration will poll for changes in the Salesforce source Org, publish the changes to a JMS topic and each subscriber will be responsible for updating the accounts in the target systems.</p>
EN

回答 2

Stack Overflow用户

发布于 2022-07-03 12:17:04

您可以在Mule 4中使用来自xpath-extractxml-module,也可以使用它来根据负载评估任何XPath。例如,下面的XPath将适用于您。

//*[matches(name(), '^h[1-6]$') and (text() = 'Name')]

它将匹配与regex ^h[1-6]$匹配的所有标记,因此h1h6匹配,并将值( text() )作为Name。如果只想查找h2,则可以相应地更新regex部件。

代码语言:javascript
复制
<xml-module:xpath-extract
    doc:name="Xpath extract"
    doc:id="4b04a662-98cb-4dac-b4b5-18f8a9c6604e"
    xpath="//*[matches(name(), '^h[1-6]$') and (text() = 'Name')]">
</xml-module:xpath-extract>

该模块将返回一个字符串数组,其中包含xpath的结果。您可以检查这个isEmpty()的输出。

票数 0
EN

Stack Overflow用户

发布于 2022-07-03 13:20:58

DataWeave不支持HTML作为一种格式,但是它确实支持XML。如果HTML输入有一个根标记,您可以将其解析为XML,并使用一个递归函数来查找它是否具有键名和值匹配。

代码语言:javascript
复制
%dw 2.0
output application/java
import every from dw::core::Arrays
import someEntry from dw::core::Objects
fun findString(x, keyName, s)= x  match {
    case o is Object -> o mapObject (($$): if ($$ as String == keyName and $ == s) true else  findString($, keyName, s) ) someEntry  (value, key) -> value == true
    case s1 is String -> s1
    case is Number -> false
    case is Boolean -> false
    case a is Array -> !((a map findString($, s)) every ($ == false)) // should not happen in XML
    case is Null -> false
    else -> false
  } 
---
findString(payload, "h2", "Name")

输入:

代码语言:javascript
复制
<html>    
  <body>
    <h2 id="name">Name</h2>
    <p>This Anypoint Template should serve as a foundation for setting an online sync of accounts from a Salesforce instance to many destination systems, using the Publish-subscribe pattern. Every time there is a new account or a change in an already existing one, the integration will poll for changes in the Salesforce source Org, publish the changes to a JMS topic and each subscriber will be responsible for updating the accounts in the target systems.</p>
  </body>
</html>

输出:true

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72845652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档