简体   繁体   English

Powershell-XPath-SUM()和Count()子节点-仅XPath表达式

[英]Powershell - XPath - SUM() and Count() Child Nodes - XPath Expression Only

New to Xpath, intermediate with powershell. Xpath的新手,在powershell中处于中间。 Spent a good amount of time researching yesterday, I could not find a single example of xpath sum() or count() expressions within a powershell -XPath command. 昨天花了很多时间研究,但在powershell -XPath命令中找不到xpath sum()或count()表达式的单个示例。 I'm writing this verbosely as my first stackoverflow question so that future people can find this article for a clear example answer from an XPert. 我将这作为我的第一个stackoverflow问题来进行详细介绍,以便将来的人们可以从XPert那里找到本文的清晰示例答案。

Objectives (2): 1. Count() the critical processes. 目标(2):1. Count()关键过程。 2. SUM() the transactions processed by critical processes. 2. SUM()由关键流程处理的交易。
Note: The output of the powershell XPath command should be the raw number. 注意:powershell XPath命令的输出应为原始数字。

<units>
<unit>
  <unitName>Generic Process Unit</unitName>
    <attribute>
      <name>Process Type</name>
      <value>Critical Process</value>
    </attribute>
    <attribute>
      <name>Transactions Processed</name>
      <value>10</value>
    </attribute>
</unit>

<unit>
  <unitName>Generic Process Unit</unitName>
    <attribute>
      <name>Process Type</name>
      <value>Trivial Process</value>
    </attribute>
    <attribute>
      <name>Transactions Processed</name>
      <value>7</value>
    </attribute>
</unit>

<unit>
  <unitName>Generic Process Unit</unitName>
    <attribute>
      <name>Process Type</name>
      <value>Critical Process</value>
    </attribute>
    <attribute>
      <name>Transactions Processed</name>
      <value>5</value>
    </attribute>
</unit>
</units>

My Best attempt at the SUM function. 我对SUM功能的最佳尝试。 Didn't even try the Count: 甚至没有尝试Count:

select-xml -path mine.xml -XPath '//attribute[value="Critical Process"]/../attribute[name="Transactions Processed"][sum(value)]' | Select-Object -ExpandProperty node | select Value

Edit: Working XPath from notepad++ XMLTool plugin is below, but still need the Powershell equivalent (this doesn't work in Powershell): 编辑:下面是从notepad ++ XMLTool插件工作的XPath,但仍需要Powershell等效项(这在Powershell中不起作用):

sum(//attribute[value="Critical Process"]/../attribute[name="Transactions Processed"]/value)

Errata: The reason it needs to be XPath only expressions, is that I'm only using powershell to construct and validate my XPath strings. 勘误表:之所以需要仅XPath表达式,是因为我仅使用powershell构造和验证我的XPath字符串。 The end goal is to put those strings into a non-powershell software that will actually use the XPath string. 最终目标是将这些字符串放入实际上将使用XPath字符串的非Powershell软件中。 That software gives me no way to validate an XPath, and it takes 20 minutes to tell if it works after I put it in, so I'm using powershell as my testbed. 该软件使我无法验证XPath,将它放入后需要20分钟的时间才能知道它是否可以工作,因此我将Powershell用作测试平台。

I've changed the item names, descriptions, and removed attributes to make the example simpler. 我更改了项目名称,说明并删除了属性,以使示例更简单。 However, I don't know if the "schema" words are important, so here is an example of an original "value" line. 但是,我不知道“ schema”一词是否重要,因此这里是原始“ value”行的一个示例。

<value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">Critical Process</value>

Issues: 问题:

  • It seems that a given XPath expression needs to be tweaked just a bit to work in powershell. 似乎需要对给定的XPath表达式进行一些调整才能在Powershell中工作。 For example, after -XPath '/xpath/node' you seem to always need to end with: '/text()' | 例如,在-XPath'/ xpath / node'之后,您似乎总是需要以'/ text()'结尾。 Select-Object -ExpandProperty node | 选择对象-ExpandProperty节点| select Value to actually see the output you want. 选择“值”以实际查看所需的输出。

  • Microsoft has XPath documentation, and Powershell documentation, but no "XPath Expressions in Powershell" documentation. Microsoft具有XPath文档和Powershell文档,但是没有“ PowerShell中的XPath表达式”文档。

  • Most internet examples of expressions use "Powershell + XPath" do bare minimum XPath to get data object out, and then loop through using foreach in powershell. 大多数Internet表达式的示例都使用“ Powershell + XPath”来做最少的XPath来获取数据对象,然后在powershell中使用foreach进行循环。

  • It looks like the command syntax might have some dependency on XPath versions 1, 2, etc. 看起来命令语法可能与XPath版本1、2等有关。

Thanks in advance. 提前致谢。

As far as I can tell those functions won't work with Select-Xml as it appears to use [XmlDocument]::SelectNodes behind the scenes which can only return nodes. 据我所知,这些功能不适用于Select-Xml因为它似乎在幕后只能使用返回节点的[XmlDocument] :: SelectNodes。

As an alternative you can use a navigator: 或者,您可以使用导航器:

$xml = @'
#your xml here
'@ -as [xml]

$xml.CreateNavigator().Evaluate('count(//unit)')
$xml.CreateNavigator().Evaluate('sum(//unit)')

The counting should be as simple as 计数应尽可能简单

count(/units/unit[attribute[name="Process Type"][value="Critical Process"]])

(using two nested levels of predicates to ensure that the name and value are both on the same attribute ). (使用两个嵌套的谓词级别来确保namevalue都在同一 attribute )。 For the sum, we just add a bit to the end of the expression 对于总和,我们只需在表达式的末尾添加一点

sum(/units/unit[attribute[name="Process Type"][value="Critical Process"]]/attribute[name="Transactions Processed"]/value)

But note that both of these expressions will return a numeric value, not a node set, which may be what is confusing powershell. 但是请注意,这两个表达式都将返回数字值,而不是节点集,这可能会使powershell感到困惑。

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

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