Jump to content

Passing Dynamic value to the xpath


sireesha g
 Share

Recommended Posts

Hi All, i have xml

<?xml version="1.0" encoding="UTF-8"?><Rowsets DateCreated="2011-02-03T06:14:20" EndDate="2011-02-03T06:14:20" StartDate="2011-02-03T05:14:20" Version="12.1.5 Build(83)">	<Rowset>		<Columns>			<Column Description="Unit" MaxRange="1" MinRange="0" Name="Unit" SQLDataType="12" SourceColumn="Unit"/>			<Column Description="Power_Consumption" MaxRange="1" MinRange="0" Name="Power_Consumption" SQLDataType="2" SourceColumn="Power_Consumption"/>			<Column Description="Cooling_Water_Consumption" MaxRange="1" MinRange="0" Name="Cooling_Water_Consumption" SQLDataType="2" SourceColumn="Cooling_Water_Consumption"/>			<Column Description="Raw_Water_Consumption" MaxRange="1" MinRange="0" Name="Raw_Water_Consumption" SQLDataType="2" SourceColumn="Raw_Water_Consumption"/>			<Column Description="Fuel_Oil_Consumption" MaxRange="1" MinRange="0" Name="Fuel_Oil_Consumption" SQLDataType="2" SourceColumn="Fuel_Oil_Consumption"/>			<Column Description="Fuel_Gas_Consumption" MaxRange="1" MinRange="0" Name="Fuel_Gas_Consumption" SQLDataType="2" SourceColumn="Fuel_Gas_Consumption"/>		</Columns>		<Row>			<Unit>CDU</Unit>			<Power_Consumption>15</Power_Consumption>			<Cooling_Water_Consumption>345</Cooling_Water_Consumption>			<Raw_Water_Consumption>890</Raw_Water_Consumption>			<Fuel_Oil_Consumption>245</Fuel_Oil_Consumption>			<Fuel_Gas_Consumption>124</Fuel_Gas_Consumption>		</Row>	<Row>			<Unit>VDU</Unit>			<Power_Consumption>14</Power_Consumption>			<Cooling_Water_Consumption>234</Cooling_Water_Consumption>			<Raw_Water_Consumption>678</Raw_Water_Consumption>			<Fuel_Oil_Consumption>321</Fuel_Oil_Consumption>			<Fuel_Gas_Consumption>123</Fuel_Gas_Consumption>		</Row>	<Row>			<Unit>FCCU</Unit>			<Power_Consumption>18</Power_Consumption>			<Cooling_Water_Consumption>456</Cooling_Water_Consumption>			<Raw_Water_Consumption>234</Raw_Water_Consumption>			<Fuel_Oil_Consumption>432</Fuel_Oil_Consumption>			<Fuel_Gas_Consumption>345</Fuel_Gas_Consumption>		</Row>	<Row>			<Unit>NHT</Unit>			<Power_Consumption>12</Power_Consumption>			<Cooling_Water_Consumption>678</Cooling_Water_Consumption>			<Raw_Water_Consumption>126</Raw_Water_Consumption>			<Fuel_Oil_Consumption>876</Fuel_Oil_Consumption>			<Fuel_Gas_Consumption>233</Fuel_Gas_Consumption>		</Row>	<Row>			<Unit>DHT</Unit>			<Power_Consumption>16</Power_Consumption>			<Cooling_Water_Consumption>234</Cooling_Water_Consumption>			<Raw_Water_Consumption>675</Raw_Water_Consumption>			<Fuel_Oil_Consumption>234</Fuel_Oil_Consumption>			<Fuel_Gas_Consumption>656</Fuel_Gas_Consumption>		</Row>	<Row>			<Unit>Cooling Systems</Unit>			<Power_Consumption>7</Power_Consumption>			<Cooling_Water_Consumption>123</Cooling_Water_Consumption>			<Raw_Water_Consumption>345</Raw_Water_Consumption>			<Fuel_Oil_Consumption>122</Fuel_Oil_Consumption>			<Fuel_Gas_Consumption>454</Fuel_Gas_Consumption>		</Row>	<Row>			<Unit>Utility</Unit>			<Power_Consumption>8</Power_Consumption>			<Cooling_Water_Consumption>678</Cooling_Water_Consumption>			<Raw_Water_Consumption>234</Raw_Water_Consumption>			<Fuel_Oil_Consumption>342</Fuel_Oil_Consumption>			<Fuel_Gas_Consumption>436</Fuel_Gas_Consumption>		</Row>	<Row>			<Unit>Lighting</Unit>			<Power_Consumption>5</Power_Consumption>			<Cooling_Water_Consumption>567</Cooling_Water_Consumption>			<Raw_Water_Consumption>456</Raw_Water_Consumption>			<Fuel_Oil_Consumption>321</Fuel_Oil_Consumption>			<Fuel_Gas_Consumption>554</Fuel_Gas_Consumption>		</Row>			</Rowset></Rowsets>

And an xslt to the xml

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">	<xsl:output method="xml" encoding="UTF-8"/>	<Results>		<xsl:apply-templates select="document('http://uscinc1b5s3:52000/XMII/CM/BPCL_Refinery_Demo/Quadrant/xmldata/TopTier/Finance/PowerandUtilitiesVariance.xml')" />	</Results>		<xsl:param name="SelectName"/>     <xsl:template name="description"  match="/">		[b]<xsl:for-each select="Rowsets/Rowset/Row[unit="CDU"]>	[/b]	 					<Rowsets DateCreated="2011-02-03T06:14:20" EndDate="2011-02-03T06:14:20" StartDate="2011-02-03T05:14:20" Version="12.1.5 Build(83)">	<Rowset>		<Columns>			<Column Description="Unit" MaxRange="1" MinRange="0" Name="Unit" SQLDataType="12" SourceColumn="Unit"/>			<Column Description="Power_Consumption" MaxRange="1" MinRange="0" Name="Power_Consumption" SQLDataType="2" SourceColumn="Power_Consumption"/>			<Column Description="Cooling_Water_Consumption" MaxRange="1" MinRange="0" Name="Cooling_Water_Consumption" SQLDataType="2" SourceColumn="Cooling_Water_Consumption"/>			<Column Description="Raw_Water_Consumption" MaxRange="1" MinRange="0" Name="Raw_Water_Consumption" SQLDataType="2" SourceColumn="Raw_Water_Consumption"/>			<Column Description="Fuel_Oil_Consumption" MaxRange="1" MinRange="0" Name="Fuel_Oil_Consumption" SQLDataType="2" SourceColumn="Fuel_Oil_Consumption"/>			<Column Description="Fuel_Gas_Consumption" MaxRange="1" MinRange="0" Name="Fuel_Gas_Consumption" SQLDataType="2" SourceColumn="Fuel_Gas_Consumption"/>		</Columns>		<Row>			<Unit><xsl:value-of select="Unit" /></Unit>			<Power_Consumption><xsl:value-of select="Power_Consumption" /></Power_Consumption>			<Cooling_Water_Consumption><xsl:value-of select="Cooling_Water_Consumption" /></Cooling_Water_Consumption>			<Raw_Water_Consumption><xsl:value-of select="Raw_Water_Consumption" /></Raw_Water_Consumption>			<Fuel_Oil_Consumption><xsl:value-of select="Fuel_Oil_Consumption" /></Fuel_Oil_Consumption>			<Fuel_Gas_Consumption><xsl:value-of select="Fuel_Gas_Consumption" /></Fuel_Gas_Consumption>		</Row>			</Rowset></Rowsets>               </xsl:for-each>    </xsl:template></xsl:stylesheet>

when i hard coded the value as CDU the desired xml is coming,but i need to pass this value dynamically to xslt, this means can we pass the xpath dynamically????

Link to comment
Share on other sites

That's what xsl:param is for :) .I see you already have a parameter called "SelectName". Just create another one for whatever "CDU" represents (some sort of measurement unit by the looks of it). Then, in the predicate, use it. For example, if you called it "unit", in the XPath, that would look like:

<xsl:for-each select="Rowsets/Rowset/Row[Unit=$unit]">

Link to comment
Share on other sites

If you want to set an XSLT parameter then you either need to run the transformation programmatically and in that case you need to check the API documentation of the XSLT processor you use or you need to run the transformation from the command line or with a tool like an XML editor that has some way to set the parameters. How you do that exactly depends on the XSLT processor you use or the XML editor you use. For instance Saxon 6.5.5 http://saxon.sourceforge.net/saxon6.5.5/us...ml#Command-line allows parameters on the command line.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...