2012-06-26 29 views
1

目前,我有以下XML:更少的行,取決於病症

<Rowsets> 
    <Rowset> 
     <Row> 
      <DrilldownDepth>0</DrilldownDepth> 
      <ScheduleWeek>---</ScheduleWeek> 
      <ABC>---</ABC> 
      <PQR>1500</PQR> 
      <XYZ>15000</XYZ> 
      <Quant>285</Quant> 
     </Row> 
     <Row> 
      <DrilldownDepth>1</DrilldownDepth> 
      <ScheduleWeek>12</ScheduleWeek> 
      <ABC>---</ABC> 
      <PQR>1500</PQR> 
      <XYZ>15000</XYZ> 
      <Quant>285</Quant> 
     </Row> 
     <Row> 
      <DrilldownDepth>2</DrilldownDepth> 
      <ScheduleWeek>12</ScheduleWeek> 
      <ABC>SUPER</ABC> 
      <PQR>100</PQR> 
      <XYZ>200</XYZ> 
      <Quant>300</Quant> 
     </Row> 
     <Row> 
      <DrilldownDepth>2</DrilldownDepth> 
      <ScheduleWeek>12</ScheduleWeek> 
      <ABC>Duper</ABC> 
      <PQR>100</PQR> 
      <XYZ>200</XYZ> 
      <Quant>300</Quant> 
     </Row> 
     <Row> 
      <DrilldownDepth>2</DrilldownDepth> 
      <ScheduleWeek>12</ScheduleWeek> 
      <ABC>Fun</ABC> 
      <PQR>100</PQR> 
      <XYZ>200</XYZ> 
      <Quant>300</Quant> 
     </Row> 
     <Row> 
      <DrilldownDepth>2</DrilldownDepth> 
      <ScheduleWeek>7</ScheduleWeek> 
      <ABC>SUPER</ABC> 
      <PQR>100</PQR> 
      <XYZ>200</XYZ> 
      <Quant>300</Quant> 
     </Row> 
     <Row> 
      <DrilldownDepth>2</DrilldownDepth> 
      <ScheduleWeek>7</ScheduleWeek> 
      <ABC>Duper</ABC> 
      <PQR>100</PQR> 
      <XYZ>200</XYZ> 
      <Quant>300</Quant> 
     </Row> 
     <Row> 
      <DrilldownDepth>2</DrilldownDepth> 
      <ScheduleWeek>8</ScheduleWeek> 
      <ABC>SUPER</ABC> 
      <PQR>100</PQR> 
      <XYZ>200</XYZ> 
      <Quant>300</Quant> 
     </Row> 
    </Rowset> 
</Rowsets> 

現在我只需要那些<Row>其中<DrilldownDepth>2</DrilldownDepth><ScheduleWeek>12</ScheduleWeek>。我可以使用中繼器/循環來實現它,但我需要使用Xpath或XSLT來實現它。所以在這個問題上的任何幫助將不勝感激。

SOTS

回答

0

以下XPath語句使用predicate過濾器來選擇包含符合指定條件的子元素行元素:

/Rowsets/Rowset/Row[DrilldownDepth=2 and ScheduleWeek=12] 

使用在XSLT應用的值的變量:

<xsl:variable name="depth">2</xsl:variable> 
<xsl:variable name="week">12</xsl:variable> 
<xsl:apply-templates select="/Rowsets/Rowset/Row[DrilldownDepth=$depth and ScheduleWeek=$week]"/> 
+0

謝謝漢森。但是,這可以通過XSLT或XQUERY來實現,其中「2」和「12」通過變量傳遞? –

+0

是的。替換變量的值 –

+0

當前我正在嘗試XSLT:<?xml version =「1.0」encoding =「UTF-8」?> 2 XSL:模板匹配= 「/行集」> \t \t <的xsl:for-每個選擇= 「行集[行[DrilldownDepth = $深度和ScheduleWeek = $周]]」> \t \t \t的 \t的 \t 不工作 –

0

'和'運算符的替代方法是使用第二個謂詞。哪一種更好取決於個人風格,但如果你仔細研究了很多關於SO的答案,你會發現多謂詞方法是流行的選擇。

它看起來像這樣...

/Rowsets/Rowset/Row[DrilldownDepth='2'][ScheduleWeek='12'] 

注:這同樣適用帶或不帶周圍的面值數字引號。沒有可能會稍微更具可讀性。可能會稍微高效。這也是一種風格選擇。

+0

謝謝肖恩。但是,這可以通過XSLT或XQUERY來實現,其中「2」和「12」通過變量傳遞? –

+0

是的。 ............... –