Main.SideBar (edit)
|
Main /
XSL-XSLTFind an Element by an attribute of another ElementHere is an XSL snippet showing how to look-up one element somewhere else in the document based on an attribute of the current element. E.g. suppose you have two sections in the XML, one holding author details and a separate one holding books, and you want to get the author details from the author name attribute of the book. <xsl:template name="authordetail" > <xsl:param name="authname" /> <xsl:comment> Looking for wire <xsl:value-of select="$refid"/> </xsl:comment> <xsl:for-each select="/authors/author[@name=$authname]" > <xsl:element name='p' > <!-- get the author's country of residence --> <xsl:value-of select="@country"/> </xsl:element> </xsl:for-each> </xsl:template> <xsl:template match="books" priority="1"> ... <!-- this puts the wirename and id at the start --> <xsl:call-template name="authordetail"> <xsl:with-param name="authname" select="@authorname" /> </xsl:call-template> ... <xsl:apply-templates/> </xsl:template> See BatchUpdateOfBC for one XSL example;Here's XSL for generating a test plan from a Canoo Webtest XML file<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="webtest"> <h4>Tests for <xsl:value-of select="@name"/></h4> <table> <tr><td><b>Type</b></td> <td><b>Action</b></td> <td><b>Value</b></td> <td><b>Confirm</b></td></tr> <xsl:apply-templates/> </table> </xsl:template> <xsl:template match="webtest/steps/ifStep" priority="1"> <tr><td><xsl:value-of select="name()"/></td> <td><xsl:value-of select="@test"/></td> <td><xsl:value-of select="@description"/></td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/clickButton" priority="1"> <tr><td>Select/Navigate</td> <td><xsl:value-of select="@description"/></td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/clickLink" priority="1"> <tr><td>Select/Navigate</td> <td><xsl:value-of select="@description"/></td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/verifyText" priority="1"> <tr><td></td> <td><xsl:value-of select="@description"/> Text exists?</td> <td><b><xsl:value-of select="@text"/></b></td> <td>Yes/No</td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/verifyInputField" priority="1"> <tr><td></td> <td><xsl:value-of select="@description"/> Text exists?</td> <td><b><xsl:value-of select="@text"/></b></td> <td>Yes/No</td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/setInputField" priority="1"> <tr><td>Set </td> <td><xsl:choose> <xsl:when test="@description"> <xsl:value-of select="@description"/> </xsl:when> <xsl:when test="@forLabel"> <xsl:value-of select="@forLabel"/> </xsl:when> <xsl:when test="@htmlid"> the field with ID <xsl:value-of select="@htmlid"/> </xsl:when> <xsl:otherwise> the field </xsl:otherwise> </xsl:choose> to the value </td> <td><b><xsl:value-of select="@value"/></b></td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/setSelectField" priority="1"> <tr><td>Select Value </td> <td><xsl:value-of select="@description"/> to the value </td> <td><b><xsl:value-of select="@value"/></b></td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/expectDialog" priority="1"> <tr><td></td> <td>Dialog for <b><xsl:value-of select="@description"/></b> should be displayed</td> <td></td> <td>Yes/No</td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/*" priority="-1"> <tr><td><xsl:value-of select="name()"/></td> <td><xsl:value-of select="@description"/></td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/not/*" priority="-1"> <tr><td><xsl:value-of select="name()"/></td> <td><xsl:value-of select="@description"/></td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/ifStep" priority="1"> <tr><td> </td> <td><b><xsl:value-of select="@name"/><xsl:value-of select="@description"/></b></td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/ifStep/*" priority="-1"> <tr><td><xsl:value-of select="name()"/></td> <td><xsl:value-of select="@description"/></td></tr> </xsl:template> <xsl:template match="webtest/steps/ifStep/clickButton" priority="1"> <tr><td>Select/Navigate</td> <td><xsl:value-of select="@description"/></td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/ifStep/clickLink" priority="1"> <tr><td>Select/Navigate</td> <td><xsl:value-of select="@description"/></td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/ifStep/verifyText" priority="1"> <tr><td></td> <td><xsl:value-of select="@description"/> Text exists?</td> <td><b><xsl:value-of select="@text"/></b></td> <td>Yes/No</td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/ifStep/verifyInputField" priority="1"> <tr><td></td> <td><xsl:value-of select="@description"/> Text exists?</td> <td><b><xsl:value-of select="@text"/></b></td> <td>Yes/No</td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/ifStep/setInputField" priority="1"> <tr><td>Set </td> <td><xsl:choose> <xsl:when test="@description"> <xsl:value-of select="@description"/> </xsl:when> <xsl:when test="@forLabel"> <xsl:value-of select="@forLabel"/> </xsl:when> <xsl:when test="@htmlid"> the field with ID <xsl:value-of select="@htmlid"/> </xsl:when> <xsl:otherwise> the field </xsl:otherwise> </xsl:choose> to the value </td> <td><b><xsl:value-of select="@value"/></b></td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/ifStep/setSelectField" priority="1"> <tr><td>Select Value </td> <td><xsl:value-of select="@description"/> to the value </td> <td><b><xsl:value-of select="@value"/></b></td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/ifStep/expectDialog" priority="1"> <tr><td></td> <td>Dialog for <b><xsl:value-of select="@description"/></b> should be displayed</td> <td></td> <td>Yes/No</td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/ifStep/storeElementAttribute" priority="1"> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/ifStep/not/*" priority="-1"> <tr><td><xsl:value-of select="name()"/></td> <td><xsl:value-of select="@description"/></td></tr> <xsl:apply-templates/> </xsl:template> <xsl:template match="webtest/steps/not/*" priority="-1"> <tr><td><xsl:value-of select="name()"/></td> <td><xsl:value-of select="@description"/></td></tr> <xsl:apply-templates/> </xsl:template> </xsl:stylesheet> |