简体   繁体   English

xpath 只需选择相关节点

[英]xpath just select the relevant nodes

Hi i've got some xml files, I'm trying to use xpath by getting the "mapfile" attribute value from the xml (shown below), then getting all the "key" values from the external mapfile, which I can do no problem.嗨,我有一些 xml 文件,我正在尝试通过从 xml 中获取“mapfile”属性值(如下所示)来使用 xpath,然后从外部映射文件中获取所有“键”值,我不能这样做问题。

Then I need to get all the "key" numbers from the xml file below that correspond to this mapfile.然后我需要从下面的 xml 文件中获取与此映射文件相对应的所有“关键”数字。 The problem is I can get them all out, but I need to get them out and check against each file individually, not the whole lot.问题是我可以将它们全部取出,但我需要将它们取出并单独检查每个文件,而不是全部。

the xml is split up into "listsections" each "listsection" has a map file. xml 被分成“listsections”,每个“listsection”都有一个映射文件。 I want to check the "key" values in the "listsection" against the "key" values in the map file.我想根据映射文件中的“键”值检查“列表部分”中的“键”值。

so what i've tried is所以我试过的是

//get the list of mapfiles
XmlNodeList mapfiles = xmlDoc.SelectNodes("//a:listsection/a:views/a:explodeddiagram", nsmgr);

//get the mapfiles
foreach (XmlNode mapfile in mapfiles)
{
            string mapfilename = mapfile.Attributes["mapfile"].Value;

        //load the mapfile and read it and get the key values from it.
        XmlDocument xmlDockey = new XmlDocument();
        xmlDochot.Load(parentdir + "\\" + mapfilename);
        XmlNodeList xmlkeyfrommapfile = xmlDockey.SelectNodes("//*[name()='area'][@key]");

        foreach (XmlNode keynode in xmlkeyfrommapfile)
        {
                string keyTextmapfile = keynode.Attributes["key"].Value;
                //So here i've got all the values I need for each "mapfile"
                list1.Add(keyTextmapfile );
        }

}

 //now I get the values of the "key" from the xml file (xml shown below)
 //get the values for the keys from the list file (xml shown below)
 XmlNodeList keysfromlistfile = xmlDoc.SelectNodes("//*[name()='key']");
 foreach (XmlNode keyfromlistfile in keysfromlistfile)
 {
                            string keyTextlistfile = keyfromlistfile.InnerText;
                            list2.Add(keyTextlistfile);
 }

So to reitterate: I get the whole lot.所以重申:我得到了全部。 I just want to check each file for the "key" values in the same "listsection" There's multiple "listsections" sometimes over 20.我只想在同一个“listsection”中检查每个文件的“key”值,有时有多个“listsections”超过 20 个。

<Thislist>
 <listsection>
  <views>
   <explodeddiagram title="Some image title1" graphicfile="SomeFilename1.tif" mapfile="SomeFilename1.xml" />
  </views>
   <rows>
    <row>
     <key>1</key>
     <remark>(Generic Remark 1)</remark>
        <part>
         <number>25376.0000</number>
         <description>Generic Description</description>
       </part>
     <qty>1</qty>
    </row>
    <row>
    <key>2</key>
     <remark>(Generic Remark 4)</remark>
        <part>
        <number>253767890002</number>
        <description>Generic Description</description>
       </part>
     <qty>1</qty>
    </row>
    <row>
     <key>2</key>
     <remark>(Generic Remark 3)</remark>
       <part>
        <number>253764560001</number>
        <description>Generic Description</description>
       </part>
     <qty>1</qty>
    </row>
    <row>
     <key>3</key>
     <remark>(Generic Remark 5)</remark>
       <part>
        <number>209213231004</number>
        <description>Generic Description</description>
       </part>
     <qty>1</qty>
    </row>
   </rows>
 </listsection>
<listsection>
  <views>
    <explodeddiagram title="Some image title2" graphicfile="SomeFilename2.tif" mapfile="SomeFilename2.xml" />
  </views>
   <rows>
    <row>
     <key>1</key>
     <remark>(Generic Remark 6)</remark>
       <part>
        <number>25376656560000</number>
        <description>Cover Assy, Top SST</description>
       </part>
     <qty>1</qty>
    </row>
    <row>
     <key>2</key>
     <remark>(Generic Remark 7)</remark>
       <part>
        <number>2537677902</number>
        <description>Generic Description</description>
       </part>
     <qty>1</qty>
    </row>
    <row>
     <key>2</key>
     <remark>(Generic Remark 8)</remark>
       <part>
        <number>25376457881</number>
        <description>Generic Description</description>
       </part>
     <qty>1</qty>
    </row>
    <row>
     <key>3</key>
     <remark>(Generic Remark 9)</remark>
       <part>
        <number>209288004</number>
        <description>Generic Description</description>
       </part>
     <qty>1</qty>
    </row>
   </rows>
 </listsection>
</Thislist>

Example of mapfile地图文件示例

<?xml version="1.0" encoding="utf-8"?>
<mapfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="hotspot.xsd">
  <map name="SomeMapName">
    <area shape="rect" coords="169,210,199,252" key="3" />
    <area shape="rect" coords="2094,881,2124,924" key="1" />
    <area shape="rect" coords="2094,1031,2124,1074" key="2" />
    <area shape="rect" coords="2094,1145,2124,1187" key="6" />
  </map>
</mapfile>

I figured it out, I needed to我想通了,我需要

  1. Select each section node in Thislist xml file选择Thislist xml文件中的每个节节点
  2. Get the mapfile values foreach section in the Thislist xml file.获取 Thislist xml 文件中每个部分的映射文件值。
  3. Read all the key values in the mapfiles for that section into a list将该部分的映射文件中的所有键值读入列表
  4. Get all the key values from Thislist xml file for that section into a list从该部分的 Thislist xml 文件中获取所有键值到列表中
  5. Compare the two lists and see if there are any that are missing比较两个列表,看看是否有任何遗漏
  6. If there are some that are missing flag it up如果有一些丢失的标记出来

     // namespace var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("a", "http://tempuri.org/XMLSchema.xsd"); //get every section in "Thislist" xml file. XmlNodeList sections = xmlDoc.SelectNodes("//a:section", nsmgr); foreach (XmlNode section in sections) { //Get all the explodeddiagrams out for the section XmlNodeList Views = section.SelectNodes("a:views/a:explodeddiagram", nsmgr); foreach (XmlNode ExplodedDiagram in Views) { //get the mapfile attribute which gives us the explodeddiagram filename string mapfile = ExplodedDiagram.Attributes["mapfile"].Value; //load the mapfile as xmlDocmap XmlDocument xmlDocmap = new XmlDocument(); xmlDocmap.Load(mapfile); //go through everymap file in the section and get the key values. XmlNodeList xmlmapkeylist = xmlDocmap.SelectNodes("//*[name()='area'][@key]"); foreach (XmlNode areakey in xmlmapkeylist) { string keyText = areakey.Attributes["key"].Value; //add the key values from the mapfile into a list listmapkeys.Add(keyText); } } //get all the key values from the "Thislist" xml file. XmlNodeList keys = section.SelectNodes("a:partslistsectionrows/a:partslistsectionrow/a:key", nsmgr); foreach (XmlNode ListKey in keys) { //add the key values from the each section in "Thislist"xml into a list listsectionkeys.Add(ListKey.InnerText); } } foreach (string mapkey in listmapkeys) { //if the section doesn't contain a key value present in the mapfile. if(!listsectionkeys.Contains(mapkey)) {//output the differences or discrepencies this is going to listBox1 in this case listBox1.Items.Add(hotspot + " not in partslist"); } } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM