xsl:变量高级选择

时间:2012-09-03 11:52:48

标签: xslt

我有几个这样的XML元素:

  <testcase starttime="2012-09-03 10:41:29" timestamp=" 622.922000">
    <testlogfile file="" />
    <teststep timestamp=" 622.944000" level="0" type="user" ident="1" result="pass">Do something</teststep>
    <teststep timestamp=" 622.965000" level="0" type="user" ident="2" result="pass">Do something</teststep>
    <teststep timestamp=" 622.986000" level="0" type="user" ident="3" result="pass">Do something</teststep>
    <verdict time="2012-09-03 10:41:30" timestamp=" 623.428000" endtime="2012-09-03 10:41:30" endtimestamp=" 623.428000" result="pass" />
    <title>Title goes here</title>
    <ident>TC100_06</ident>
    <description>description goes here</description>
    <extendedinfo type="test case status">Approved</extendedinfo>
    <extendedinfo type="traceability">some requirement</extendedinfo>
    <extendedinfo type="vehicle mode">Hibernate, Parked, Living, Accessory</extendedinfo>
    <extendedinfo type="environment">Station with ATB</extendedinfo>
    <extendedinfo type="variants">veh variants</extendedinfo>
  </testcase>

我想创建一个xsl:variable select查询,通过“ident”和“test case status”来计算测试用例。我能够实现2个单独的查询,但我不知道如何加入它们:

<xsl:variable name="totalTc" select="count(//testcase[./ident!='text' and ./ident!='obsolete' and ./ident!='rtm' and ./ident!='status overview' and ./ident!='statistics'])"/>
<xsl:variable name="approvedTc" select="count(//extendedinfo[@type='test case status' and text()='Approved'])"/>

我希望加入的查询看起来像这样,但我无法查询属性:

<xsl:variable name="totalTc" select="count(//testcase[./ident!='text' and ./ident!='obsolete' and ./ident!='rtm' and ./ident!='status overview' and ./ident!='statistics' and ./extendedinfo=='Approved'])"/>

2 个答案:

答案 0 :(得分:2)

怎么样......

<xsl:variable name="totalTc" select="count(
  //testcase[
     ident!='text' and
     ident!='obsolete' and
     ident!='rtm' and
     ident!='status overview' and
     ident!='statistics' and
     extendedinfo[
         @type='test case status' and
         .='Approved']
   ])"/>

选项

请注意,在XSLT 2.0中,您可以使用:

<xsl:variable name="totalTc" select="count(
  //testcase[
     not (ident in ('text','obsolete','rtm','status overview','statistics')) and
     extendedinfo[@type='test case status' and .='Approved']
   ])"/>

此外,而不是形式...

的表达
testcase[ A and B]

......人们可以改写......

testcase[A][B]

前一种情况可能会稍微有点效率,但我认为个人风格也应该考虑到你选择的表达方式。例如,人们也可以放......

<xsl:variable name="totalTc" select="count(//testcase
   [not (ident in ('text','obsolete','rtm','status overview','statistics'))]
   [extendedinfo[@type='test case status'][.='Approved']])"/>

答案 1 :(得分:1)

使用

 count(
   //testcase
      [extendedinfo[@type='test case status']='Approved'
     and
       not(teststep
            [contains('|text|obsolete|rtm|status overview|statistics',
                       concat('|',@ident,'|')
                       )
           ]
         )
      ]
       )