我有一个文档,其中我必须检查文档是否有一个名为"Name“的标题。如何使用数据编织来检查这个问题?
文件示例:
<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>发布于 2022-07-03 12:17:04
您可以在Mule 4中使用来自xpath-extract的xml-module,也可以使用它来根据负载评估任何XPath。例如,下面的XPath将适用于您。
//*[matches(name(), '^h[1-6]$') and (text() = 'Name')]
它将匹配与regex ^h[1-6]$匹配的所有标记,因此h1与h6匹配,并将值( text() )作为Name。如果只想查找h2,则可以相应地更新regex部件。
<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()的输出。
发布于 2022-07-03 13:20:58
DataWeave不支持HTML作为一种格式,但是它确实支持XML。如果HTML输入有一个根标记,您可以将其解析为XML,并使用一个递归函数来查找它是否具有键名和值匹配。
%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")输入:
<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
https://stackoverflow.com/questions/72845652
复制相似问题