Question

I'm using XSLT 1.0. I'm basically trying to create output based on an attribute's value(s). Here's an example of the XML I'm converting:

<object include="name number" id="5" />

This is an example of the data it is referencing by the "id" attribute:

<People>

  ...

  <row>
    <id>5</id>
    <number>1455210</number>
    <name>Mike</name>
    <age>38</age>
    <city>London</city>
  </row>

  ...

</People>

Here are parts of my code:

<xsl:template match="object">
  <param name="id" select="@id" />
  <param name="filename">
    ...
  </param>

  ...

  <xsl:apply-templates select="document($filename)//row[id=$id]">
    <xsl:with-param name="include" >
      <xsl:call-template name="tokenize">
        <xsl:with-param name="string" select="@include" />
      </xsl:call-template>
    </xsl:with-param>
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="row">
  <xsl:param name="include" />

  <xsl:for-each select="exsl:node-set($include)/token">
    <span type="{.}">
      INSERT VALUE OF ORIGINAL NODE HERE (i.e. the value of the child of the "row" node with name current() or ".")
    </span>
  </xsl:for-each>
</xsl:template>

<xsl:template name="tokenize">
  <param name="string" />

  Turn a string with multiple values (i.e. "car dog hat") into an XML string
</xsl:template>

Aside from using $id instead of @id, my problem is referencing the node that got passed to the "row" template after I enter the for-each loop. Given the original XML, I would like to have the following output:

<span type="name">Mike</span>
<span type="number">1455210</span>

Instead, I can only manage to get the following:

<span type="name">name</span>
<span type="number">number</span>

I could just use a bunch of "if" statements and not tokenize anything, but the order of the items in the "include" attribute matters, so I feel I need a node set.

Any help or better ways of going about this would be greatly appreciated. Thanks!

Was it helpful?

Solution

Store the outer context node in a variable

<xsl:template match="row">
  <xsl:param name="include" />
  <xsl:variable name="curRow" select="."/>

You can then access $curRow anywhere within the template, including in the inner for-each.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top