Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 18th, 2005, 06:32 AM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSL select by child element value

Hello, I'm an XSLT novice. I have an XML data structure which looks like:

<Products>
<Product>
    <Name>LUMINAIRES FOR EMERGENCY LIGHTING</Name>
    <PDF>006.PDF</PDF>
    <Number>999</Number>
</Product>
</Products>

and I'm doing an XSLT transformation. I'd like to display only the <Product> matched by either <Name> or <Number> values. So, I wrote a filter XSLT with this code:

<xsl:template match="Product">
        <xsl:if test="@Number = /Products/Product/Number">
            <xsl:element name="Product">
                <xsl:value-of select="@Product"/>
            </xsl:element>
        </xsl:if>
        <xsl:if test="@Value= /Products/Product/Value">
            <xsl:element name="Product">
                <xsl:value-of select="@Product"/>
            </xsl:element>
        </xsl:if>

At this point I'm stuck. I want to display the <Product> search result, but am not sure
about the test clauses or about selecting the parent <Product> element.
Can you help?

Thanks in advance.
 
Old May 18th, 2005, 09:56 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm not sure what you are trying to do. In your template, the current node is a Product element. You product element does not have an attribute called Number or Value, so the expressions @Number and @Value will select nothing. And I'm not sure whether you really intended to compare these values with all the product numbers in the whole document, which is what "= /Products/Product/Number" means.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 18th, 2005, 10:09 AM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your message. Let me clarify this, because I am now less confused about it than when I first posted. There are several <Product> elements inside of the root <Products> element. I want to loop over each <Product> element and compare the values of its <Name> and <Number> children nodes with passed-in values. If either of the two values match, to print out the entire <Product> node. The <Name> value should match on a substring, like "Luminaires" should match <Name>LUMINAIRES FOR EMERGENCY LIGHTING</Name>.

So, right now I've adjusted my code as follows, problem is that I get all the <Product> elements instead of just the one with Number = 3000160.

<xsl:template match="/">
    <xsl:for-each select="/Products/Product">
       <xsl:if test="Number = '3000160'">
          <xsl:value-of select="Number" />
       </xsl:if>
    </xsl:for-each>
  <xsl:apply-templates />
</xsl:template>


Sorry about the confusion earlier, I appreciate your help.
 
Old May 21st, 2005, 02:58 AM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got this working finally:

<xsl:template match="/">
        <Products>
            <xsl:apply-templates/>
        </Products>
    </xsl:template>

    <xsl:template match="reporter" />

    <xsl:template match="Product">
        <xsl:choose>
            <xsl:when test="Number = /Products/reporter/search">
                <xsl:copy-of select="."/>
            </xsl:when>
            <xsl:when test="contains(Name, /Products/reporter/search)">
                <xsl:copy-of select="."/>
            </xsl:when>
        </xsl:choose>
    </xsl:template>






Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding the first child element humansky XSLT 5 April 17th, 2008 08:46 AM
Attribute to Child element Conversion kudzuconf XSLT 6 July 13th, 2007 04:02 AM
Access specific Element and its Child elements! suersh79 XML 3 November 22nd, 2006 12:58 AM
child element kgoldvas XML 4 April 27th, 2006 01:51 AM
Match element based on attribute of child? transom XSLT 1 September 11th, 2005 03:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.