Finding Specific Descendant Nodes With XSL:Key

Posted by DBA_Alex on Stack Overflow See other posts from Stack Overflow or by DBA_Alex
Published on 2010-05-27T18:30:57Z Indexed on 2010/05/29 2:32 UTC
Read the original article Hit count: 287

Filed under:
|

Given the following code:

<database>
    <table name="table1">
       <column name="id"/>
       <column name="created_at"/>
    </table>
    <table name="table2">
       <column name="id"/>
       <column name="updated_at"/>
    </table>
</database>

I want to be able to test using an xsl:key if specific table has a specific column by name. For instance, I want to know if a table has a 'created_at' column so I can write specific code within the transformation.

I've gotten a general key that will test if any table has a given column by name, but have not figured out how to make it specific to the table the transformation is currently working with.

    <xsl:key name="columnTest" match="column" use="@name"/>
 <xsl:for-each select="database/table">   
    <xsl:choose>
       <xsl:when test="key('columnTest', 'created_at')">
          <xsl:text>true</xsl:text>
       </xsl:when>
       <xsl:otherwise>
          <xsl:text>false</xsl:text>
       </xsl:otherwise>
    </xsl:choose>
</xsl:for-each>

So I end up with 'true' for all the tables. Any guidance would be appreciated.

© Stack Overflow or respective owner

Related posts about xslt

Related posts about xslkey