Family
Family

Reputation: 1113

Using the value of an XML element in XSLT transform

I have an XML file like this:

<Root>
  <Sensor xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <position_x>2170</position_x>
    <position_y>1830</position_y>
    <module_number>10</module_number>
    <disabled>false</disabled>
    <sequence_number>0</sequence_number>
    <id_number>0</id_number>
    <channel_number>10</channel_number>
  </Sensor>
</Root>

And I have an XSL file like this:

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
  <xsl:template match='/'>
    <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
      <xsl:for-each select="Root/Sensor">
        <g transform="translate(position_x,position_y)" id="S">
          <text x="118" y="20" font-family="sans-serif" font-size="18px" font-weight="bold" fill="black"><xsl:value-of select="Root/Sensor/sequence_number" /></text>
          <rect x="12" y="32" width="8" height="18" fill="#FFFFFF" stroke="black" stroke-width="1" />
          <text x="23" y="97" font-family="sans-serif" font-size="16px" fill="black"><xsl:value-of select="Root/Sensor/id_number" /></text>
          <text x="23" y="117" font-family="sans-serif" font-size="16px" fill="black"><xsl:value-of select="Root/Sensor/channel_number" /></text>
          <text x="142" y="80" font-family="sans-serif" font-size="20px" font-weight="bold" fill="black">S<xsl:value-of select="Root/Sensor/module_number" /></text>
        </g>
      </xsl:for-each>
    </svg>
  </xsl:template>
</xsl:stylesheet>

The problem is I want the value of position_x (2170) and position_y (1830), in my transform. But I can't work out how to put them there. It seems I'm not able to put

<xsl:value-of select="Root/Sensor/position_x" />

into the transform.

Is there a way to achieve this or am I going about this the wrong way?

EDIT:

If I try to have the line:

<g transform="translate(<xsl:value-of select="position_x"/>,<xsl:value-of select="position_y" />)" id="S">

I get the error: '<', hexadecimal value 0x3C, is illegal in XML attribute values.

Upvotes: 1

Views: 602

Answers (2)

Michael Liu
Michael Liu

Reputation: 55359

First, as Martin Honnen correctly points out, you should remove Root/Sensor/ from all the value-of elements because the current node inside the for-each is a particular Sensor, not the whole document:

<xsl:value-of select="sequence_number" />

Second, in the transform attribute, you should surround position_x and position_y with curly braces to mark them as expressions to be evaluated:

<g transform="translate({position_x},{position_y})" id="S">

So the XSL file should look like this:

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
  <xsl:template match='/'>
    <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
      <xsl:for-each select="Root/Sensor">
        <g transform="translate({position_x},{position_y})" id="S">
          <text x="118" y="20" font-family="sans-serif" font-size="18px" font-weight="bold" fill="black"><xsl:value-of select="sequence_number" /></text>
          <rect x="12" y="32" width="8" height="18" fill="#FFFFFF" stroke="black" stroke-width="1" />
          <text x="23" y="97" font-family="sans-serif" font-size="16px" fill="black"><xsl:value-of select="id_number" /></text>
          <text x="23" y="117" font-family="sans-serif" font-size="16px" fill="black"><xsl:value-of select="channel_number" /></text>
          <text x="142" y="80" font-family="sans-serif" font-size="20px" font-weight="bold" fill="black">S<xsl:value-of select="module_number" /></text>
        </g>
      </xsl:for-each>
    </svg>
  </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167471

Inside of your for-each select="Root/Sensor" the context element is a Sensor element so any paths inside of the for-each should be relative e.g. <xsl:value-of select="position_x"/>.

Upvotes: 1

Related Questions