Re: [xsl] Still thinking to object oriented...

Subject: Re: [xsl] Still thinking to object oriented...
From: "Jonas Bassl" <Jonas@xxxxxxxx>
Date: Mon, 29 Sep 2008 11:39:12 +0200
Hallo,

thank you very much Mukul for helping me with this problem. As i was
testing your solution today, i was really impressed, but there is
still one problem:

the function "distinct-values" filters out all duplicate values of a sequence.
that's okay, but do i do, if my sequence has duplicate values which
should appear anyway?
For example i have the structure like this:

<node_a>
   <node_b>
      <node_c>
         <node_a>
            <node_to_search_1/>
         </node_a>
         <node_b>
            <node_d>
               <node_to_search_2/>
            </node_d>
         </node_b>
      </node_c>
   </node_b>
</node_a>

I am searching for "node_to_search_1" and "node_to_search_2" and i
want the result like this:

node_a
node_b
node_c
node_a
node_to_search_1
node_b
node_d
node_to_search_2

My question:
Is there a way to make the items in the sequence constructed in the
solution from Mukul unique with their position for example, or has
someone another solution for me?

Thank you in advance

Regards,
Jonas

2008/9/24 Mukul Gandhi <gandhi.mukul@xxxxxxxxx>:
> You can leverage some of the XSLT 2.0 facilities for this. Below is a
> stylesheet which works ...
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>                        xmlns:xs="http://www.w3.org/2001/XMLSchema";
>                        version="2.0">
>
>  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
>
>  <xsl:template match="/">
>    <xsl:variable name="temp" as="xs:string*">
>       <xsl:for-each select="//*[contains(name(), 'special_node')]">
>          <xsl:for-each select="ancestor-or-self::*">
>             <xsl:value-of select="name()" />
>          </xsl:for-each>
>       </xsl:for-each>
>    </xsl:variable>
>    <xsl:for-each select="distinct-values($temp)">
>      <xsl:value-of select="." />
>      <xsl:text>&#xa;</xsl:text>
>    </xsl:for-each>
>  </xsl:template>
>
> </xsl:stylesheet>
>
>
> On Wed, Sep 24, 2008 at 12:35 PM, Jonas Bassl
> <jonas.bassl@xxxxxxxxxxxxxx> wrote:
>> Hallo,
>>
>> i have a problem to solve in XSLT and i just need help with the
>> processing of that language. I have an XML-tree as follows:
>>
>> [CODE]
>> <node1>
>>  <node2>
>>     <node3>
>>         <special_node1/>
>>     </node3>
>>     <node4>
>>         <node5>
>>             <node6>
>>                 <special_node2/>
>>             </node6>
>>         </node5>
>>     </node4>
>>  </node2>
>> </node1>
>> [/CODE]
>>
>> What i have to do is, to search for every "special_node*" for printing
>> out the name and potential attributes of that node and all his
>> parents. This is quite easy to get. I do it like this:
>>
>> [CODE]
>> <xsl:stylesheet version="2.0"
>> ...
>>  <xsl:template match="//*">
>>     <xsl:for-each select="//*">
>>         <xsl:variable name="current_name" select="name(.)"/>
>>         <xsl:if test="contains(string($current_name),'Special')">
>>             <xsl:for-each select="ancestor-or-self::node()">
>>                 <xsl:value-of select="name()"></xsl:value-of>
>>             </xsl:for-each>
>>         </xsl:if>
>>     </xsl:for-each>
>>  </xsl:template>
>> </xsl:stylesheet>
>> [/CODE]
>>
>> My problem now is, that i don't want one node to appear twice in the
>> result list.
>> For example in the code above, the result would be something like:
>>
>> node1
>> node2
>> node3
>> special_node1
>> node1
>> node2
>> node4
>> node5
>> node6
>> special_node2
>>
>> You see that "node1" and "node2" appear twice which is right after the
>> style sheet i wrote there.
>> Can anyone help me now to change the style sheet that the following
>> result would be created:
>>
>> node1
>> node2
>> node3
>> special_node1
>> node4
>> node5
>> node6
>> special_node2
>>
>> Needles to say, that this example is by far not as complicated as the
>> real XML-structures, i have to process, but i hope, you will
>> understand the main problem. I would be really happy if someone can
>> help my here because i am racking my brain now since days.
>>
>> Regards,
>>
>> Jonas
>
>
>
> --
> Regards,
> Mukul Gandhi

Current Thread