sqlpadawan
sqlpadawan

Reputation: 305

Problems writing a for-each select with filters on multiple fields

I have an xml document with nodes that look like this:

<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>

How do I write a for-each select that will filter on multiple fields, I was working on something like this:

<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']!=''">

Or am I looking at this wrong?

Upvotes: 1

Views: 482

Answers (1)

Wayne
Wayne

Reputation: 60414

In general, you should keep the conditions in a single predicate:

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

This is even shorter:

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

This is complicated a bit by your position condition, but you can still do this (with union):

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

Note: I've left /objects on the expression, even though your sample XML doesn't show it. I'm guessing you're actually working with a larger document.

Upvotes: 1

Related Questions