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 20th, 2011, 01:12 AM
Authorized User
 
Join Date: Apr 2011
Posts: 22
Thanks: 4
Thanked 0 Times in 0 Posts
Default Conditional Replace

Hi XSLT Geniuses, I am trying to do a conditional replace in XSLT but dont seem to get it right.

I am trying something like
If condition1 replace ($x, $a)
else If condition1 replace ($x, $b) and so on.

To make the problem more trickier, i have to run this into a loop and should not override the changes made from first iteration, i.e.

if condition1 matches, i need to store the result in a temp and use that as the stringToBeReplaced in condition2.

Is this doable in XSLT ( Using SAXON)?

Note: I am looking for a function similar to functx_replace-multi, but just that my changeFrom and changeTo are dynamic and for some reason when using this function i get the output as the input string itself.
 
Old June 20th, 2011, 03:42 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You can't create temporary variables in XSLT, so from your description above which describes the solution in a procedural manner it is hard to work out how you should do it in XSLT, which is a functional language.

Perhaps if you could give us some examples of your input and your expected output and describe the rules for the transformation then we might be able to help more.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old June 20th, 2011, 04:27 AM
Authorized User
 
Join Date: Apr 2011
Posts: 22
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Input will be like
<Input>
<A Id="1">
<A1 Id="1.1">Value</A1>
</A>
<B Id="2">Y</B>
<C Id="3">Select(A[Id=1]/A1[Id=1.1])</C>
<D Id="4">D</D>
<E Id="5">E</E>
<F Id="6">Select(D[Id=4]) and Select(E[Id=5])</F>
</Input>

Output needs to be:
<Output>
<A Id="1">
<A1 Id="1.1">Value</A1>
</A>
<B Id="2">Y</B>
<C Id="3">Value</C>
<D Id="4">D</D>
<E Id="5">E</E>
<F Id="6">D and E</F>
</Output>

As i said earlier, i find transforming F trickier, cos i do a xpath evaluation and obtain the required result, but not able to replace it.
 
Old June 20th, 2011, 04:38 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

A very strange requirement and I'm really not sure I understand it fully. But it will need saxon:evaluate() (or the new xsl:evaluate instruction in XSLT 3.0) to evaluate the XPath expressions dynamically, and it will need recursion so that the processing of each input element is applied to the result of processing the previous element.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 20th, 2011, 05:44 AM
Authorized User
 
Join Date: Apr 2011
Posts: 22
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Thanks Kay.

To make things easier, the least i can do is generate all elements with the same name as,

<Input>
<A Id="1">
<A1 Id="1.1">Value</A1>
</A>
<A Id="2">Y</A>
<A Id="3">Select(A[Id=1]/A1[Id=1.1])</A>
<A Id="4">D</A>
<A Id="5">E</A>
<A Id="6">Select(A[Id=4]) and Select(A[Id=5])</F>
</Input>

I am able to retrieve the XPATH values by using plain xsl itself (something like storing the root into a variable and retrieving as $root//A[<dynamic>]//A1[<dynamic>]).

The main problem is when there are two selects (like in <A Id = 6>), I am not able to determine which XPATH value goes where.

Any help is appreciated.
 
Old June 20th, 2011, 07:33 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Is "Select(A[Id=1]/A1[Id=1.1])" supposed to an XPath expression selecting nodes in the input sample? I don't see how it selects anything, I think you need "A[@Id = 1]/A1[@Id = 1.1]" as your input has "Id" attributes and not "Id" elements.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old June 20th, 2011, 12:47 PM
Authorized User
 
Join Date: Apr 2011
Posts: 22
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Martin, I have provided just the inout XML. I convert this XML to the format you have specified using concat and other functions. I am stuck in replacing the string after obtaining the XPATH value.
 
Old June 20th, 2011, 12:58 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you are stuck with a specific part of the code and need help solving it then we need to see the specific code, together with an explanation of its input, what it is supposed to to, the current output and the desired output.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old June 20th, 2011, 04:42 PM
Authorized User
 
Join Date: Apr 2011
Posts: 22
Thanks: 4
Thanked 0 Times in 0 Posts
Default

So here is where I am with this change.

I was able separately create a sequence of string to be replaced and sequence of replacement string.

$selectValue is the incoming string for each iteration.

All I need to do is get the following code to work,

<xsl:variable name="some" select="for $m in tokenize($selectValue,'Select')
return if (contains($m,'Id'))
then replace($selectValue in first iteration (the string created in the previous iteration for alll rest),$toBeReplacedString[position() <not working>]),$replacementString[position() <not working>]) else concat($selectValue,.)"/>
<xsl:value-of select="$some" />
 
Old July 9th, 2011, 04:01 AM
Authorized User
 
Join Date: Apr 2011
Posts: 22
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Can i assume this cannot be done using XSLT?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Conditional Formating ks1102 Excel VBA 2 March 12th, 2009 09:28 PM
Conditional Find and Replace Styles creativeconnect Word VBA 1 August 3rd, 2007 12:15 PM
conditional IF roy_mm Reporting Services 0 October 30th, 2006 07:52 AM
Conditional default value twalter Beginning VB 6 1 January 3rd, 2006 06:31 PM
Conditional Login acdsky Classic ASP Basics 2 November 25th, 2003 11:59 AM





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