遍历单个xslt

时间:2016-09-26 12:14:22

标签: xml xslt-2.0 transformation

节点1:

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

节点2:

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

预期产出:

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

这两个节点是我的xslt的输入。我的输出xml应该包含&#34; retrieveSubscriberDetailResponse&#34;如果任何促销ID与促销ID匹配&#34; processresponse&#34; &#34; processresponse&#34;应该被选中。对于上面给出的Node1和Node2(输入到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:ns2="http://www.abs-c.com/LoggingVariables"
            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/LFProductsAPI/LFProductsAPI"
            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/SBLMVNE/MVNERetrieveSubscriberDetail/MVNERetrieveSubscriberDetailProcess"
            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="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:Subsc riber/client:promotions/client:promotion/client:id">
<!--<xsl:for-each    select="$Invoke2_process_OutputVariable.payload/ns3:processResponse/ns3:promotio    ns/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>

1 个答案:

答案 0 :(得分:0)

代码

<?xml version="1.0" encoding="UTF-8"?>
<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:ns2="http://www.abs-c.com/LoggingVariables" 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/KPProductsAPI/KPProductsAPI" 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="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">
        <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>
    </xsl:param>
    <xsl:output indent="yes"/>
    <xsl:key name="cust" match="ns3:promotion" use="ns3:id"/>
    <xsl:template match="/">
        <ns4:processResponse>
            <ns4:subscribers>
                <ns4:Subscriber>
                    <ns4:promotions>
                        <xsl:apply-templates select="//client:promotions/client:promotion"/>
                    </ns4:promotions>
                </ns4:Subscriber>
            </ns4:subscribers>
        </ns4:processResponse>
    </xsl:template>
    <xsl:template match="client:promotions/client:promotion[key('cust', client:id, $Invoke2_process_OutputVariable.payload)]">
        <xsl:variable name="ref" select="key('cust', client:id, $Invoke2_process_OutputVariable.payload)"/>
        <ns4:promotion>
            <ns4:id>
                <xsl:value-of select="$ref/ns3:id"/>
            </ns4:id>
            <ns4:name>
                <xsl:value-of select="$ref/ns3:name"/>
            </ns4:name>
        </ns4:promotion>
    </xsl:template>
    <xsl:template match="client:promotions/client:promotion[not(key('cust', client:id, $Invoke2_process_OutputVariable.payload))]">
        <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:template>
</xsl:stylesheet>

转换输入

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

进入输出

<?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>KP_MHP_499_Wolverine_KP</ns4:name>
            </ns4:promotion>
            <ns4:promotion>
               <ns4:id>71</ns4:id>
               <ns4:name>Free XYZP</ns4:name>
            </ns4:promotion>
         </ns4:promotions>
      </ns4:Subscriber>
   </ns4:subscribers>
</ns4:processResponse>

根据您自己的需要在输出中添加您想要的其他元素,它只是为了向您展示如何使用键交叉引用元素并检查是否存在元素。