Re: [xsl] XSL help

Subject: Re: [xsl] XSL help
From: Jonas Mellin <jonas.mellin@xxxxxx>
Date: Mon, 18 Aug 2008 13:50:11 +0200
Sathasivam, Elayaraja wrote, On 2008-08-18 11:33:
I am new to XSL:key.

Could any one tell for the following,
1) <xsl:variable name="result" select="key($index,$key)"/> what it does

From XSLT 2.0: http://www.w3.org/TR/xslt20/#function-key

The result is a sequence containing every node $N that satisfies the
following conditions:

*

     |$N/ancestor-or-self::node() intersect $top| is non-empty. (If the
     third argument is omitted, |$top| defaults to |/|)

*

     $N matches the pattern specified in the |match| attribute of an
     |xsl:key| <http://www.w3.org/TR/xslt20/#element-key> declaration
     whose |name| attribute matches the name specified in the
     |$key-name| argument.

*

     When the key specifier
     <http://www.w3.org/TR/xslt20/#dt-key-specifier> of that |xsl:key|
     <http://www.w3.org/TR/xslt20/#element-key> declaration is
     evaluated with a singleton focus
     <http://www.w3.org/TR/xslt20/#dt-singleton-focus> based on $N, the
     atomized <http://www.w3.org/TR/xslt20/#dt-atomization> value of
     the resulting sequence includes a value that compares equal to at
     least one item in the atomized value of the sequence supplied as
     |$key-value|, under the rules of the |eq| operator with the
     collation selected as described above.

Somewhere you need an index declaration by using xsl:key tag, for example,

<xsl:key name="func" match="prototype" use="@name"/>

The $index in your question refer to the name of the index, in my example, the
"func". The $key is matched against all nodes
that matches "prototype" and the value of the "@name" attribute is compared to
the $key variable. All nodes whose @name attribute
matches the $key in an element that matched the pattern "prototype" are part
of the sequence

<a>
<prototype @name="1">
One
</protoype>

<prototype @name="2">
Two
</protoype>

<prototype @name="1">
Three
</protoype>
</a>

and the statement
<xsl:variable name="zz" select="key('func','1')"/> would set the variable zz
to the two elements prototype with text "One" and "Three".

The behavior in XSLT 1.0 is similar. See
http://www.w3.org/TR/xslt#function-key.








2) To find long descripting, the existing xsl mapping for Type="PP" and
searching keyword for ZZ.  Question: without specifying the attibue name
such as Pkey and Type....How it searching the output of LongDes as ZO
Contracts ???

I do not understand the question. Do you want to search all elements
with an attribute "LongDes" that matches the value "ZO Contracts"?
From the exising Input xml file,

<Document>
  <Legend>
	<TypeDesc Type="PP" Id="1" PKey="ZZ" xml:lang="EN" ShDes="ZZ"
LongDes="ZO Contracts"/>
  </Legend>
</Document>

Requirement: To find LongDes

Exising XSL file:

<xsl:call-template name="getLegend">
	<xsl:with-param name="cat" select="'PP'"/>
	<xsl:with-param name="key" select="'ZZ'"/>
</xsl:call-template>

<xsl:template name="getLegend">
	<xsl:param name="cat"/>
	<xsl:param name="key"/>
	<xsl:call-template name="lookup">
		<xsl:with-param name="index" select="'legendIndex'"/>
		<xsl:with-param name="key"
select="concat($cat,'_',$key)"/>
		<xsl:with-param name="doc" select="$legend"/>
		<xsl:with-param name="expr" select="'@LongDes'"/>
	</xsl:call-template>
</xsl:template>

<xxsl:template name="lookup">
	<!-- index name, i.e. name of the key -->
	<xsl:param name="index"/>
	<xsl:param name="key"/>
	<!-- default value -->
	<xsl:param name="default" select="''"/>
	<!-- node in index source document, per default default value
-->
	<xsl:param name="doc" select="$default"/>
	<!--expr to evaluate to return value, per default result node
-->
	<xsl:param name="expr" select="'.'"/>
	<!-- force context to document where the lookup table is -->
	<xsl:for-each select="$doc[1]">
		<xsl:variable name="result" select="key($index,$key)"/>
		<xsl:choose>
			<xsl:when test="$result">
				<xsl:value-of
select="xalan:evaluate(concat('$result[1]/',$expr))"/>
			</xsl:when>
			<xsl:when test="$default">
				<xsl:value-of select="$default"/>
			</xsl:when>
		</xsl:choose>
	</xsl:for-each>
</xsl:template>




--
Carpe Diem!
===
Jonas Mellin, Assistant Professor in Computer Science
School of Humanities and Informatics, Building E-2
University of Skvvde, P.O. Box 408, SE-541 28 Skvvde, Sweden
Phone: +46 500 448321, Fax: +46 500 448399
Email: jonas.mellin@xxxxxx, URL: http://www.his.se/melj

Current Thread