XSL, select on one attribute, get value of another

Discussion in 'XML' started by cching@mqsoftware.com, Sep 19, 2005.

  1. Guest

    Hi,

    I'm trying to turn this:

    <Component>
    <Properties>
    <Property Name="X" Value="200" />
    <Property Name="Y" Value="200" />
    <Property Name="Width" Value="100" />
    <Property Name="Height" Value="100" />
    ... other properties ...
    </Properties>
    </Component>

    into this:

    <div style="position: absolute; left: 200px; top: 200px; height: 100px;
    width: 100px;">Blah</div>

    I know how to find the nodes individually, e.g.:

    <xsl:template match="Properties/Property[@Name='X']">
    X: <xsl:value-of select="@Value" /> <br />
    </xsl:template>

    <xsl:template match="Properties/Property[@Name='Y']">
    <xsl:variable name="Y" select="@Value" />
    Y: <xsl:value-of select="@Value" /> <br />
    </xsl:template>

    but how can I do it for all of them and feed them into the div
    attributes? I'm sure I'm missing something very fundamental and am
    going to have an aha! experience if someone can help me figure this
    out. Thanks in advance!

    Cheers,
    Craig
     
    , Sep 19, 2005
    #1
    1. Advertisements

  2. Joris Gillis Guest

    Hi,

    On Mon, 19 Sep 2005 17:27:40 +0200, <> wrote:

    > I'm trying to turn this:
    ><Component>
    > <Properties>
    > <Property Name="X" Value="200" />
    > <Property Name="Y" Value="200" />
    > <Property Name="Width" Value="100" />
    > <Property Name="Height" Value="100" />
    > ... other properties ...
    > </Properties>
    > </Component>
    >into this:
    ><div style="position: absolute; left: 200px; top: 200px; height: 100px;
    > width: 100px;">Blah</div>


    There are lots of methods to do this. It's always best to look for a generic solution. Example:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    exclude-result-prefixes="myvar"
    xmlns:myvar="myvariables">
    <xsl:eek:utput method="xml" indent="yes"/>

    <myvar:map xmlns="">
    <group unit="px">
    <item name="X">left</item>
    <item name="Y">top</item>
    <item name="Height">height</item>
    <item name="Width">width</item>
    </group>
    <group>
    <item name="Colour">color</item>
    </group>
    </myvar:map>

    <xsl:variable name="items" select="document('')/xsl:stylesheet/myvar:map//item"/>

    <xsl:template match="Component">
    <div>
    <xsl:apply-templates select="Properties"/>
    <xsl:apply-templates select="*[not(self::properties)]"/>
    </div>
    </xsl:template>

    <xsl:template match="Properties">
    <xsl:attribute name="style">
    <xsl:apply-templates select="Property"/>
    </xsl:attribute>
    </xsl:template>

    <xsl:template match="Property">
    <xsl:value-of select="$items[@name=current()/@Name]"/>
    <xsl:text>: </xsl:text>
    <xsl:value-of select="@Value"/>
    <xsl:value-of select="$items[@name=current()/@Name]/ancestor-or-self::*/@unit[last()]"/>
    <xsl:text>;</xsl:text>
    </xsl:template>

    </xsl:stylesheet>

    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
     
    Joris Gillis, Sep 19, 2005
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kourosh
    Replies:
    6
    Views:
    948
    Peter Flynn
    May 17, 2006
  2. Replies:
    2
    Views:
    868
  3. Replies:
    8
    Views:
    625
  4. Stefan_W
    Replies:
    1
    Views:
    8,621
    Stefan_W
    Nov 5, 2008
  5. palmiere
    Replies:
    1
    Views:
    584
    Erwin Moller
    Feb 9, 2004
Loading...

Share This Page