C#只查询xslt的父节点

Posted

技术标签:

【中文标题】C#只查询xslt的父节点【英文标题】:C# query only parent nodes of xslt 【发布时间】:2019-07-16 06:26:12 【问题描述】:

我有以下 xslt :-

<IncidentTicket ExternalSystemName="ExternalSystemName" Company="$Company">
<xsl:if test="Incident-LastResolvedDate and Incident-LastResolvedDate != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="LastResolvedDate">
      <xsl:value-of select="Incident-LastResolvedDate" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-ServiceType and Incident-ServiceType != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="ServiceType">
      <xsl:value-of select="'Incident'" />
    </xsl:attribute>
  </xsl:if>
<ExternalRelease>
<xsl:if test="incTest4 and incTest4 != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:attribute name="TestDate">
<xsl:value-of select="incTest4" />
</xsl:attribute>
</xsl:if>
</ExternalRelease>
</IncidentTicket>

我们可以看到节点内有节点&lt;ExternalRelease&gt;

当我查询属性名称“TestDate”是否存在时,:-

var isAttributeAlreadyExists = result.Descendants().Attributes("name").Where(x => x.Value == "TestDate").Any();

这总是返回 True,因为它将它放入 ExternalRelease 节点。

当我查询时,我不想使用像 ExternalRelease 这样的子节点,我只想在 IncidentTicket 中检查。

我怎样才能做到这一点?

结果内容:-

<IncidentTicket ExternalSystemName="ExternalSystemName" Company="$Company">
  <xsl:if test="Incident-LastResolvedDate and Incident-LastResolvedDate != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="LastResolvedDate">
      <xsl:value-of select="Incident-LastResolvedDate" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-ServiceType and Incident-ServiceType != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="ServiceType">
      <xsl:value-of select="'Incident'" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-IncidentNumber and Incident-IncidentNumber != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="IncidentNumber">
      <xsl:value-of select="Incident-IncidentNumber" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-ReportedDateTime and Incident-ReportedDateTime != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="ReportedDateTime">
      <xsl:value-of select="Incident-ReportedDateTime" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-LastModifiedTime and Incident-LastModifiedTime != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="LastModifiedTime">
      <xsl:value-of select="Incident-LastModifiedTime" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-Priority and Incident-Priority != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="Priority">
      <xsl:value-of select="Incident-Priority" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-Status and Incident-Status != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="Status">
      <xsl:value-of select="Incident-Status" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-TicketDescription and Incident-TicketDescription != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="TicketDescription">
      <xsl:value-of select="Incident-TicketDescription" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-AssigneeLoginId and Incident-AssigneeLoginId != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="AssigneeLoginId">
      <xsl:value-of select="Incident-AssigneeLoginId" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-DueDate and Incident-DueDate != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="DueDate">
      <xsl:value-of select="Incident-DueDate" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-SupportGroupName and Incident-SupportGroupName != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="SupportGroupName">
      <xsl:value-of select="Incident-SupportGroupName" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-ResolutionDescription and Incident-ResolutionDescription != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="ResolutionDescription">
      <xsl:value-of select="Incident-ResolutionDescription" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-Category and Incident-Category != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="Category">
      <xsl:value-of select="Incident-Category" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-ClosedDate and Incident-ClosedDate != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="ClosedDate">
      <xsl:value-of select="Incident-ClosedDate" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-ReportedSource and Incident-ReportedSource != 'null'" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="ReportedSource">
      <xsl:value-of select="Incident-ReportedSource" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-CreatedBy and Incident-CreatedBy != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="CreatedBy">
      <xsl:value-of select="Incident-CreatedBy" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-LongDescription and Incident-LongDescription != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="LongDescription">
      <xsl:value-of select="Incident-LongDescription" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-CallerId and Incident-CallerId != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="CallerId">
      <xsl:value-of select="Incident-CallerId" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-Impact and Incident-Impact != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="Impact">
      <xsl:value-of select="Incident-Impact" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-Urgency and Incident-Urgency != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="Urgency">
      <xsl:value-of select="Incident-Urgency" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-CorrelationId and Incident-CorrelationId != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="CorrelationId">
      <xsl:value-of select="Incident-CorrelationId" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-RelatedIncidents and Incident-RelatedIncidents != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="RelatedIncidents">
      <xsl:value-of select="Incident-RelatedIncidents" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-RelatedProblems and Incident-RelatedProblems != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="RelatedProblems">
      <xsl:value-of select="Incident-RelatedProblems" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-RelatedTasks and Incident-RelatedTasks!= ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="RelatedTasks">
      <xsl:value-of select="Incident-RelatedTasks" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-ResponseDueDate and Incident-ResponseDueDate != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="ResponseDueDate">
      <xsl:value-of select="Incident-ResponseDueDate" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-ResolutionDueDate and Incident-ResolutionDueDate != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="ResolutionDueDate">
      <xsl:value-of select="Incident-ResolutionDueDate" />
    </xsl:attribute>
  </xsl:if>
  <xsl:if test="Incident-ConfigurationItem and Incident-ConfigurationItem!= ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:attribute name="ConfigurationItem">
      <xsl:value-of select="Incident-ConfigurationItem" />
    </xsl:attribute>
  </xsl:if>
                  Incident-TestCondition
                  Attr-Incident

                <ExternalRelease><xsl:if test="incTest4 and incTest4 != ''" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:attribute name="TestDate"><xsl:value-of select="incTest4" /></xsl:attribute></xsl:if></ExternalRelease></IncidentTicket>

【问题讨论】:

【参考方案1】:

使用Elements() 代替Descendants()

【讨论】:

result.Elements().Attributes("name").Where(x => x.Value == "TestDate").Any() 每次这给我错误..即使元素存在,它返回我 false 发布结果内容 posted..请看一下 ExternalRelease 节点不是静态的,它可以包含任何名称 属性名称="TestDate"

以上是关于C#只查询xslt的父节点的主要内容,如果未能解决你的问题,请参考以下文章

如何将前缀类型添加到重复父节点并使用 XSLT 选择每个元素的所有元素?

XPath 1.0:使用当前节点的父节点的属性值来寻找另一个匹配的节点

sql根据子节点查出所有的父节点的

c#关于树节点怎么获取父节点下的所有子节点的ID

ORACLE数据库递归查询当前节点的父节点和子节点(tree)

sql server使用cte递归查询获取树形的父节点/子节点