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 July 21st, 2006, 10:37 AM
Authorized User
 
Join Date: Jun 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Selecting Data from External Documents

Ok, I know (I think) how to select nodes from external Documents using the document() and doc() functions, but I need to know how to search or choose specific nodes based on the content of their attributes and then access the values of attributes in the same node. Here is an example of my problem:

Code:
<folder name="pictures" dirroot="Main\data">
<file name="A01CH-10 Overview.jpg" Section="A01CH-10" Description=" Overview" Path="pictures\A01CH-10 Overview.jpg"/>
    <file name="A01CH-20 Overview.jpg" Section="A01CH-20" Description=" Overview" Path="pictures\A01CH-20 Overview.jpg"/>
    <file name="A01CH-20 Patching.jpg" Section="A01CH-20" Description=" Patching" Path="pictures\A01CH-20 Patching.jpg"/>
    <file name="A01CM-10 Corner Break.jpg" Section="A01CM-10" Description=" Corner Break" Path="pictures\A01CM-10 Corner Break.jpg"/>
    <file name="A01CM-10 Overview.jpg" Section="A01CM-10" Description=" Overview" Path="pictures\A01CM-10 Overview.jpg"/>
    <file name="A01EM-10 Overview.jpg" Section="A01EM-10" Description=" Overview" Path="pictures\A01EM-10 Overview.jpg"/>
    <file name="A01GB-10 Overview.jpg" Section="A01GB-10" Description=" Overview" Path="pictures\A01GB-10 Overview.jpg"/>
    <file name="A01GB-20 Overview.jpg" Section="A01GB-20" Description=" Overview" Path="pictures\A01GB-20 Overview.jpg"/>
So I have to access this document as a external document, and then select the name and Description attributes only from those file nodes where the Section attribute = x, where x is combination of two nodes' values in my source document:

Code:
<xsl:copy-of select="DATE"/>
<xsl:copy-of select="SAMPLES"/>
<xsl:copy-of select="BranchID"/>
<xsl:copy-of select="SectionID"/>
<xsl:copy-of select="Surface"/>
<xsl:copy-of select="Condition"/>
Where BranchID and SectionID would concat to equal the Section attribute from the first snippet. For example, if BranchID = "AO1CH" and SectionID = "20", I would need to get the data "A01CH-20 Overview.jpg" and "A01CH-20 Patching.jpg" from the external document. Ideally, it would look like this:

Code:
<pavement_x0020_Branch>
                <DATE>2006-04-13T00:00:00</DATE>
                <SAMPLES>197</SAMPLES>
                <BranchID>A01CH</BranchID>
                <SectionID>20</SectionID>
                <Surface>PCC</Surface>
                <Condition>58</Condition>
                                <Pictures>
                                    <Picture>A01CH-20 Overview.jpg</Picture>
                                    <Picture>A01CH-20 Patching.jpg</Picture>   
                                </Pictures>
</pavement_x0020_Branch>
My two problems are concating the two node and using the result to search through the external document. I am studying right now how to use keys to search, but am not having much luck. Any help would be great.

Thanks.
 
Old July 21st, 2006, 12:26 PM
Authorized User
 
Join Date: May 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

presumably you have something like this:
<?xml version="1.0" encoding="UTF-8"?>
<branches>
    <pavement_x0020_Branch>
        <DATE>2006-04-13T00:00:00</DATE>
        <SAMPLES>197</SAMPLES>
        <BranchID>A01CH</BranchID>
        <SectionID>20</SectionID>
        <Surface>PCC</Surface>
        <Condition>58</Condition>
    </pavement_x0020_Branch>
    <pavement_x0020_Branch>
        <DATE>2006-04-13T00:00:00</DATE>
        <SAMPLES>197</SAMPLES>
        <BranchID>A01CM</BranchID>
        <SectionID>10</SectionID>
        <Surface>PCC</Surface>
        <Condition>60</Condition>
    </pavement_x0020_Branch>
    <pavement_x0020_Branch>
        <DATE>2006-04-13T00:00:00</DATE>
        <SAMPLES>197</SAMPLES>
        <BranchID>A01EM</BranchID>
        <SectionID>10</SectionID>
        <Surface>PCC</Surface>
        <Condition>50</Condition>
    </pavement_x0020_Branch>
    <pavement_x0020_Branch>
        <DATE>2006-04-13T00:00:00</DATE>
        <SAMPLES>197</SAMPLES>
        <BranchID>A01GB</BranchID>
        <SectionID>10</SectionID>
        <Surface>PCC</Surface>
        <Condition>80</Condition>
    </pavement_x0020_Branch>
</branches>
you can create a variable with the two values with xsl:variable and the concat function and
check to see if a value in the document is equal to this variable and if so loop over the found values to create the pictures nodes like this:

<xsl:transform
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0"
    >
    <xsl:output indent="yes"/>
    <xsl:template match="branches">
        <branches>
            <xsl:for-each select="pavement_x0020_Branch">
                <branch>
                    <xsl:copy-of select="DATE"/>
                    <xsl:copy-of select="SAMPLES"/>
                    <xsl:copy-of select="BranchID"/>
                    <xsl:copy-of select="SectionID"/>
                    <xsl:copy-of select="Surface"/>
                    <xsl:copy-of select="Condition"/>
                    <xsl:variable name="code" select="concat(BranchID,'-', SectionID)" />
                    <xsl:if test="document('folder.xml')//file[@Section=$code]">
                        <pictures>
                        <xsl:for-each select="document('folder.xml')//file[@Section=$code]">
                            <picture>
                                <xsl:value-of select="@name"/>
                            </picture>
                        </xsl:for-each>
                        </pictures>
                    </xsl:if>
                </branch>
            </xsl:for-each>
        </branches>
    </xsl:template>
</xsl:transform>
 
Old July 24th, 2006, 10:35 AM
Authorized User
 
Join Date: Jun 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This works perfectly. Thanks you very much.

Oddly, it does not work under Altova, but fine under Saxon. Under Altova, it cannot find the external xml document, thought Saxon can.

Anyway...thanks again.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting Data from a table nkrust Javascript 7 January 11th, 2007 05:44 AM
Selecting Data.. NitinJoshi General .NET 3 January 18th, 2005 02:59 AM
Read external data rajanikrishna Classic ASP Databases 0 May 6th, 2004 03:24 AM
Selecting data from lookup fields hashtlishnii Access 3 September 25th, 2003 11:14 PM





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