 |
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 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
|
|
|
|

November 7th, 2011, 11:43 AM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|

November 7th, 2011, 12:55 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|
The Following User Says Thank You to mhkay For This Useful Post:
|
|
|

November 8th, 2011, 06:41 AM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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?
|
|

November 8th, 2011, 06:54 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Just get rid of the curly braces {}. select="@value".
|
|

November 8th, 2011, 10:45 AM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|

November 8th, 2011, 12:05 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|
 |