在多个字段上使用过滤器编写for-each选项时出现问题

时间:2012-01-05 20:35:09

标签: xslt xpath

我有一个xml文档,其节点如下所示:

<SQLIndexUsage>
  <Property Name="database_name">ClearTrace</Property>
  <Property Name="Object_Name">CTTraceSummary</Property>
  <Property Name="Schema_name">dbo</Property>
  <Property Name="Index_name">IX_CTTraceSummary_Date</Property>
  <Property Name="Type_Desc">NONCLUSTERED</Property>
  <Property Name="user_seeks" />
  <Property Name="user_scans" />
  <Property Name="user_lookups" />
  <Property Name="user_updates" />
  <Property Name="usage_ratio">0.00</Property>
  <Property Name="RowError" />
  <Property Name="RowState">Detached</Property>
  <Property Name="Table" />
  <Property Name="ItemArray">
  <Property>ClearTrace</Property>
  <Property>CTTraceSummary</Property>
  <Property>dbo</Property>
  <Property>IX_CTTraceSummary_Date</Property>
  <Property>NONCLUSTERED</Property>
  <Property />
  <Property />
  <Property />
  <Property />
  <Property>0.00</Property>
  </Property>
  <Property Name="HasErrors">False</Property>
</SQLIndexUsage>

如何编写将在多个字段上过滤的for-each选项,我正在研究这样的事情:

<xsl:for-each select="/objects/SQLIndexUsage/Property[@Name ='user_seeks'][1]!='' or /objects/SQLIndexUsage/Property[@Name ='user_scans']!='' or /objects/SQLIndexUsage/Property[@Name ='user_updates']!=''">

还是我看错了?

1 个答案:

答案 0 :(得分:1)

通常,您应该将条件保存在单个谓词中:

/objects/SQLIndexUsage/Property[
   (@Name ='user_seeks' or @Name='user_scans' or @Name='user_updates') and .!='']

这更短:

/objects/SQLIndexUsage/Property[
   @Name[.='user_seeks' or .='user_scans' or .='user_updates'] and .!='']

你的位置状况有点复杂,但你仍然可以这样做(使用union):

/objects/SQLIndexUsage/Property[@Name='user_seeks' and .!=''][1] |
/objects/SQLIndexUsage/Property[
   (@Name='user_scans' or @Name='user_updates') and .!='']

注意:我已经在表达式上留下了/objects,即使您的示例XML没有显示它。我猜你实际上正在使用更大的文档。