Re: Attribute matching

Subject: Re: Attribute matching
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 27 Jul 2000 18:20:12 +0100
Srini,

>I have a problem is displaying an xml element based on the attribute of the
>other element. i.e. I am trying to match the attributes and if they matches
>then trying to display the content in the a window.
[snip]
>and here is the XSL code
><A>
>  <xsl:attribute name="href">javascript:windowWriter('<xsl:apply-templates
>select="/DB2SUB/SQL_Type/SQL_Stmt_Text[@Stmt_No =
>/DB2SUB/SQL_Type/CPU/SQL_Stmt/@Invok_Stmt_No]"/>')</xsl:attribute>
>...
></A>

You don't mention what it is about what happens when you run this that you
don't want to happen, but perhaps explaining what's happening will help you
figure out which bit is going wrong.

When you use xsl:apply-templates, you get the result of the application of
the relevant templates on all the selected nodes.  In your case, the
selected nodes are determined by the value of the 'select' attribute:

  /DB2SUB/SQL_Type/SQL_Stmt_Text[@Stmt_No = 
                             /DB2SUB/SQL_Type/CPU/SQL_Stmt/@Invok_Stmt_No]

To put this into pseudo-English:

All SQL_Stmt_Text elements that
  have a parent SQL_Type element that
    has a parent DB2SUB element that is the document element and
  have a Stmt_No attribute with the same value as
    any Invok_Stmt_No attribute of
      a SQL_Stmt element that
        has a parent CPU element that
          has a parent SQL_Type element that
            has a parent DB2SUB element that is the document element

Let's look at your XML now:

<DB2SUB>
   <SQL_Type>
      <Name>BTRDSN01  </Name>
      <Time_Stamp>16.13.04 19990801</Time_Stamp>
      <Type>DBRM</Type>
      <SQL_Stmt_Text Stmt_No="01"> SELECT * FROM
SYSIM.SYSTABLES</SQL_Stmt_Text>
      <SQL_Stmt_Text Stmt_No="02"> SELECT * FROM
SYSIM.SYSPLANS</SQL_Stmt_Text>
      <CPU>
            <SQL_Stmt Invok_Stmt_No="01">
              <Stmt_Type>Static</Stmt_Type>
             <Exec_Stmt_No>932</Exec_Stmt_No>
             <Exec_Stmt_Text>Open</Exec_Stmt_Text>
             <Invok_Stmt_Text>Declare</Invok_Stmt_Text>
           </SQL_Stmt>
         <SQL_Stmt Invok_Stmt_No="02">
            <Stmt_Type>Static</Stmt_Type>
            <Exec_Stmt_No>1024</Exec_Stmt_No>
            <Exec_Stmt_Text>Fetch</Exec_Stmt_Text>
            <Invok_Stmt_Text>Declare</Invok_Stmt_Text>
         </SQL_Stmt>
         <SQL_Stmt Invok_Stmt_No="01">
            <Stmt_Type>Static</Stmt_Type>
            <Exec_Stmt_No>990</Exec_Stmt_No>
            <Exec_Stmt_Text>Open</Exec_Stmt_Text>
            <Invok_Stmt_Text>Declare</Invok_Stmt_Text>
         </SQL_Stmt>
      </CPU>
      ...
   </SQL_Type>
   ...
</DB2SUB>

The first SQL_Stmt_Text element has a Stmt_No attribute with a value of
'01'.  Looking inside the CPU element, there is a SQL_Stmt element with an
@Invok_Stmt_No equal to '01' (two, in fact), so that SQL_Stmt_Text element
is selected.

The second SQL_Stmt_Text element has a Stmt_No attribute with a value of
'02'.  Again, looking inside the CPU element, there is a SQL_Stmt element
with an @Invok_Stmt_No equal to '02', so that SQL_Stmt_Text element is
selected as well.

So you are applying templates to both of the SQL_Stmt_Text elements, which
should lead to the result ' SELECT * FROM SYSIM.SYSTABLES SELECT * FROM
SYSIM.SYSPLANS'.

It seems likely that what you are *trying* to do is select a specific
SQL_Stmt_Text element with a known @Stmt_No.  The template containing the
XSL code that's causing you problems is probably processing a SQL_Stmt, and
you want to retrieve the relevant text for that particular @Invok_Stmt_No.
To do this, you can either use something like current()/@Invok_Stmt_No (if
the current node is a SQL_Stmt) or assign the statement number to a
variable, e.g.:

  <xsl:variable name="no" select="@Invok_Stmt_No" />
  <A>
    <xsl:attribute name="href">javascript:windowWriter('<xsl:apply-templates
      select="/DB2SUB/SQL_Type/SQL_Stmt_Text[@Stmt_No = 
                                             $no]"/>')</xsl:attribute>
    ...
  </A>

Another possibility is to use a key to index all the SQL_Stmt_Text elements
according to their @Stmt_No, and use the key to quickly retrieve the
relevant one rather than searching through the document each time.

I hope this helps,

Jeni

Dr Jeni Tennison
Epistemics Ltd * Strelley Hall * Nottingham * NG8 6PE
tel: 0115 906 1301 * fax: 0115 906 1304 * email: jeni.tennison@xxxxxxxxxxxxxxxx


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread