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 June 19th, 2008, 03:39 PM
Authorized User
 
Join Date: May 2008
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anboss
Default copying a tag to output based on a query

need help writing xsl for following problem:

the following will be input

<main>
<item>books</item>
<source>A</source>
<destination>B</destination>
<status>yes</status>
</main>

based on the values for source, destination and the text of shipQuery, present in the following xml (stored in c:\)... the tag status should be copied in output.

<shipQuery>
<item source='A' destination='B'>books</item>
<item source='Z' destination='default'>pen</item>
<item source='B' destination='A'>default</item>
</shipQuery>

here a default means dont care. (source/destination/item can be anything)

as we can see my output will be

<main>
<item>pen</item>
<source>A</source>
<destination>B</destination>
<status>yes</status>
</main>

if there is no match in the shipQuery xml,

<main>
<item>pen</item>
<source>A</source>
<destination>B</destination>
</main>

will be my output

how can this be achieved?
 
Old July 14th, 2008, 03:04 PM
Authorized User
 
Join Date: May 2008
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anboss
Default

i am able to do this using the following template. but gives rise to another doubt... the status is supposed to return a node set.. but it returns either true or false. why is it so... ?

    <xsl:template name="getShipStatus">
        <xsl:param name="input"/>
        <xsl:variable name="config" select="document('c:\shipQuery.xml')"/>

            <xsl:variable name="list">
                <xsl:copy-of select="$config/item[normalize-space(.) = $input/main/item/text() or normalize-space(.) = 'default'] "/>
            </xsl:variable>
            <xsl:variable name="status">
                <xsl:copy-of select="$list//item[@source= $input/main/source/text() or @source = 'default'] and $list//item[@destination=$input/main/destination/text() or @destination = 'default']"/>
            </xsl:variable>
<xsl:value-of select="$status"/>
    </xsl:template>

 
Old July 15th, 2008, 03:47 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Look at this expression:

select="$list//item[@source= $input/main/source/text() or @source = 'default'] and $list//item[@destination=$input/main/destination/text() or @destination = 'default']"/>


it can be reduced to

select="E[predicate1] and E[predicate2]"

The result of the "and" operator is always a boolean.

Perhaps you meant "select everything in E[predicate1] and also select everything in E[predicate2]". If so you want the union operator, written "|" , rather than the "and" operator.

Or perhaps you wanted all the Es that satisfy both predicates. In that case you want E[predicate1 and predicate2]".

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 15th, 2008, 07:15 AM
Authorized User
 
Join Date: May 2008
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anboss
Default

Dear Kay... true but, the following is also a boolean expression.

<xsl:copy-of select="$config/item[normalize-space(.) = $input/main/item/text() or normalize-space(.) = 'default'] "/>

something similar to... E[predicate1] or E[predicate2]

but this returns a node set. why is this so.
 
Old July 15th, 2008, 07:25 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The above isn't a boolean expression - it is equivalent to "E[p1 or p2]", i.e. selects all the E elements where p1 and p2 are true.

"E[p1] or E[p2]" means is there an element in E where p1 is true and an element in E where p2 is true.

The position of the square brackets is the important part you seem to be misunderstanding.



/- Sam Judson : Wrox Technical Editor -/
 
Old July 17th, 2008, 06:36 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You seem to have got yourself very confused.

Assuming N is a node-set (or in 2.0 a node-sequence) and P, Q are boolean expressions, then

N[P and Q]

selects those nodes in N for which P and Q are both true, while

N[P or Q]

selects those nodes in N for which either P or Q is true.

If you use a node-set in a context where a boolean is expected, for example <xsl:if test="N[P or Q]">, then the result is true if the node-set is non-empty.

If you write

N[P] and N[Q]

then the expressions "N[P]" and "N[Q]" both select node-sets. The "and" operator converts these node-sets to booleans, so the result is true if neither N[P] nor N[Q] is empty. Similarly

N[P] or N[Q]

is true if either N[P] or N[Q] is non-empty.

I hope this clears things up for you.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 17th, 2008, 11:10 AM
Authorized User
 
Join Date: May 2008
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anboss
Default

yes i understood. Thanks.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying DOCTYPE in input to the output xml mrame XSLT 3 July 31st, 2008 04:26 PM
Extra xmlns="" tag in output Mango_Lier XSLT 4 October 16th, 2007 11:35 AM
How to force XSLT to output a close tag in XML? richcon XSLT 2 June 29th, 2006 02:39 AM
Tag library for field level role based security mmalik_altaf Pro JSP 0 June 7th, 2006 05:02 AM
Trying to output a tag within a tag jaucourt XSLT 3 January 12th, 2005 11:57 AM





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