RE: [xsl] passing_cousin_content_as_counter

Subject: RE: [xsl] passing_cousin_content_as_counter
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Sun, 1 Aug 2004 13:55:37 +0100
> I was wondering if someone could, by the use of one 
> stylesheet, select the
> values of the first,
> the fourth and the seventh <ELEM_INFO_ITEM> children of <ELEM_INFO>
> element. Note that the number
> of <ELEM_INFO_ITEM> nodes is not predefined.In this example there are
> exactly nine nodes, but
> in other cases there might be only three or twelve etch, but 
> always number
> that is multiple of
> three.

If there are 12, do you want to select (1,4,7), or (1,4,7,10)? I imagine the
latter. Then the answer is 

select="ELEM_INFO_ITEM[position() mod 3 = 0]
> 
> I then would like to use the extracted values as counter 
> values, to apply
> counting on
> the COORDINATE_ITEM node-set and select specific COORDINATE_ITEM
> elements.In this example, based
> on the extracted values (1, 7 and 11 ), i would like to 
> select the first,
> seventh and eleventh
> COORDINATE_ITEM elements content.

Then you do 

<xsl:for-each select="/SHAPE/ELEM_INFO/ELEM_INFO_ITEM[position() mod 3 =
0]">
  <xsl:value-of select="/SHAPE/COORDINATES_ITEM[number(current())]"/>



> 
> The source xml is the one following:
> 
> 
> <SHAPE>
> - <ELEM_INFO>
>      <ELEM_INFO_ITEM>1</ELEM_INFO_ITEM>
>      <ELEM_INFO_ITEM>1001</ELEM_INFO_ITEM>
>      <ELEM_INFO_ITEM>5000</ELEM_INFO_ITEM>
> 
>      <ELEM_INFO_ITEM>7</ELEM_INFO_ITEM>
>      <ELEM_INFO_ITEM>1002</ELEM_INFO_ITEM>
>      <ELEM_INFO_ITEM>5000</ELEM_INFO_ITEM>
> 
>      <ELEM_INFO_ITEM>11</ELEM_INFO_ITEM>
>      <ELEM_INFO_ITEM>1003</ELEM_INFO_ITEM>
>      <ELEM_INFO_ITEM>5000</ELEM_INFO_ITEM>
>   </ELEM_INFO>
> - <COORDINATES>
>     <COORDINATES_ITEM>68</COORDINATES_ITEM>  !first
>     <COORDINATES_ITEM>54</COORDINATES_ITEM>
>     <COORDINATES_ITEM>72/COORDINATES_ITEM>
>     <COORDINATES_ITEM>65</COORDINATES_ITEM>
>     <COORDINATES_ITEM>83</COORDINATES_ITEM>
>     <COORDINATES_ITEM>17</COORDINATES_ITEM>
>     <COORDINATES_ITEM>20</COORDINATES_ITEM>  !seventh
>     <COORDINATES_ITEM>60</COORDINATES_ITEM>
>     <COORDINATES_ITEM>24</COORDINATES_ITEM>
>     <COORDINATES_ITEM>55</COORDINATES_ITEM>
>     <COORDINATES_ITEM>98</COORDINATES_ITEM>  !eleventh
>     <COORDINATES_ITEM>25</COORDINATES_ITEM>
>   <COORDINATES>
> </SHAPE>
> 
> 
> Can this be achieved with the use of one and only 
> stylesheet??The output i
> want to generate
> should be identical with the source, except for that the selected
> COORDINATE_ITEM elements
> will also contain a character like d for example, 
> concatenated with their
> number value.

This inverts the logic but it is still possible. Use an identity template:

<xsl:template match="*">
 <xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy>
</xsl:template>

together with a template that alters the relevant COORDINATES_ITEM elements:

<xsl:template match="COORDINATES_ITEM[position() = 
     /SHAPE/ELEM_INFO/ELEM_INFO_ITEM[position() mod 3 = 0]]">
  <xsl:copy>
    <xsl:text>d</xsl:text>
    <xsl:value-of select="."/>
  </xsl:copy>
</xsl:template>

Not tested.

(If you don't understand the predicate in this template rule, look up the
XPath 1.0 rules for comparing a number with a node-set)

Michael Kay


That
> last thing is very easy with the use of concat, but what 
> about the previous
> issues.They look
> quite heavy programmaticaly for xslt. Should i use variables??
> 
> 
> output xml
> 
> 
> <SHAPE>
> - <ELEM_INFO>
>      <ELEM_INFO_ITEM>1</ELEM_INFO_ITEM>
>      <ELEM_INFO_ITEM>1001</ELEM_INFO_ITEM>
>      <ELEM_INFO_ITEM>5000</ELEM_INFO_ITEM>
> 
>      <ELEM_INFO_ITEM>7</ELEM_INFO_ITEM>
>      <ELEM_INFO_ITEM>1002</ELEM_INFO_ITEM>
>      <ELEM_INFO_ITEM>5000</ELEM_INFO_ITEM>
> 
>      <ELEM_INFO_ITEM>11</ELEM_INFO_ITEM>
>      <ELEM_INFO_ITEM>1003</ELEM_INFO_ITEM>
>      <ELEM_INFO_ITEM>5000</ELEM_INFO_ITEM>
>   </ELEM_INFO>
> - <COORDINATES>
>     <COORDINATES_ITEM>d68</COORDINATES_ITEM> !first
>     <COORDINATES_ITEM>54</COORDINATES_ITEM>
>     <COORDINATES_ITEM>72/COORDINATES_ITEM>
>     <COORDINATES_ITEM>65</COORDINATES_ITEM>
>     <COORDINATES_ITEM>83</COORDINATES_ITEM>
>     <COORDINATES_ITEM>17</COORDINATES_ITEM>
>     <COORDINATES_ITEM>d20</COORDINATES_ITEM>  !seventh
>     <COORDINATES_ITEM>60</COORDINATES_ITEM>
>     <COORDINATES_ITEM>24</COORDINATES_ITEM>
>     <COORDINATES_ITEM>55</COORDINATES_ITEM>
>     <COORDINATES_ITEM>d98</COORDINATES_ITEM> !eleventh
>     <COORDINATES_ITEM>25</COORDINATES_ITEM>
>   <COORDINATES>
> </SHAPE>
> 
> 
> Regards
> Manousos
> Athens

Current Thread