ksr81
ksr81

Reputation: 1

How to use XSLT to get a row value in a table

I need help in using xslt to retrieve a row value from a table using another row value.

I have a table as follows:

Employee       Manager
ABC           PQR
ABC           LMN
DEF           XYZ
IJK           XYZ

Employee can belong to more than one department and can have more than one manager.

and the xml for the table is:

<List>
    <Mapping>
      <Employee>ABC</Employee>
      <Manager>PQR</Manager>
  </Mapping>
  <Mapping>
      <Employee>ABC</Employee>
      <Manager>LMN</Manager>
  </Mapping>
  <Mapping>
      <Employee>DEF</Employee>
      <Manager>XYZ</Manager>
  </Mapping>
  ...
</List>

I get the employee name from a function. Using employee name as input in XSLT, how should i find the employee's manager name using XSLT. My only output value should be the Manager Name either a list or single value. Passing "ABC" employee name should give me both "PQR" and "LMN" as manager values.

Thanks KSR81

Upvotes: 0

Views: 1235

Answers (2)

LarsH
LarsH

Reputation: 27994

<xsl:param name="empName" />

<xsl:template match="/">
    <xsl:for-each select="//Mapping[Employee = $empName]">
        <xsl:value-of select="Manager"/>
        <xsl:text> </xsl:text>
    </xsl:for-each>
</xsl:template>

This will output the name of each manager for the given employee, separated and terminated by a space.

To do this more efficiently, you will probably want to use keys:

<xsl:param name="empName" />

<xsl:key name="mappingByEmployee" match="Mapping" use="Employee" />

<xsl:template match="/">
    <xsl:for-each select="key('mappingByEmployee', $empName)">
        <xsl:value-of select="Manager"/>
        <xsl:text> </xsl:text>
    </xsl:for-each>
</xsl:template>

Upvotes: 1

indiPy
indiPy

Reputation: 8062

   <xsl:for-each select="Mapping">
     <xsl:if test="Employee = "ABC"">
         <xsl:value-of select="manager"/>
     <xsl:if>
  </xsl:for-each>

Check here for more, and working with parameters here?

Upvotes: 0

Related Questions