在这篇文章中,我们将带领您了解删除具有特定CDATA的xml节点的全貌,包括删除包含特定字符的行的相关情况。同时,我们还将为您介绍有关Ansible-从列表中删除具有特定键值的字典、c#–在.NET2
在这篇文章中,我们将带领您了解删除具有特定CDATA的xml节点的全貌,包括删除包含特定字符的行的相关情况。同时,我们还将为您介绍有关Ansible - 从列表中删除具有特定键值的字典、c# – 在.NET 2中搜索具有特定属性值的节点的XML文件、c# – 将多个类似的XML节点附加到XML文档、javascript – 将特定的XML节点拉入HTML文档的知识,以帮助您更好地理解这个主题。
本文目录一览:- 删除具有特定CDATA的xml节点(删除包含特定字符的行)
- Ansible - 从列表中删除具有特定键值的字典
- c# – 在.NET 2中搜索具有特定属性值的节点的XML文件
- c# – 将多个类似的XML节点附加到XML文档
- javascript – 将特定的XML节点拉入HTML文档
删除具有特定CDATA的xml节点(删除包含特定字符的行)
首先您的xml损坏:第二个标签GoalMeasurementEORow
没有关闭,并且由于它只是xml的一部分,因此它没有根标签。
无论如何,我已经修复了您的XML,以显示如何从XML中删除cdata
。
请查看以下示例:
-- sample data:
with t(x) as (
select xmltype(q'{
<ROOT>
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<![CDATA[0001FFFFFFFF]]>
<GoalMeasurementEORow PS="0" Hdl="16" PK="Y" CI="Y" AV="111111111111111111111111111111">
<MeasurementId>
<DATA null="true"/>
</MeasurementId>
</GoalMeasurementEORow>
</EO>
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<![CDATA[00010000000EACED00057708000110D943311B2F]]>
<GoalMeasurementEORow PS="0" Hdl="1019" PK="Y" CI="Y" AV="">
<MeasurementId>
<DATA>300000297949999</DATA>
</MeasurementId>
<BusinessGroupId>
<DATA>1</DATA>
</BusinessGroupId>
</GoalMeasurementEORow>
</EO>
</ROOT>
}')
from dual)-- end of sample data
-- main query:
select
XMLQuery(
'copy $i := $p1 modify
(for $j in $i//EO
let $o := <EO >{$j/@*}{ $j/node()[name() ne ""]}</EO>
return replace node $j with $o
)
return $i'
PASSING t.x AS "p1"
returning content
) xdata_new
from t;
结果:
XDATA_NEW
----------------------------------------------------------------------------------
<ROOT>
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<GoalMeasurementEORow PS="0" Hdl="16" PK="Y" CI="Y" AV="111111111111111111111111111111">
<MeasurementId>
<DATA null="true"/>
</MeasurementId>
</GoalMeasurementEORow>
</EO>
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<GoalMeasurementEORow PS="0" Hdl="1019" PK="Y" CI="Y" AV="">
<MeasurementId>
<DATA>300000297949999</DATA>
</MeasurementId>
<BusinessGroupId>
<DATA>1</DATA>
</BusinessGroupId>
</GoalMeasurementEORow>
</EO>
</ROOT>
如您所见,我用其内容替换了所有子节点EO
,CDATA消失了。
Ansible - 从列表中删除具有特定键值的字典
JMESPath 适用于简单的问题,但很难解决复杂的问题,尤其是因为您的最终问题涉及选择性地构建一个新的“用户”字典(或改变 var,很难判断您的结果是什么) d 想要)。如果您希望原始数据发生变异,只需删除克隆用户字典的 | combine({})
- name: Filter users by group name
set_fact:
filtered_users: >-
{%- set results = [] -%}
{%- for ur in users -%}
{%- set u = ur | combine({}) -%}
{%- set g1 = u.groups | selectattr("name","eq",search_name) -%}
{%- if g1 | length > 0 -%}
{%- set _ = u.update({"groups": g1}) -%}
{%- set _ = results.append(u) -%}
{%- endif -%}
{%- endfor -%}
{{ results }}
c# – 在.NET 2中搜索具有特定属性值的节点的XML文件
我想打开一个XML文件(〜50Kb,所有简单的文本),并搜索所有< Tool>属性名称具有特定值的节点.
似乎XmlDocument.SelectNodes()可能是我正在寻找,但我不知道XPath.这是正确的方法,如果是这样的代码看起来像什么?
解决方法
<root> <element name="value1" /> <element name="value2" /> <element name="value1" /> </root> XmlDocument xDoc = new XmlDocument(); // Load Xml XmlNodeList nodes = xDoc.SelectNodes("//element[@name='value1']"); // nodes.Count == 2
Here你可以找到一些额外的XPath样本
c# – 将多个类似的XML节点附加到XML文档
<Reply Document> <ConfirmationItem name = "One"> <ItemDetail /> </ConfirmationItem> <ConfirmationItem name = "Two"> <ItemDetail /> </ConfirmationItem> ... <ConfirmationItem name = "Twenty"> <ItemDetail /> </ConfirmationItem> </Reply Document>
我做了一些研究并找到了这个帖子:XmlReader AppendChild is not appending same child value,其中接受的答案是OP必须创建新元素才能追加到最后而不是覆盖第一个.
我的原始代码如下,它从传入的Request创建XMLNode并将结果附加到XmlDocument本身:
//p_transdoc is the XmlDocument that holds all the items to process. XmlNodeList nodelst_cnfrm = p_transdoc.SelectNodes("//OrderRequest"); foreach (XmlNode node in nodelst_cnfrm) { //this is just an XML Object XmlNode node_cnfrm_itm = this.CreateElement("ConfirmationItem"); node_cnfrm_itm.Attributes.Append(this.CreateAttribute("name")).InnerText = p_transdoc.Attributes["name"].InnerText; XmlNode node_itmdtl = this.CreateElement("ItemDetail"); node_cnfrm_itm.AppendChild(node_itmdtl); //xml_doc is the return XML request xml_doc.AppendChild(node_cnfrm_itm); }
因此,在阅读该线程和答案后,我尝试更改代码以使用每个传递的新XmlElement.
//p_transdoc is the XmlDocument that holds all the items to process. XmlNodeList nodelst_cnfrm = p_transdoc.SelectNodes("//OrderRequest"); foreach (XmlNode node in nodelst_cnfrm) { XmlElement node_cnfrm_itm = new XmlElement(); node_cnfrm_itm = this.CreateElement("ConfirmationItem"); node_cnfrm_itm.Attributes.Append(this.CreateAttribute("name")).InnerText = p_transdoc.Attributes["name"].InnerText; XmlElement node_itmdtl = new XmlElement(); node_itmdtl = this.CreateElement("ItemDetail"); node_cnfrm_itm.AppendChild(node_itmdtl); //xml_doc is the return XML request xml_doc.AppendChild(node_cnfrm_itm); }
但这不仅无效,还会返回服务器错误.所以我来找你帮忙.现在这段代码只返回一个ConfirmationItem.我如何能够将ConfirmationItem附加到Document的末尾而不是覆盖它,以便能够返回尽可能多的数量?
(我应该指出,这段代码经过大量格式化,易于阅读,简单,并且可以减少混乱.任何印刷错误都纯粹是因为Asker在有效校对时出现内部故障).
解决方法
// dummy data XmlDocument p_transdoc = new XmlDocument(); p_transdoc.LoadXml(@" <root name='rootAttribute'> <OrderRequest name='one' /> <OrderRequest name='two' /> <OrderRequest name='three' /> </root> "); XmlDocument xml_doc = new XmlDocument(); xml_doc.LoadXml("<ReplyDocument />"); foreach (var node in p_transdoc.SelectNodes("//OrderRequest").OfType<XmlElement>()) { XmlElement node_cnfrm_itm = xml_doc.CreateElement("ConfirmationItem"); node_cnfrm_itm = xml_doc.DocumentElement.AppendChild(node_cnfrm_itm) as XmlElement; node_cnfrm_itm.SetAttribute("name",node.GetAttribute("name")); XmlElement node_itmdtl = xml_doc.CreateElement("ItemDetail"); node_itmdtl = node_cnfrm_itm.AppendChild(node_itmdtl) as XmlElement; }
CreateElement方法返回一个XmlElement,因此您可以使用方法SetAttribute和GetAttribute.
代码:p_transdoc.Attributes [“name”].InnerText似乎不正确.如果要获取文档根元素的属性,则需要键入:p_transdoc.DocumentElement.GetAttribute(“name”)
如果您使用Linq to XML,IMO会更容易.
在Linq to XML中,这类似于(某些变量具有不同的名称):
// dummy data var transDoc = XDocument.Parse(@" <root name='rootAttribute'> <OrderRequest name='one' /> <OrderRequest name='two' /> <OrderRequest name='three' /> </root>"); var xmlDoc = XDocument.Parse("<ReplyDocument />"); xmlDoc.Root.Add( transDoc.Root.Elements("OrderRequest").Select(o => new XElement("ConfirmationElement",new XAttribute("name",(string)o.Attribute("name")),new XElement("ItemDetail"))));
两个例子都输出:
<ReplyDocument> <ConfirmationElement name="one"> <ItemDetail /> </ConfirmationElement> <ConfirmationElement name="two"> <ItemDetail /> </ConfirmationElement> <ConfirmationElement name="three"> <ItemDetail /> </ConfirmationElement> </ReplyDocument>
javascript – 将特定的XML节点拉入HTML文档
这个主题可能类似于其他几个问题,但据我所知,这是我试图解决的一个独特问题. FWIW,我主要是设计师;我对Flash和HTML以及CSS很危险,但在其他一切杂草中都有一点危险.从搜索周围,我似乎正在寻找一个ajax / jquery解决方案,我需要一些帮助.
首先,我构建了一个相当复杂的Flash接口,该接口从格式良好且经过验证的XML文件中提取内容.这部分完成,效果很好.
但是,客户端希望为没有Flash的查看者创建一个简单的(r)javsScript版本的接口.并且,他们希望此版本从SAME XML文件中提取值(文本),因此他们只需编辑一个文件即可进行更改.
似乎是一个合理的请求,但执行它似乎并不那么简单.最大的障碍是我不想循环XML输出 – 我想将特定的节点值调用到特定的元素中.
这是一个简单的例子,我可以鼓起勇气.从XML这样:
<section> <page> <name image="image1.jpg">Section One</name> <copy>This is a description of section one.</copy> </page> <page> <name image="image2.jpg">Section Two</name> <copy>This is a description of section two.</copy> </page> </section>
我想像这样创建HTML:
<div id="greenBackground"> <h1>[value from 1st XML <name> node]</h1> <p>[value from 1st XML <copy> node]</p> <img src="[value from 1st XML <name> node image attribute]" /> </div> <div id="blueBackground"> <h1>[Value of 2nd XML <name> node]</h1> <p>[Value of 2nd XML <copy> node]</p> <img src="[value from 2nd XML <name> node image attribute]" /> </div>
它们的关键是每个div都有不同的id,以便每个’page’具有独特的背景颜色和格式.我的想法是,我将使用一个简单的重叠div脚本来显示/隐藏同一足迹内的每个“部分”.
希望这是有道理的,请不要犹豫澄清.任何和所有的帮助表示赞赏.
解决方法
这是xml :(将div id添加到xml中)
<?xml version="1.0" encoding="utf-8"?> <section> <page> <div id="greenBackground"></div> <name image="image1.jpg">Section One</name> <copy>This is a description of section one.</copy> </page> <page> <div id="blueBackground"></div> <name image="image2.jpg">Section Two</name> <copy>This is a description of section two.</copy> </page> </section>
这是xslt:
<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <xsl:for-each select="section/page"> <div> <xsl:attribute name="id"> <xsl:value-of select="div//@id"/> </xsl:attribute> <h1> <xsl:value-of select="name"/> </h1> <p> <xsl:value-of select="copy"/> </p> <img> <xsl:attribute name="src"> <xsl:value-of select="name//@image"/> </xsl:attribute> </img> </div> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>
以下是运行转换后的输出:
<html> <body> <div id="greenBackground"> <h1>Section One</h1> <p>This is a description of section one.</p><img src="image1.jpg"></div> <div id="blueBackground"> <h1>Section Two</h1> <p>This is a description of section two.</p><img src="image2.jpg"></div> </body> </html>
请享用!
关于删除具有特定CDATA的xml节点和删除包含特定字符的行的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Ansible - 从列表中删除具有特定键值的字典、c# – 在.NET 2中搜索具有特定属性值的节点的XML文件、c# – 将多个类似的XML节点附加到XML文档、javascript – 将特定的XML节点拉入HTML文档的相关知识,请在本站寻找。
本文标签: