如何获取包含某些字符串的某些节点的属性值? [英] How to get attribute values of certain nodes containing some string?

查看:84
本文介绍了如何获取包含某些字符串的某些节点的属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些xml文件,其中有一些节点(如其他节点)

< disp-formula id =" deqn1"> 
... \ tag {1}
< / disp-formula>
< disp-formula id =" deqn2">
... \ tag {2}
< / disp-formula>
< disp-formula id =" deqn3-6">
... \ tag {3}
...
... \ tag {4}
... \ tag {5} ...
...
...... \ tag {6}
< / disp-formula>

...等和节点如

< xref ref-type =" disp-formula" RID = QUOT; deqn5">(5)< /外部参照> 
< xref ref-type =" disp-formula" RID = QUOT; deqn2"> 2'; /外部参照>
< xref ref-type =" disp-formula" rid =" deqn4"> 3,4< / xref>
< xref ref-type =" disp-formula" rid =" deqn6">(5) - (6)< / xref>

我想修改rid属性值通过使用以下逻辑排除具有属性
ref-type =" disp-formula" 的节点的字符串 deqn


1)获取具有 ref-type =" disp-属性的节点
xref
的内容(仅限数字或alpha_numeric值)公式"


2)检查文件中是否存在相同的值是否存在于字符串
\ tag {}
中在节点内部  < disp-formula> ,其中包含名为
id 的属性,


3)如果是,则获取 id 值并将其粘贴到相应节点< xref>


<的
rid
属性中p>文件中的修改应该是

< xref ref-type =" disp-formula" RID = QUOT; deqn3-6">(5)< /外部参照> 
< xref ref-type =" disp-formula" RID = QUOT; deqn2"> 2'; /外部参照>
< xref ref-type =" disp-formula" rid =" deqn3-6"> 3,4< / xref>
< xref ref-type =" disp-formula" rid =" deqn3-6">(5) - (6)< / xref>



我一开始就陷入困境并且没有想法

 public static void Main(string [] args)
{
XDocument doc = XDocument.Load(@" C:\ Users \sample.xml");
var eqIDS =来自doc.Descendants(" xref")中的x
其中x.Attribute(" ref-type")。Value ==" disp-formula"
let _x = x.Attribute(" rid")。Value.ToString()。Substring(4)
select _x;

if(eqIDS.Any())
{
foreach(eqIDS中的var元素)
{
????????
}
}
其他
{
Console.WriteLine("找不到匹配项");
}
Console.ReadLine();
}



任何人都可以帮助!!!


解决方案

样本输入和样本输出不基于对齐根据你的规则。您提到您要查找具有给定值的标记(在我假设的deqn之后)。对于行deqnc,您没有提供任何匹配的输入值,但您的输出自动神奇
将其映射到3-6。由于您没有指定此范围如何工作且没有任何样本输入,因此很难理解您的实际规则,因为算法不完整。


这里是您将遵循的一般算法仅基于您使用定义规则的初始值集。


选择所有称为disp-formula的节点

对于每个节点

  &NBSP;确定将映射到它的标签

  &NBSP;将标记作为键添加到字典中,并将值设置为id属性


使用ref-type of disp-formula选择所有名为xref的节点,使用XPath

节点 

  ;&NBSP;获取跳过'deqn'开始字符串的值

  在前面创建的字典中查找计算值

  如果找到该键,那么该值是rid属性的新值

  否则??


I have some xml files that have some nodes like (among other nodes)

<disp-formula id="deqn1">
...\tag{1}
</disp-formula>
<disp-formula id="deqn2">
...\tag{2}
</disp-formula>
<disp-formula id="deqn3-6">
...\tag{3}
...
...\tag{4}
...\tag{5}...
...
......\tag{6}
</disp-formula>

... etc and nodes like

<xref ref-type="disp-formula" rid="deqn5">(5)</xref>
<xref ref-type="disp-formula" rid="deqn2">2</xref>
<xref ref-type="disp-formula" rid="deqn4">3, 4</xref>
<xref ref-type="disp-formula" rid="deqn6">(5)-(6)</xref>

I want to modify the rid attribute values excluding the string deqn of the nodes which also have the attribute ref-type="disp-formula" by using the following logic:

1) Take the contents (only the numeric or alpha_numeric values) of the node xref which have the attribute ref-type="disp-formula",

2) Check the file for that same value whether it exists inside a string \tag{} which is inside a node  <disp-formula> with an attribute named id,

3) If yes, then get that id value and paste it in the rid attribute of the respective node <xref>

The modifications in the file should be

<xref ref-type="disp-formula" rid="deqn3-6">(5)</xref>
<xref ref-type="disp-formula" rid="deqn2">2</xref>
<xref ref-type="disp-formula" rid="deqn3-6">3, 4</xref>
<xref ref-type="disp-formula" rid="deqn3-6">(5)-(6)</xref>


I'm stuck at the very beginning and running out of ideas

public static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(@"C:\Users\sample.xml");
            var eqIDS=from x in doc.Descendants("xref")
                where x.Attribute("ref-type").Value=="disp-formula"
                let _x=x.Attribute("rid").Value.ToString().Substring(4)
                select _x;

            if (eqIDS.Any())
            {
                foreach (var element in eqIDS)
                {
                    ????????
                }
            }
            else
            {
                Console.WriteLine("NO matches found");
            }
            Console.ReadLine();
        }


Can anyone help!!!

解决方案

The sample input and the sample output do not line up based upon your rules. You mentioned that you look for a tag with the given value (after the deqn I assume). For line deqnc you didn't provide any matching input values yet your output auto-magically mapped it to 3-6. Since you didn't specify how this range works and there isn't any sample input it is hard to understand your actual rules because the algorithm isn't complete.

Here's the general algorithm you would follow based solely upon your initial set of values that work with your defined rules.

Select all nodes called disp-formula
For each node
    Determine the tag(s) that will map to it
    Add the tag(s) as keys to a dictionary with the value set to the id attribute

Select all nodes called xref with a ref-type of disp-formula, using XPath
For each of the nodes 
   Get the value of rid skipping the 'deqn' start string
   Look up the calculated value in the dictionary created earlier
   If the key is found then the value is the new value of the rid attribute
   Otherwise ??


这篇关于如何获取包含某些字符串的某些节点的属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆