English 中文(简体)
删除XPath NodeIterator的分母
原标题:Delete / Remove node from XPathNodeIterator, given an XPath
  • 时间:2010-03-26 16:38:19
  •  标签:
  • c#
  • xml
  • xslt

首先,如果一个人有不同的、也许较短的(或更好的)解决问题的办法,那也是受欢迎的。


我试图“简单”去除(几乎)小口径终端中重复的内容。 有一些(元数据)的节点在比较时不想包括,而一则无法说明在异常低价竞标中如何将它延伸到职能消除这些节点。 与此类似:

<xsl:for-each select="abx:removeNodes(d/df600|d/df610|d/df611|d/df630|d/df650|d/df651|d/df655,  *[@key=&quot;i1&quot; or @key=&quot;i2&quot; or key=&quot;db&quot;] )">
   <xsl:if test="not(node()=preceding-sibling::*)">
      blah
   </xsl:if>
</xsl:for-each>

延长期限的工作进展良好...... (C#)

public XPathNodeIterator removeNodes(XPathNodeIterator p_NodeIterator, String removeXPath)
{
   Logger Logger = new Logger("xslt");
   Logger.Log("removeNodes(removeXPath={0}):", removeXPath);

   foreach (XPathNavigator CurrentNode in p_NodeIterator)
   {
      Logger.Log("removeNodes(): CurrentNode.OuterXml={0}.", CurrentNode.OuterXml);

      foreach (XPathNavigator CurrentSubNode in CurrentNode.Select(removeXPath))
      {
         Logger.Log("removeNodes(): CurrentSubNode.OuterXml={0}.", CurrentSubNode.OuterXml);
         // How do i delete this node!?
         //CurrentSubNode.DeleteSelf();
      }
   }

   return p_NodeIterator;
}

我采用目前的第2号办法。 删除(a) 做工是因为在XPathNavigator被混为一谈并失去其立场,因此只能删除它使用“搬运XPath”发现的第一个项目。 象删除AndMoveNext(AndreMoveNext)这样的一些做法是 n的,但似乎没有这样的方法。


实例数据:

<df650>
  <df650 key="i1"> </df650>
  <df650 key="i2">0</df650>
  <df650 key="a">foo</df650>
  <df650 key="x">bar</df650>
  <df650 key="db">someDB</df650>
  <df650 key="id">b2</df650>
  <df650 key="dsname">someDS</df650>
</df650>

接着是另一个相同的节点(如果你无视美术区;db,id,dsname)。

<df650>
  <df650 key="i1"> </df650>
  <df650 key="i2">0</df650>
  <df650 key="a">foo</df650>
  <df650 key="x">bar</df650>
  <df650 key="db">someOtherDB</df650>
  <df650 key="id">b2</df650>
  <df650 key="dsname">someOtherDS</df650>
</df650>

结果是......

<df650>
  <df650 key="i1"> </df650>
  <df650 key="i2">0</df650>
  <df650 key="a">foo</df650>
  <df650 key="x">bar</df650>
</df650>
最佳回答

这个问题可以像这样解决(不管怎样,它并不能解决千年实际问题......)。

  • Create a List of type XPathNavigator that will contain nodes you want to delete.
  • Add the nodes to this list instead of using DeleteSelf().
  • When done finding all nodes you want to delete, iterate through your List and delete the nodes. Since these nodes are Navigators, there is no issue with lost position.

我在十分钟后就试图重开该法典。

问题回答

仅凭在《国际公路货运公约》中,你就能够轻易地做到这一点,实际上不需要一项延期职能。 考虑:

<!-- make a template that matches all nodes that cold be removed -->
<xsl:template match="d/df600|d/df610|d/df611|d/df630|d/df650|d/df651|d/df655">
  <!-- check the your condition for node removal, whatever it may be -->
  <xsl:if test="not(@key= i1  or @key= i2  or @key= db )">
    <!-- ...if it is *not* met, copy the node -->
    <xsl:copy-of select="." />
  </xsl:if>
  <!-- ...in all other cases, nothing happens, i.e. the node is removed -->
</xsl:template>

由于有人对RymdPung表示歉意,我得以利用你的名单建议,在重复部分删除空白。

我又提到该系统。 收集。 我的法典中通用名称空间。

这里是我通过一套节点进行扫描、确定我想要删除的节点,然后在另外的休息室中删除。

         private void deleteEmptyRows(string path)
    {
        XPathNodeIterator nodesToCheck = MainDataSource.CreateNavigator().Select(path, NamespaceManager);
        List<XPathNavigator> nodesToDelete = new List<XPathNavigator>();
        foreach (XPathNavigator currentItem in nodesToCheck)
            if (currentItem.Value.Trim().Length == 0)
                nodesToDelete.Add(currentItem);

        foreach(XPathNavigator deleteMe in nodesToDelete)
            deleteMe.DeleteSelf();
    }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签