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

Subject: Re: [xsl] Still thinking to object oriented...
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Wed, 24 Sep 2008 14:27:08 +0530
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