Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0
This is the forum to discuss the Wrox book XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition by Michael Kay; ISBN: 9780470192740
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 7th, 2011, 11:43 AM
Registered User
Points: 24, Level: 1
Points: 24, Level: 1 Points: 24, Level: 1 Points: 24, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default XSLT logic for repeating child elements within parent node

Sorry for the lengthy request...The logic has to be built in XSLT(converting from SOAP to XML)

Input

<m6:hasProperties>
<m6:name>cv2_policy_notprovided</m6:name>
<m6:value>reject</m6:value>
<m6:name>cv2_policy_notchecked</m6:name>
<m6:value>accept</m6:value>
<m6:name>cv2_policy_matched</m6:name>
<m6:value>accept</m6:value>
<m6:name>cv2_policy_notmatched</m6:name>
<m6:value>reject</m6:value>
<m6:name>cv2_policy_partialmatch</m6:name>
<m6:value>reject</m6:value>
<m6:name>postcode_policy_notprovided</m6:name>
<m6:value>reject</m6:value>
<m6:name>postcode_policy_notchecked</m6:name>
<m6:value>accept</m6:value>
<m6:name>postcode_policy_matched</m6:name>
<m6:value>accept</m6:value>
<m6:name>postcode_policy_notmatched</m6:name>
<m6:value>reject</m6:value>
<m6:name>postcode_policy_partialmatch</m6:name>
<m6:value>reject</m6:value>
<m6:name>address_policy_notprovided</m6:name>
<m6:value>reject</m6:value>
<m6:name>address_policy_notchecked</m6:name>
<m6:value>accept</m6:value>
<m6:name>address_policy_matched</m6:name>
<m6:value>accept</m6:value>
<m6:name>address_policy_notmatched</m6:name>
<m6:value>reject</m6:value>
<m6:name>address_policy_partialmatch</m6:name>
<m6:value>reject</m6:value>
</m6:hasProperties>

Output

<ExtendedPolicy>
<cv2_policy notprovided="reject" notchecked="accept" matched="accept" notmatched="reject" partialmatch="reject/>
<postcode_policy notprovided="reject" notchecked="accept" matched="accept" notmatched="reject" partialmatch="reject/>
<address_policy notprovided="reject" notchecked="accept" matched="accept" notmatched="reject" partialmatch="reject/>
</ExtendedPolicy>


1) No definite order(name/value pair) in the incoming request and not necessary that all the 5 attributes will be present for each policy
2) if a particular attribute is absent, then the corrospending attribute should not be built. For example if address_policy_partialmatch is not provided then the attribute in <Address_policy> for partial match should not be built.
3) if a policy is absent, then corrospending policy should be omitted in the extended

In the client request, the children of <hasProperties> node will dictate which attributes are created in the children of output XML <EntendedPolicy> nodes. There are 3 policies and 5 attributes for each polcies. The tricky thing is its getting difficult to handle repeating elements within <has properties> and the attributes has to be built based upon the <value> element.

Please can somene
Reply With Quote
  #2 (permalink)  
Old November 7th, 2011, 12:55 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Like many transformations, this is best split into a number of phases.

Step 1: combine the name-value pairs:

Code:
<xsl:variable name="t1">
<xsl:for-each select="m6:name">
  <att name="{.}" value="{following-sibling::*[1]}"/>
</xsl:for-each>
</xsl:variable>
Step 2: group by policy:

Code:
<xsl:for-each-group select="$t1/att" group-by="substring-before(@name, '_')">
  <xsl:element name="{current-grouping-key()}_policy">
    <xsl:for-each select="current-group()">
       <xsl:attribute name="{substring-after(@name, 'policy_')}" select="{@value}"/>
    </xsl:for-each>
  </xsl:element>
</xsl:for-each-group>
<
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
The Following User Says Thank You to mhkay For This Useful Post:
Srinibondugula (November 7th, 2011)
  #3 (permalink)  
Old November 8th, 2011, 06:41 AM
Registered User
Points: 24, Level: 1
Points: 24, Level: 1 Points: 24, Level: 1 Points: 24, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Firstly thanks a lot Kay. I have tested the code and there seems to be a slight problem with the code execution.

The transformation is failing with reason

Inavlid Xpath 2.0 expression
Unexpected token - "{@value}"

The attribute is being built correclty when it appears to be an issue while populating attribute values. I'm pasting the code that is being used now

<xsl:variable name="path_name" select="/hasProperties/name" />

<xsl:variable name="t1">
<xsl:for-each select="$path_name">
<att name="{.}" value="{following-sibling::*[1]}"/>
</xsl:for-each>
</xsl:variable>

<xsl:for-each-group select="$t1/att" group-by="substring-before(@name, '_')">
<xsl:element name="{current-grouping-key()}_policy">
<xsl:for-each select="current-group()">
<xsl:attribute name="{substring-after(@name, 'policy_')}" select="{@value}"/>
</xsl:for-each>
</xsl:element>
</xsl:for-each-group>

</xsl:template>
</xsl:stylesheet>

Please could you help?
Reply With Quote
  #4 (permalink)  
Old November 8th, 2011, 06:54 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Just get rid of the curly braces {}. select="@value".
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #5 (permalink)  
Old November 8th, 2011, 10:45 AM
Registered User
Points: 24, Level: 1
Points: 24, Level: 1 Points: 24, Level: 1 Points: 24, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hello,

Thanks a lot for the swift reply. I have tested this code on my machine(XML SPY) and it worked fine. But when i tested this on Datapower to convert the request from SOAP to XML, it didnot produce desired result.

It appears that this firmware doesn't support XSLT2.0 and will support only 1.0

Is there any way to code similar logic using the functions in 1.0? If you can point me in right direction at least, that would be greatly appreciated.

Thanks,
Srini
Reply With Quote
  #6 (permalink)  
Old November 8th, 2011, 12:05 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you ask a question on this particular forum then it's reasonable to assume you are looking for an XSLT 2.0 solution.

For a 1.0 solution you need to look at Muenchian grouping. You'll find it described in any good XSLT book - even the one pictured above ;-)
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find child node when there is mentioned (child::*) mlohokare XSLT 3 May 12th, 2009 12:40 PM
Displaying data as a Parent Node with Left Node and Right Node Manoj Bisht Visual Basic 2008 Professionals 0 April 2nd, 2009 02:34 AM
Accessing a node bases on child node value musman0047 XSLT 1 February 27th, 2009 12:26 PM
Problem with displaying parent /child node in C# vishnu108mishra XML 1 November 16th, 2007 05:37 PM
Select parent node when any one child has key pparadesia XSLT 2 January 7th, 2005 11:29 AM



All times are GMT -4. The time now is 01:57 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.