1
votes

I am applying xpath expression(s) into an XSLT stylesheet mainly in the for each and value-of sections of anXSLT stylesheet using javascript embedded in an HTML form. I am wanting to filter my table data (multiple XML elements data that is being displayed as an HTML table in an area of the HTML form). If i had dvd title and price elements what would be an XPATH expression including predicate to let me display all the DVD title and price information (both elements) in a table showing only those with price > 2.00? At the moment I am having a lot of problems with regards to handling predicates to filter my table data and showing all the rows data for the 2 elements. I have an xpath expression that lets me filter and show ONLY one of the elements but NOT the two. I would like to know how to show 2 elements in a row (elements at the same level in the DOM) where one is filtering the other?

Grateful for any help.

I have update with the xml file

<?xml version="1.0"?>
<catalog>
<dvd>
<title>Gone with the wind</title><price>2.00</price>
</dvd>
<dvd>
<title>Independence Day</title><price>3.00</price>
</award>
</catalog>

As mentioned above I would like to have output in my HTML table the title AND price output where the row(s) output is those that satisfy price > 2.00

The content of the XML is simplified and there would be much more DVD elements in it. I am passing XPATH expressions as strings into the XSLT template in the for each and the value of select parts of the XSLT stylesheet.

The XSLT snippet is below. In javascript I am replacing the "*" in for each with XPATH expression string "price[.>2.00]" which shows the price > 2.00. I want to be able to show the title as well for that price as a row in my HTML table

<xsl:template match="/*/*">
  <tr>    
  <xsl:for-each select="*">
     <td><xsl:value-of select="."/></td>
  </xsl:for-each>
  </tr>
</xsl:template>
2
Do you have some code examples explaining the question better?Joep
You forgot to provide the source XML document (complete but minimal, please) and for this document you must specify which exactly nodes you want to be selected. Please, edit the question and provide this missing, important information.Dimitre Novatchev
daveb, Good you included the important information. One last thing is that it is good to provide both the XML document and the XSLT code (the latter is OK) in a well-indented, readable form. +1.Dimitre Novatchev

2 Answers

0
votes
<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
        <table>
            <tbody>
                <xsl:apply-templates select="/catalog/dvd[number(price) > 2.0]"/>
            </tbody>
        </table>
    </xsl:template>

    <xsl:template match="dvd">
        <tr>
            <td><xsl:value-of select="title"/></td>
            <td><xsl:value-of select="price"/></td>
        </tr>
    </xsl:template>

</xsl:transform>
0
votes

Use:

/*/dvd[price > 2]

This selects any element named dvd that is a child of the top element of the XML document and that has at least one price child, whose string value converted to a number is greater than 2.