如何在xml中选择多个子节点

时间:2013-08-20 11:59:45

标签: xml xpath xquery

我看起来像这样的xml

<Details>
  <UserResource>
    <ResourceGroup>AJD-IPSP1</ResourceGroup>
    <ResourceType>master</ResourceType>
    <Access>r</Access>
    <UseSOAPFault>soap </UseSOAPFault>
  </UserResource>
  <UserResource>
    <ResourceGroup>AJD-VME1</ResourceGroup>
    <ResourceType>trx</ResourceType>
    <Access>r</Access>
    <UseSOAPFault>soap </UseSOAPFault>
  </UserResource>
  <UserResource>
    <ResourceGroup>AJD-VME10</ResourceGroup>
    <ResourceType>trx</ResourceType>
    <Access>r</Access>
    <UseSOAPFault>soap </UseSOAPFault>
  </UserResource>
  <UserResource>
    <ResourceGroup>AJD-VME11</ResourceGroup>
    <ResourceType>trx</ResourceType>
    <Access>r</Access>
    <UseSOAPFault>soap </UseSOAPFault>
  </UserResource>
</Details>

我想要实现的目标是:

/Details/UserResource[ResourceGroup="AJD-VME10" or ResourceGroup="AJD-VME1"]

结果应该是

  <UserResource>
    <ResourceGroup>AJD-VME1</ResourceGroup>
    <ResourceType>trx</ResourceType>
    <Access>r</Access>
    <UseSOAPFault>soap </UseSOAPFault>
  </UserResource>
  <UserResource>
    <ResourceGroup>AJD-VME10</ResourceGroup>
    <ResourceType>trx</ResourceType>
    <Access>r</Access>
    <UseSOAPFault>soap </UseSOAPFault>
  </UserResource>

我该怎么做这个xpath?

我想使用一个xpath来做这个。

3 个答案:

答案 0 :(得分:0)

这是一种简单的方法。如果您对for循环感到满意,可以遍历每个用户资源并检查Resource Group / text()是否等于该术语。

    let $x :=
<Details>
  <UserResource>
    <ResourceGroup>AJD-IPSP1</ResourceGroup>
    <ResourceType>master</ResourceType>
    <Access>r</Access>
    <UseSOAPFault>soap </UseSOAPFault>
  </UserResource>
  <UserResource>
    <ResourceGroup>AJD-VME1</ResourceGroup>
    <ResourceType>trx</ResourceType>
    <Access>r</Access>
    <UseSOAPFault>soap </UseSOAPFault>
  </UserResource>
  <UserResource>
    <ResourceGroup>AJD-VME10</ResourceGroup>
    <ResourceType>trx</ResourceType>
    <Access>r</Access>
    <UseSOAPFault>soap </UseSOAPFault>
  </UserResource>
  <UserResource>
    <ResourceGroup>AJD-VME11</ResourceGroup>
    <ResourceType>trx</ResourceType>
    <Access>r</Access>
    <UseSOAPFault>soap </UseSOAPFault>
  </UserResource>
</Details>


return $x/UserResource[ResourceGroup="AJD-VME10" or ResourceGroup="AJD-VME1"]

答案 1 :(得分:0)

这是一个xsl解决方案:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Details">
    <xsl:apply-templates select="UserResource" />
</xsl:template>

<xsl:template match="UserResource">
    <xsl:if test="ResourceGroup='AJD-VME10' or ResourceGroup='AJD-VME1'">
        <xsl:element name="{name()}">
            <xsl:copy-of select="*" />
        </xsl:element>
    </xsl:if>

</xsl:template>

答案 2 :(得分:0)

您说您的DB2查询是

SELECT SECTIONTITLE, VMETE.COALESCE_XML( 
       XMLQUERY(
         '$c/Details/UserResource
          [ResourceGroup="AJD-VME10" 
          or ResourceGroup="AJD-VME1"]' 
          passing i.SECTIONCONTENT as "c" ), 
       i.SECTIONCONTENT) 
       as SECTIONCONTENT 
  FROM IPSP_CONFIGURATION i 
 WHERE IPSPID='AJD-IPSP1' 
   AND EFFECTIVETO='9999-12-31-23.59.59' 

正如其他人已经观察到的那样,这里的XPath在语法和语义上都是正确的;我倾向于怀疑SQL / XQuery接口可能出现问题:在XQuery评估环境中,$c将被绑定到Details元素的元素节点或文本节点那个元素作为它的孩子?这种事情容易出错,但很容易解决。

但是你说那个表单“不起作用。但如果我将[ResourceGroup =”AJD-VME10“或ResourceGroup =”AJD-VME1“]更改为[ResourceGroup =”AJD-VME10“它有效。

这很奇怪。如果@ Natkeeran建议将两个析取物括起来,那么您使用的系统在其解析器中有一个错误(对于DB2来说似乎不太可能)。您是否绝对相信,按预期工作的查询与不返回任何内容的查询之间的唯一区别是从谓词中删除or ResourceGroup="AJD-VME1"