【问题标题】:Read data from XML having nested inconsistent structure从具有嵌套不一致结构的 XML 中读取数据
【发布时间】:2021-10-27 22:25:35
【问题描述】:

源 XML 数据 -

<EntityType Name="WorkItem">
   <Property Name="WorkItemId" Type="Edm.Int32" Nullable="false">
      <Annotation Term="Ref.ReferenceName" String="System.Id" />
      <Annotation Term="Display.DisplayName" String="Work Item Id" />
   </Property>
   <Property Name="InProgressDate" Type="Edm.DateTimeOffset">
      <Annotation Term="Display.DisplayName" String="InProgress Date" />
   </Property>
   <Property Name="InProgressDateSK" Type="Edm.Int32" />
   <Property Name="CompletedDateSK" Type="Edm.Int32" />
</EntityType>

所需的输出如下所示或至少以列格式。如果输出的格式不完全低于所示的标题格式,则可以,但在调整代码以获取列格式的结果时寻求帮助

我尝试了什么 -

Select-Xml -Path D:\Temp\meta.xml -XPath '/EntityType/Property' | ForEach-Object { $_.Node.Name, $_.Node.Type, $_.node.Annotation } 

还有

Select-Xml -Path D:\Temp\meta.xml -XPath '/EntityType/Property' | select -ExpandProperty Node

【问题讨论】:

  • 酷。但是你在问我们什么?
  • 寻求帮助以获得我在所需输出中显示的结果
  • 哦,好的。您能否让我们知道您当前的方法有哪些有效和无效,以帮助我们确定要做什么?你投入的工作越多,你就越有可能得到好的答案。高质量的问题通常会在 15 分钟内得到答案。或许阅读How to Ask 来提供帮助?

标签: xml powershell


【解决方案1】:

首先,我会找到设计该 XML 模式的人,并向他们提供 XML 操作指南。这是一个可怕的架构设计。

其次,我可能会像下面这样处理 XML。将文件转换为 XML 文档,然后遍历每个节点。由于 Powershell 输出对象属性的方式,我们必须跟踪注释列。

# Get the XML file as a raw string, and then convert it to an XML Document with the [xml] type accellerator
[xml]$xml = Get-Content D:\Temp\meta.xml -Raw

# This is for tracking the columns we use. These two columns are always present
$Columns = 'Name', 'Type'

$Results = foreach ($node in $xml.EntityType.Property) {
    # Create a hashtable with a key and value for each column
    $Record = @{
        Name = $node.Name
        Type = $node.Type
    }

    # Add a key and value for each column from the annotation
    foreach ($Annotation in $node.Annotation) {
        $Record[$Annotation.Term] = $Annotation.String

        # If we haven't seen this column before, note it so we can output it later
        if ($Annotation.Term -notin $Columns) {
            $Columns += $Annotation.Term
        }
    }

    # Convert the hashtable record to a custom object and save them all to results
    [PSCustomObject]$Record
}

# Output the results using the dynamic list of columns we found
$Results | Select-Object -Property $Columns

这让我得到了输出:

Name             Type               Ref.ReferenceName Display.DisplayName
----             ----               ----------------- -------------------
WorkItemId       Edm.Int32          System.Id         Work Item Id
InProgressDate   Edm.DateTimeOffset                   InProgress Date
InProgressDateSK Edm.Int32
CompletedDateSK  Edm.Int32

【讨论】:

  • 你能给我推荐一个xml如何预订吗?
  • @AbrahamZinala 我的书早就不见了,恐怕。 O'Reilly 一如既往地是一个很好的起点。我在这里评论的原因是设计者将数据放入属性中,并且元素名称非常通用,导致难以理解 XML。 IMO,您应该避免使用 XML 中的属性来支持更灵活的子元素,并为元数据保留属性。 XML 数据文件不应看起来像 XSD 文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
  • 2016-02-07
  • 2023-04-05
相关资源
最近更新 更多