• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

xslt looping according to a value

 
Nishant Pandian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys... i have a task to do and I am struck up...

i have a set of data as below..

<books>
<book>
<bookname> xxx </bookname>
<quantity> y </quantity>
<cost> zzz </cost>
</book>
</books>


if the the quantity of the record is say 3 i need the same set of record to be repeated 3 times as below.

<books>
<book>
<bookname> xxx </bookname>
<quantity> y </quantity>
<cost> zzz </cost>
</book>
<book>
<bookname> xxx </bookname>
<quantity> y </quantity>
<cost> zzz </cost>
</book>
<book>
<bookname> xxx </bookname>
<quantity> y </quantity>
<cost> zzz </cost>
</book>
</books>


How can this be done... ???/

 
g tsuji
Ranch Hand
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not see a motive as to how this exercise arises. What if I leave out the functionality of copying "the same set of record to be repeated", can you fill it in yourself? It actually takes one line. But you can do other thing on it, then you can elaborate there.
 
Nishant Pandian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey g tsuji ,


Thanks for your response... But will I be able to call a template from within a for-each.???

A more detailed explanation to my situation is given below.

<xsl:element name="TradeInDetails">
<xsl:for-each select="CascadeNode[@name='trade_in_item']/Record">
<xsl:element name="TradeIn">
<xsl:variable name="trxItemqty" select="Field[@name='trx_line_item.trx_item_quantity']/@value" />
<!--Nishant added/modified starts -->
<xsl:element name="TradeInManufacturer">
<xsl:value-of select="Field[@name="asset.manufacturer"]/@value" />
</xsl:element>
<xsl:element name="TradeInModel">
<xsl:value-of select="Field[@name="asset.title"]/@value" />
</xsl:element>
<xsl:element name="TradeInSerial">
<xsl:value-of select="Field[@name="trx_line_item.serial_number"]/@value" />
</xsl:element>
<xsl:element name="TradeInVIN">
<xsl:value-of select="Field[@name="trx_line_item.vin"]/@value" />
</xsl:element>
<xsl:element name="TradeInAmount" />
<!--Nishant added/modified ends -->
</xsl:element>
</xsl:for-each>
</xsl:element>

In the above code snippet the trxItemqty will hold the quantity. If say trxItemqty=2 then there should be 2 TradeInDetails element. Hope this will ease your question on where my exercise arises. Hoping for a positive response.

Thanks.
 
g tsuji
Ranch Hand
Posts: 656
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will I be able to call a template from within a for-each.???

Sure. Suppose the production of the subelements of TradeIn is going to replicate themselves $trxItemqty number of times, it will be like this. (I produce all the details for this assumed case for illustration only. Any variation of it, you can thereby adapt the demo to it.)

[1] The part of the element TradeInDetails production becomes this.

[2] The template "doit" will be sitting at top level of the xslt and becomes this.
 
Nishant Pandian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a tonne g tsuji !!! it did work out...

I have one more general doubt. Will i be able to call template containing a different for-each inside a for-each of a different element. For example consider the same situation we were discussing about .

I call a template do-it from a for-each of trx_line_item. What if i want to iterate a different object inside my doit template and insert those records in the trx_line_item records... is this feasible???

 
g tsuji
Ranch Hand
Posts: 656
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me ask an even more general question.
Q: Given an xpath-compliant processor operating on a dom document, and given a context (node) of the dom document, is there any other part of the dom document which is unreachable by and inaccessible (zone forbidden) to the xpath processor?
A: None.

So the answer to your question is a yes before even deconstructing the phrase to understand.
 
Nishant Pandian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is my situation.... I have a xpath which goes through customer --> address--> and fetches the contact details .








In the above scenario I am calling a template named "Contact template " whose code snippet is provided below.






The above template will have a XPATH as customer--> contact . Now the above template does not fetch me the records(No records are returned) Kindly help me through this.
 
g tsuji
Ranch Hand
Posts: 656
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[3]
<xsl:for-each select="CascadeNode[@name="customer"]/Record/CascadeNode[@name="address"]/Record/">

There is no such select if you put a slash at the end. It is simply a severe error and the process abruptly stops. It happens there and another place in the named templated.


[4] In the named template, apart from the correction you've to make exactly as [3], you have to retrace back to the context node where you start the first xsl:for-each: that is at least my best guess - nothing more. (You arrive at that kind of entanglement probably for the questionable design, but I give you all the benefit of the doubt.)
 
Nishant Pandian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!!! Made my DAY!!!
 
g tsuji
Ranch Hand
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the feedback. I have unfortunately amendments to make to rectify some obvious overlook.

[amendments]
In order to avoid any misunderstanding, I have to make an amentment to my post above where I had overlooked the propagation of misuses of nested quotes.
Those two xsl:for-each should obviously read as follows.
[3.1]

[4.1]
 
Nishant Pandian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i got it thanks...

Now i have been held up with a another comlex situation (For me )...


I am fetching a field which returns a timestamp as follows..



which returns me a output



But i want the above as


With a 'T' between the date and a time.

Kindly help me out with this.Thanks
 
g tsuji
Ranch Hand
Posts: 656
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still have not got the idea of nested quote/apos right, have you?!

[5] For xslt 1.0 (continue to be applicable in 2.0), you can do this, using translate() function and safeguarded by normalize-space() function.
 
Nishant Pandian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What has to done inorder to omit the whole element if no value is returned.

For e.g.


If the above code returns output like



The whole element must be omitted. i know that this can be done inside a IF condition. But I am having 120 such elements. Is there any generalised way of doing it? I.e., if the value is null, no tag renders.
 
g tsuji
Ranch Hand
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should you not worry about the nested quotes before anything else? Knowingly post invalid code is unprofessional.

There are things you have to take it on as such if there is no regularity about it. If there is some regularity, make it into the template match attribute so that existence of @value is taken care of at that level. In any case, it is unavoidable a node test operation is to take place.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. It's just like any other programming language: if you found yourself writing the same code over and over again in Java, you would write a method to encapsulate that code, right? Same thing in XSLT.
 
Nishant Pandian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a xml like below...




right now I am just getting the value of address.region_code using the following xsl code.




what if i want to get the lookup value of the same field where language='EN'??

Thanks
 
g tsuji
Ranch Hand
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try this.

I don't know what kind of editor/parser you're using, that dreadful nested quotes are getting out of control. Are you doing only mental exercise? Any code testing would point that out and you never correct it? That's incredible!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic