XSL, select on one attribute, get value of another

C

cching

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
 
J

Joris Gillis

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>

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top