XSLT problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gendag

    XSLT problem

    Hi all,
    I have a simple xml file:

    <imgs>
    <img name="1"/>
    <img name="2"/>
    <img name="3"/>
    <img name="4"/>
    </imgs>

    And i have the following (simplified) XSLT:

    <?xml version="1.0"?>
    <xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/imgs>
    <for-each select="img">
    <for-each select="../img">
    <xsl:choose>
    <xsl:when test="???">
    <span style="color: red;">
    <xsl:value-of select="@name"/>
    </span>
    </xsl:when>

    <xsl:otherwis e>
    <xsl:value-of select="@name"/>
    </xsl:otherwise>
    </xsl:choose>
    </for-each>
    </for-each>
    </xsl:template>

    </xsl:stylesheet>

    What I want to do is: for each IMG, print _all_ the IMG and if the IMG
    in the second for-each is equal to the IMG in the first for-each, color
    it of red. But how can I say with XPath "if the IMG name in equal to the
    name of the IMG in the outer for-each"? I remember that there is the
    possibility to express it with XPath, but i don't remember how.

    Thx!

    Gendag
  • Joris Gillis

    #2
    Re: XSLT problem

    Hi,
    Tempore 21:28:13, die Sunday 25 September 2005 AD, hinc in foro {comp.text.xml} scripsit Gendag <thenews-no_spam_please@ libero.it>:
    [color=blue]
    > What I want to do is: for each IMG, print _all_ the IMG and if the IMG
    > in the second for-each is equal to the IMG in the first for-each, color
    > it of red. But how can I say with XPath "if the IMG name in equal to the
    > name of the IMG in the outer for-each"? I remember that there is the
    > possibility to express it with XPath[/color]

    There is no such Xpath solution.
    You'll have to use at least one variable.
    You can use 'current()' to select to context node within Xpath predicates, but there's no way to recall the context node of an outer loop.

    regards,
    --
    Joris Gillis (http://users.telenet.be/root-jg/me.html)
    «Numquam mores quos extuli refero» - Seneca , Ad Lucilium I, 7

    Comment

    • toudidel

      #3
      Re: XSLT problem

      don't use <xsl:for-each> - it's terrible construction (instead of externl
      xml document's "looping" with document() function). Instead of it you should
      use <xsl:apply-templates match="img"> and <xsl:template match="img">... to
      do it


      Comment

      • Gendag

        #4
        Re: XSLT problem

        Joris Gillis wrote:
        [color=blue]
        > There is no such Xpath solution.
        > You'll have to use at least one variable.
        > You can use 'current()' to select to context node within Xpath
        > predicates, but there's no way to recall the context node of an outer loop.[/color]

        Thx for the advice, I think I use a variable to solve the problem.
        The function of which i didn't remember the name was 'current()', but as
        you say, it woks only insiede an Xpath. Thx to rember me the nome of it :-)

        Gendag

        Comment

        • Gendag

          #5
          Re: XSLT problem

          toudidel wrote:[color=blue]
          > don't use <xsl:for-each> - it's terrible construction (instead of externl
          > xml document's "looping" with document() function). Instead of it you should
          > use <xsl:apply-templates match="img"> and <xsl:template match="img">... to
          > do it[/color]

          Thx of the advice.
          I know that a recursive style is better of the iterative one, but I used
          the 'for-each' beacuse the stylesheet is very simple and i wrote it
          quickly :-)

          Gendag

          Comment

          Working...