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 September 26th, 2016, 12:49 AM
Bat Bat is offline
Registered User
 
Join Date: Sep 2016
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Traverse through all sub nodes of two different xml nodes in a single xslt

Node1:
<retrieveSubscriberDetailResponse xmlns:client="http://xmlns.oracle.com/SBLPOAP/POAPRetrieveSubscriberDetail/POAPRetrieveSubscriberDetailProcess" xmlns="http://xmlns.oracle.com/SBLPOAP/POAPRetrieveSubscriberDetail/POAPRetrieveSubscriberDetailProcess">
-
-<client:promotions>
-<client:promotion>
<client:id>139</client:id>
<client:name>P_499_Wolverine</client:name>
<client:Description/>
<client:activationDt>2015-05-10T02:37:16+08:00</client:activationDt>
<client:expiryDt/>
</client:promotion>
-<client:promotion>
<client:id>71</client:id>
<client:name>Free XYZP</client:name>
<client:Description/>
<client:activationDt>2016-01-29T15:30:58+08:00</client:activationDt>
<client:expiryDt>2017-06-01T00:00:00+08:00</client:expiryDt>
</client:promotion>
</client:promotions>

</retrieveSubscriberDetailResponse>

Node2:

<processResponse xmlns:client="http://xmlns.oracle.com/Custom/KPProductsAPI/KPProductsAPI" xmlns="http://xmlns.oracle.com/Custom/KPProductsAPI/KPProductsAPI">
-<client:promotions>
-<client:promotion>
<client:id>139</client:id>
<client:name>KP_MHP_499_Wolverine_KP</client:name>
<client:activationDt>2015-05-10T02:37:16+08:00</client:activationDt>
</client:promotion>
<client:promotion>
<client:id>140</client:id>
<client:name>PQR_XYZ_499_Wolverine_KP</client:name>
<client:activationDt>2015-05-10T02:37:16+08:00</client:activationDt>
</client:promotion>
</client:promotions>
</processResponse>

These two nodes are inputs to my xslt . My output xml should contain all promotions in "retrieveSubscriberDetailResponse " and if any promotion id matches with promotion id in "processresponse" details of promotion in "processresponse" should be selected .

For the above given Node1 and Node2 (input to xslt)

Expected output:

<output>
<promotions>
-<promotion>
<id>139</client:id>
<name>KP_MHP_499_Wolverine_KP</name>
<activationDt>2015-05-10T02:37:16+08:00</activationDt>
</promotion>
<promotion>
<id>71</id>
<name>Free XYZP</name>
<Description/>
<activationDt>2016-01-29T15:30:58+08:00</activationDt>
<expiryDt>2017-06-01T00:00:00+08:00</expiryDt>
</promotion>

</promotions>
</output>


Can anyone please help me out in achieving this
 
Old September 26th, 2016, 01:48 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XSLT 2.0, use

Code:
<xsl:for-each-group select="$doc1//client:promotion, $doc2//client:promotion" group-by="client:id">
In XSLT 3.0, consider xsl:merge.

In XSLT 1.0.... Ask someone else. I've moved on about 10 years ago.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old September 26th, 2016, 02:32 AM
Bat Bat is offline
Registered User
 
Join Date: Sep 2016
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply .
I am receiving an error

XPath expression failed to execute.
An error occurs while processing the XPath expression; the expression is ora:doXSLTransformForDoc("xsl/Transformation_4.xsl", $Invoke1_retrieveSubscriberDetailRequest_OutputVar iable.payload, "Invoke2_process_OutputVariable.payload", $Invoke2_process_OutputVariable.payload).
The XPath expression failed to execute; the reason was: javax.xml.transform.TransformerException: XML-22900: (Fatal Error) An internal error condition occurred..
Check the detailed root cause described in the exception message text and verify that the XPath query is correct.

My xslt :

<xsl:param name="Invoke2_process_OutputVariable.payload"/>
<xsl:template match="/">
<ns4:processResponse>
<ns4:subscribers>
<ns4:Subscriber>
<ns4:promotions>
<xsl:for-each-group select="$Invoke2_process_OutputVariable.payload/ns3:processResponse/ns3:promotions/ns3:promotion,/client:retrieveSubscriberDetailResponse/client:subscribers/client:Subscriber/client:promotions/client:promotion" group-by="ns3:id">
<ns4:promotion>
<ns4:id>
<xsl:value-of select="client:id"/>
</ns4:id>
<ns4:name>
<xsl:value-of select="client:name"/>
</ns4:name>
</ns4:promotion>
</xsl:for-each-group>
</ns4:promotions>
</ns4:Subscriber>
</ns4:subscribers>
</ns4:processResponse>
</xsl:template>


Small correction in the Node2 input .It has different namespace "ns3"

<processResponse xmlns:ns3="http://xmlns.oracle.com/Custom/KPProductsAPI/KPProductsAPI" xmlns="http://xmlns.oracle.com/Custom/KPProductsAPI/KPProductsAPI">
-<ns3:promotions>
-<ns3:promotion>
<ns3:id>139</ns3:id>
<ns3:name>KP_MHP_499_Wolverine_KP</ns3:name>
<ns3:activationDt>2015-05-10T02:37:16+08:00</ns3:activationDt>
</ns3:promotion>
<ns3:promotion>
<ns3:id>140</ns3:id>
<ns3:name>PQR_XYZ_499_Wolverine_KP</ns3:name>
<ns3:activationDt>2015-05-10T02:37:16+08:00</ns3:activationDt>
</ns3:promotion>
</ns3:promotions>
</processResponse>

Can u please suggest me the change in my xslt accordingly .

Last edited by Bat; September 26th, 2016 at 06:45 AM.. Reason: Added extra info
 
Old September 26th, 2016, 01:27 PM
Bat Bat is offline
Registered User
 
Join Date: Sep 2016
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Tried this also but no luck . Am I missing anything here ???


<xsl:stylesheet version="2.0"
---namespaces-----
------>
<xsl:param name="Invoke2_process_OutputVariable.payload"/>
<xsl:key name="cust" match="ns3:promotion" use="ns3:id"/>
<xsl:template match="/">
<ns4:processResponse>
<ns4:subscribers>
<ns4:Subscriber>
<ns4:promotions>
<xsl:for-each select="/client:retrieveSubscriberDetailResponse/client:subscribers/client:Subscriber/client:promotions/client:promotion/client:id">
<!--<xsl:for-each select="$Invoke2_process_OutputVariable.payload/ns3:processResponse/ns3:promotions/ns3:promotion/ns3:id">-->
<xsl:variable name="kf" select="key('cust', .)"/>
<ns4:promotion>
<ns4:id>
<xsl:value-of select="$kf/ns3:id"/>
</ns4:id>
<ns4:name>
<xsl:value-of select="$kf/ns3:name"/>
</ns4:name>
</ns4:promotion>
</xsl:for-each>
</ns4:promotions>
</ns4:Subscriber>
</ns4:subscribers>
</ns4:processResponse>
</xsl:template>

Last edited by Bat; September 26th, 2016 at 01:30 PM.. Reason: Added info
 
Old September 26th, 2016, 06:00 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Can't see anything obviously wrong in your code. I can't test it because you left out critical bits like the namespace declarations. It looks like you're using the Oracle XSLT processor, which as far as I know is an incomplete implementation of XSLT 2.0, so you might get better diagnostics by trying a different processor.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old September 26th, 2016, 09:34 PM
Bat Bat is offline
Registered User
 
Join Date: Sep 2016
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Lightbulb

Yes sir .we r using oracle xslt processor. This is my complete xslt .

<xsl:stylesheet version="2.0"
xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/"
xmlns:xp20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20"
xmlns:bpel="http://docs.oasis-open.org/wsbpel/2.0/process/executable"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:bpm="http://xmlns.oracle.com/bpmn20/extensions"
xmlns:plnk="http://schemas.xmlsoap.org/ws/2003/05/partner-link/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:ns3="http://xmlns.oracle.com/Custom/KLProductsAPI/KLProductsAPI"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:ora="http://schemas.oracle.com/xpath/extension"
xmlns:socket="http://www.oracle.com/XSL/Transform/java/oracle.tip.adapter.socket.ProtocolTranslator"
xmlns:client="http://xmlns.oracle.com/SBLPOAP/POAPRetrieveSubscriberDetail/POAPRetrieveSubscriberDetailProcess"
xmlns:mhdr="http://www.oracle.com/XSL/Transform/java/oracle.tip.mediator.service.common.functions.Media torExtnFunction"
xmlns:oraext="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc"
xmlns:dvm="http://www.oracle.com/XSL/Transform/java/oracle.tip.dvm.LookupValue"
xmlns:hwf="http://xmlns.oracle.com/bpel/workflow/xpath"
xmlns:ns0="http://docs.oasis-open.org/wsbpel/2.0/plnktype"
xmlns:med="http://schemas.oracle.com/mediator/xpath"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ids="http://xmlns.oracle.com/bpel/services/IdentityService/xpath"
xmlns:xdk="http://schemas.oracle.com/bpel/extension/xpath/function/xdk"
xmlns:xref="http://www.oracle.com/XSL/Transform/java/oracle.tip.xref.xpath.XRefXPathFunctions"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:ns4="http://xmlns.oracle.com/Custom/ParsingTwoNodesSample/BPELProcess1"
xmlns:ns1="http://schemas.oracle.com/bpel/extension"
xmlns:ldap="http://schemas.oracle.com/xpath/extension/ldap"
exclude-result-prefixes="xsi xsl ns2 plnk soap ns3 wsdl client ns0 xsd ns1 ns4 bpws xp20 bpel bpm ora socket mhdr oraext dvm hwf med ids xdk xref ldap">
<xsl:param name="Invoke2_process_OutputVariable.payload"/>
<xsl:key name="cust" match="ns3:promotion" use="ns3:id"/>
<xsl:template match="/">
<ns4:processResponse>
<ns4:subscribers>
<ns4:Subscriber>
<ns4:promotions>
<xsl:for-each select="/client:retrieveSubscriberDetailResponse/client:subscribers/client:Subscriber/client:promotions/client:promotion/client:id">
<!--<xsl:for-each select="$Invoke2_process_OutputVariable.payload/ns3:processResponse/ns3:promotions/ns3:promotion/ns3:id">-->
<xsl:variable name="lf" select="key('cust', .)"/>
<ns4:promotion>
<ns4:id>
<xsl:value-of select="$lf/ns3:id"/>
</ns4:id>
<ns4:name>
<xsl:value-of select="$lf/ns3:name"/>
</ns4:name>
</ns4:promotion>
</xsl:for-each>
</ns4:promotions>
</ns4:Subscriber>
</ns4:subscribers>
</ns4:processResponse>
</xsl:template>
</xsl:stylesheet>

The response elements are always null. Below is the response

<processResponse xmlns:ns4="http://xmlns.oracle.com/Custom/ParsingTwoNodesSample/BPELProcess1" xmlns="http://xmlns.oracle.com/Custom/ParsingTwoNodesSample/BPELProcess1">
<ns4:subscribers>
<ns4:Subscriber>
<ns4:promotions>
<ns4:promotion>
<ns4:id/>
<ns4:name/>
</ns4:promotion>
-<ns4:promotion>
<ns4:id/>
<ns4:name/>
</ns4:promotion>
</ns4:promotions>
</ns4:Subscriber>
</ns4:subscribers>
</processResponse>
 
Old September 27th, 2016, 04:19 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm not quite sure what the purpose of your transformation is. All the information in your expected output comes from node1.xml, I can't see what role node2.xml is expected to play. Your call to the key() function is clearly not doing anything useful - I think you are probably confused about which document it is searching. I struggled a bit with the complex namespaces in your code, I couldn't find exactly what was wrong, but the path expressions weren't selecting anything so I simplified them. The stylesheet below does a "nested loop" examination of the two documents, it doesn't produce exactly what you want, but perhaps you can build from it. (When you have long paths that don't select anything, simplifying them to use //*.x is often a good diagnostic technique, even if you then want to build the correct path back in later.

Code:
<xsl:stylesheet version="2.0" xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/"
    xmlns:xp20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20"
    xmlns:bpel="http://docs.oasis-open.org/wsbpel/2.0/process/executable"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:bpm="http://xmlns.oracle.com/bpmn20/extensions"
    xmlns:plnk="http://schemas.xmlsoap.org/ws/2003/05/partner-link/"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:ns3="http://xmlns.oracle.com/Custom/KLProductsAPI/KLProductsAPI"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:ora="http://schemas.oracle.com/xpath/extension"
    xmlns:socket="http://www.oracle.com/XSL/Transform/java/oracle.tip.adapter.socket.ProtocolTranslator"
    xmlns:client="http://xmlns.oracle.com/SBLPOAP/POAPRetrieveSubscriberDetail/POAPRetrieveSubscriberDetailProcess"
    xmlns:mhdr="http://www.oracle.com/XSL/Transform/java/oracle.tip.mediator.service.common.functions.MediatorExtnFunction"
    xmlns:oraext="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc"
    xmlns:dvm="http://www.oracle.com/XSL/Transform/java/oracle.tip.dvm.LookupValue"
    xmlns:hwf="http://xmlns.oracle.com/bpel/workflow/xpath"
    xmlns:ns0="http://docs.oasis-open.org/wsbpel/2.0/plnktype"
    xmlns:med="http://schemas.oracle.com/mediator/xpath"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ids="http://xmlns.oracle.com/bpel/services/IdentityService/xpath"
    xmlns:xdk="http://schemas.oracle.com/bpel/extension/xpath/function/xdk"
    xmlns:xref="http://www.oracle.com/XSL/Transform/java/oracle.tip.xref.xpath.XRefXPathFunctions"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:ns4="http://xmlns.oracle.com/Custom/ParsingTwoNodesSample/BPELProcess1"
    xmlns:ns1="http://schemas.oracle.com/bpel/extension"
    xmlns:ldap="http://schemas.oracle.com/xpath/extension/ldap" exclude-result-prefixes="#all">
    <xsl:param name="node2" select="document('node2.xml')"/>
    <xsl:key name="cust" match="*:promotion" use="*:id"/>
    <xsl:output indent="yes"/>
    <xsl:template match="/">
        <xsl:variable name="node1" select="."/>
        <ns4:processResponse>
            <ns4:subscribers>
                <ns4:Subscriber>
                    <ns4:promotions>
                        <xsl:for-each
                            select="//client:id">
                            <xsl:message>Outer loop processing <xsl:value-of select="."/></xsl:message>
                            <xsl:for-each
                                select="$node2//*:id">
                                <xsl:message>Inner loop processing <xsl:value-of select="."/></xsl:message>
                                <xsl:variable name="lf" select="key('cust', ., $node1)"/>
                                <ns4:promotion>
                                    <ns4:id>
                                        <xsl:value-of select="$lf/*:id"/>
                                    </ns4:id>
                                    <ns4:name>
                                        <xsl:value-of select="$lf/*:name"/>
                                    </ns4:name>
                                </ns4:promotion>
                            </xsl:for-each>
                        </xsl:for-each>
                    </ns4:promotions>
                </ns4:Subscriber>
            </ns4:subscribers>
        </ns4:processResponse>
    </xsl:template>
</xsl:stylesheet>
On Saxon this outputs:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<ns4:processResponse xmlns:ns4="http://xmlns.oracle.com/Custom/ParsingTwoNodesSample/BPELProcess1">
   <ns4:subscribers>
      <ns4:Subscriber>
         <ns4:promotions>
            <ns4:promotion>
               <ns4:id>139</ns4:id>
               <ns4:name>P_499_Wolverine</ns4:name>
            </ns4:promotion>
            <ns4:promotion>
               <ns4:id/>
               <ns4:name/>
            </ns4:promotion>
            <ns4:promotion>
               <ns4:id>139</ns4:id>
               <ns4:name>P_499_Wolverine</ns4:name>
            </ns4:promotion>
            <ns4:promotion>
               <ns4:id/>
               <ns4:name/>
            </ns4:promotion>
         </ns4:promotions>
      </ns4:Subscriber>
   </ns4:subscribers>
</ns4:processResponse>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old September 27th, 2016, 04:55 AM
Bat Bat is offline
Registered User
 
Join Date: Sep 2016
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

TO be frank Sir ...
Input:
=======
<?xml version="1.0" encoding="UTF-8"?>
<input>
<retrieveSubscriberDetailResponse>
<promotions>
<promotion>
<id>139</id>
<name>P_499_Wolverine</name>
<activationDt>2015-05-10T02:37:16+08:00</activationDt>
</promotion>
<promotion>
<id>122</id>
<name>P_XTY_Wolverine</name>
<activationDt>2015-05-10T02:37:16+08:00</activationDt>
</promotion>
<promotion>
<id>71</id>
<name>Free XYZP</name>
<activationDt>2016-01-29T15:30:58+08:00</activationDt>
</promotion>
</promotions>
</retrieveSubscriberDetailResponse>
<processResponse>
<promos>
<promo>
<id>139</id>
<name>KP_MHP_499_Wolverine_KP</name>
<activationDt>2016-09-10T02:37:16+08:00</activationDt>
</promo>
<promo>
<id>122</id>
<name>KP_MHP_122_Wolverine_KP</name>
<activationDt>2016-09-10T08:37:16+08:00</activationDt>
</promo>
<promo>
<id>140</id>
<name>PQR_XYZ_499_Wolverine_KP</name>
<activationDt>2019-09-10T02:37:16+08:00</activationDt>
</promo>
</promos>
</processResponse>
</input>

The Output I am struggling for:
======================

<output>
<promotions>
<promotion>
<id>139</id>
<name>KP_MHP_499_Wolverine_KP</name>
<activationDt>2016-09-10T02:37:16+08:00</activationDt>
</promotion>
<promotion>
<id>122</id>
<name>KP_MHP_122_Wolverine_KP</name>
<activationDt>2016-09-10T08:37:16+08:00</activationDt>
</promotion>
<promotion>
<id>71</id>
<name>Free XYZP</name>
<activationDt>2016-01-29T15:30:58+08:00</activationDt>
</promotion>
</promotions>
</output>





Similar Threads
Thread Thread Starter Forum Replies Last Post
need to add adjacent element(with their child nodes) in to single in XSLT xsltstarter XSLT 8 June 6th, 2012 10:13 AM
Merge nodes using xslt himanshu XSLT 5 September 2nd, 2010 06:40 AM
XSLT 1.0: Looping through nodes kwilliams XSLT 4 December 1st, 2008 06:21 PM
XML + XSLT Compare Nodes tommyready XSLT 5 September 5th, 2007 03:18 AM
Removing nodes with an XSLT Jza XSLT 2 April 19th, 2006 10:06 AM





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