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 February 21st, 2007, 01:16 PM
Authorized User
 
Join Date: Nov 2006
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Testing equality between nodes..... help!!

hey guys,
I was wondering if anyone could help me with this..I am new to XSLT and your help will be of great use..
I am trying to compare the values of nodes of two different documents.
Can anybody tell me how do I achieve this.
Example:
Person.xml
<ID>1</ID><Name>AAA</name>
<ID>2</ID><Name>BBB</Name>
<ID>3</ID><Name>CCC</Name>

Cars_Person
<Car>Mitsubishi</Car><PersonID>2</PersonID>
<Car>Honda</Car><PersonID>3</PersonID>
<Car>Ford</Car><PersonID>1</PersonID>

XSLT file should compare person.xml and cars_person and display id of the person and his car in xml format.
<ID>1</ID><Car>Ford</Car>
<ID>2</ID><Car>Mitsubishi</Car>
<ID>3</ID><Car>Honda</Car>

Help will really be appreciated

Thanx
Aditi

 
Old February 21st, 2007, 01:40 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your example only uses data from one of the files, perhaps it was a badly chosen example.

Also, your XML files aren't well-formed so I suspect the problem isn't quite as described. You're making it a lot harder if you don't have wrapper elements around the rows, e.g.

<table>
<row><Car>Mitsubishi</Car><PersonID>2</PersonID></row>
<row><Car>Honda</Car><PersonID>3</PersonID></row>
<row><Car>Ford</Car><PersonID>1</PersonID></row>
</table>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 21st, 2007, 01:45 PM
Authorized User
 
Join Date: Nov 2006
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i just assumed an example to put in the forum..
the originial files are formatted and doesnt have any xml errors.

The problem is that I am unable to find out a way to comapre values of nodes from different files.
Could you help me with this..

Thanx

 
Old February 21st, 2007, 01:54 PM
J1M J1M is offline
Registered User
 
Join Date: Feb 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm having the same problem. I've used <for-each> or <when> with document() to iterate through all the nodes that I want to compare, but inside that loop I can't seem to figure out how to get access to anything other than the first node of the name I am looking for. I need something akin to a 'this' keyword for the current node from another document that is being iterated on because the '.' always gives me a node from the document the xsl file was run on, not the one from document().

Jim
 
Old February 21st, 2007, 01:55 PM
Authorized User
 
Join Date: Nov 2006
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nyways the example I am using is:
Package.xml
<PPackage>
<?xml version="1.0" encoding="UTF-8"?>
<Package Package_Id="4039635" Package_Type_Id="1" Package_Asset_Id="3190895" </Package>
<Package Package_Id="4039643" Package_Type_Id="1" Package_Asset_Id="3190897" </Package>
<Package Package_Id="4039651" Package_Type_Id="1" Package_Asset_Id="3190899" </Package>
<PPackage>

Asset.xml
<?xml version="1.0" encoding="UTF-8"?>
<Asset>
<Asset_Keyword Keyword_Id="3261579" Asset_Id="3190895"/>
<Asset_Keyword Keyword_Id="3261624" Asset_Id="3190895"/>
<Asset_Keyword Keyword_Id="3261625" Asset_Id="3190897"/>
</Asset>

Output I need:

<Package_Id>4039635</Package_Id><Asset_Id>3190895</Asset_ID><Keyword_Id>3261579</keyword_Id>

 
Old February 21st, 2007, 02:18 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The thing akin to the "this" keyword is the current() function - or if you prefer you can assign a variable.

Typical logic is

<xsl:variable name="table1" select="document('table1.xml')/table"/>
<xsl:variable name="table2" select="document('table2.xml')/table"/>

<xsl:for-each select="table1/row">
  <output>
    <c><xsl:value-of select="dataFromTable1"/></c>
    <d><xsl:value-of select="$table2/row[@primary-key = current()/@foreign-key]/dataFromTable2"/></d>
  </output>
</xsl:for-each>

For greater efficiency, and some would say greater clarity, use keys:

<xsl:variable name="k1" match="table1/row" use="@primary-key"/>

then in XSLT 2.0:

   <d><xsl:value-of select="key('k1', current()/@foreign-key, $table1)"/></d>




Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 21st, 2007, 02:21 PM
Authorized User
 
Join Date: Feb 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

trinkets,

create a global variable with the asset.xml
Code:
<xsl:variable name="ASSETS" select="document('asset.xml')" />
and when processing the package nodes from package.xml you can access the corresponding asset with something like this
Code:
<xsl:value-of select="$ASSETS/Asset/Asset_Keyword[@Asset_Id=current()/@Package_Asset_Id]/@Keyword_Id" />
i.e. in the predicate(in the square brackets) use current() to access the Package node being processed

 
Old February 21st, 2007, 02:47 PM
J1M J1M is offline
Registered User
 
Join Date: Feb 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So current() and '.' refer to different nodes (although sometimes these could be the same)?

Jim
 
Old February 21st, 2007, 02:56 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

current() refers to the node that was current (.) at the outermost level of the XPath expression. So inside square brackets, current() refers to the node that was . outside the square brackets.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 21st, 2007, 03:17 PM
J1M J1M is offline
Registered User
 
Join Date: Feb 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm trying to use the example given above to match <EventDef> based on <EventName> and then retrieve the value in <EventDescription>.
Code:
<xsl:value-of select="$FILE2/LogDef/EventDefSection/EventDef[EventName = current()/EventName]/../EventDescription"/>
Does the above example only work when comparing attributes?

I have a tag structure as follows:
Code:
<LogDef>
<EventDefSection>
<EventDef>
  <EventName>Name50</EventName>
  <EventDescription>TheDescription50</EventDescription>
</EventDef>
<EventDef>
  <EventName>Name51</EventName>
  <EventDescription>TheDescription51</EventDescription>
</EventDef>
</EventDefSection>
</LogDef>
Jim





Similar Threads
Thread Thread Starter Forum Replies Last Post
Testing a String gabster XSLT 2 August 15th, 2007 12:13 PM
Unit Testing jeffmorris BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 7 May 14th, 2007 06:51 AM
Name Equality and problems with Java cuskichick Pro JSP 0 June 13th, 2005 12:50 PM
Testing a date Bill Crawley SQL Server 2000 9 November 27th, 2003 02:08 PM
testing mark C# 0 June 4th, 2003 08:37 PM





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