无法使用xsl:for-each映射所有xml元素

时间:2015-11-02 14:05:33

标签: xslt

我一直在尝试映射特定节点下所有子元素的出现。父母可能有多个孩子,孩子可能有多个孩子产品。我的样本xml是这样的。

<?xml version="1.0" encoding="UTF-8" ?>
<PreAuthAnet InstanceId="InstanceId1" xmlns="http://www.portal.com/schemas/CRMSync">
   <AccountObj>AccountObjparent</AccountObj>
   <Status>-0</Status>
   <ReasonCode>-0</ReasonCode>
   <Days>-0</Days>
   <PayType>-0</PayType>
   <ChildAccount>
      <AccountObj>AccountObjchild1</AccountObj>
      <Vin>Vin5</Vin>
         <ChildProducts>
         <ProductObj>ProductObj1</ProductObj>
         <Amount>80.73</Amount>
         <Tax>60.73</Tax>
      </ChildProducts>
      <ChildProducts>
         <ProductObj>ProductObj2</ProductObj>
         <Amount>82.73</Amount>
         <Tax>62.73</Tax>
      </ChildProducts>
    </ChildAccount>
   <ChildAccount>
      <AccountObj>AccountObjchild2</AccountObj>
      <Vin>Vin10</Vin>
            <ChildProducts>
         <ProductObj>ProductObj3</ProductObj>
         <Amount>200.73</Amount>
         <Tax>160.73</Tax>
      </ChildProducts>
    </ChildAccount>
</PreAuthAnet>

我的预期输出xml是:

<?xml version = '1.0' encoding = 'UTF-8'?>
<msg_out:PreAuthAnet xmlns:msg_out="http://www.portal.com/schemas/CRMSync">
   <msg_out:ChildAccount>
      <msg_out:ChildAccountObj>AccountObjchild1</msg_out:ChildAccountObj>
      <msg_out:VIN>Vin5</msg_out:VIN>
      <msg_out:ParentAccountObj>AccountObjparent</msg_out:ParentAccountObj>
      <msg_out:Status>0</msg_out:Status>
      <msg_out:ReasonCode>0</msg_out:ReasonCode>
      <msg_out:Days>0</msg_out:Days>
      <msg_out:PayType>0</msg_out:PayType>
      <msg_out:ChildProducts>
         <msg_out:ProductObj>ProductObj1</msg_out:ProductObj>
         <msg_out:Amount>80.73</msg_out:Amount>
         <msg_out:Tax>60.73</msg_out:Tax>
      </msg_out:ChildProducts>
      <msg_out:ChildProducts>
         <msg_out:ProductObj>ProductObj2</msg_out:ProductObj>
         <msg_out:Amount>81.73</msg_out:Amount>
         <msg_out:Tax>61.73</msg_out:Tax>
      </msg_out:ChildProducts>
     </msg_out:ChildAccount>
     <msg_out:ChildAccount>
      <msg_out:ChildAccountObj>AccountObjchild2</msg_out:ChildAccountObj>
      <msg_out:VIN>Vin10</msg_out:VIN>
      <msg_out:ParentAccountObj>AccountObjparent</msg_out:ParentAccountObj>
      <msg_out:Status>0</msg_out:Status>
      <msg_out:ReasonCode>0</msg_out:ReasonCode>
      <msg_out:Days>0</msg_out:Days>
      <msg_out:PayType>0</msg_out:PayType>
      <msg_out:ChildProducts>
         <msg_out:ProductObj>ProductObj10</msg_out:ProductObj>
         <msg_out:Amount>100</msg_out:Amount>
         <msg_out:Tax>73</msg_out:Tax>
      </msg_out:ChildProducts>
</msg_out:PreAuthAnet>

我一直在使用下面的xslt,但它没有遍历所有子帐户。非常感谢任何帮助。

<?xml version="1.0" encoding="UTF-8" ?>
<?oracle-xsl-mapper
  <!-- SPECIFICATION OF MAP SOURCES AND TARGETS, DO NOT MODIFY. -->
  <mapSources>
    <source type="WSDL">
      <schema location="../SyncPreAuthNotification10Days_AQ.wsdl"/>
      <rootElement name="PreAuthAnet" namespace="http://www.portal.com/schemas/CRMSync"/>
    </source>
  </mapSources>
  <mapTargets>
    <target type="WSDL">
      <schema location="../PreAuth10DaysBPELProcess.wsdl"/>
      <rootElement name="PreAuthAnet" namespace="http://www.portal.com/schemas/CRMSync"/>
    </target>
  </mapTargets>
  <!-- GENERATED BY ORACLE XSL MAPPER 11.1.1.7.8(build 150622.2350.0222) AT [MON NOV 02 17:52:42 IST 2015]. -->
?>
<xsl:stylesheet version="1.0"
                xmlns:aia="http://www.oracle.com/XSL/Transform/java/oracle.apps.aia.core.xpath.AIAFunctions"
                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:client="http://xmlns.oracle.com/Application_Test2/SyncPreAuthNotifications10Days/PreAuth10DaysBPELProcess"
                xmlns:pc="http://xmlns.oracle.com/pcbpel/"
                xmlns:bpm="http://xmlns.oracle.com/bpmn20/extensions"
                xmlns:ns2="http://www.siebel.com/xml/MinacsBRMNotificationsIO"
                xmlns:plt="http://schemas.xmlsoap.org/ws/2003/05/partner-link/"
                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: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:jca="http://xmlns.oracle.com/pcbpel/wsdl/jca/"
                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:tns="http://xmlns.oracle.com/pcbpel/adapter/AQ/Application_Test2/SyncPreAuthNotifications10Days/SyncPreAuthNotification10Days_AQ"
                xmlns:msg_out="http://www.portal.com/schemas/CRMSync"
                xmlns:ldap="http://schemas.oracle.com/xpath/extension/ldap"
                exclude-result-prefixes="xsi xsl pc plt wsdl jca xsd tns msg_out client ns2 aia bpws xp20 bpel bpm ora socket mhdr oraext dvm hwf med ids xdk xref ldap">
  <xsl:template match="/">
    <msg_out:PreAuthAnet>
      <msg_out:ChildAccount>
        <msg_out:ChildAccountObj>
          <xsl:value-of select="/msg_out:PreAuthAnet/msg_out:ChildAccount/msg_out:AccountObj"/>
        </msg_out:ChildAccountObj>
        <msg_out:VIN>
          <xsl:value-of select="/msg_out:PreAuthAnet/msg_out:ChildAccount/msg_out:Vin"/>
        </msg_out:VIN>
        <msg_out:ParentAccountObj>
          <xsl:value-of select="/msg_out:PreAuthAnet/msg_out:AccountObj"/>
        </msg_out:ParentAccountObj>
        <msg_out:Status>
          <xsl:value-of select="/msg_out:PreAuthAnet/msg_out:Status"/>
        </msg_out:Status>
        <msg_out:ReasonCode>
          <xsl:value-of select="/msg_out:PreAuthAnet/msg_out:ReasonCode"/>
        </msg_out:ReasonCode>
        <msg_out:Days>
          <xsl:value-of select="/msg_out:PreAuthAnet/msg_out:Days"/>
        </msg_out:Days>
        <msg_out:PayType>
          <xsl:value-of select="/msg_out:PreAuthAnet/msg_out:PayType"/>
        </msg_out:PayType>
        <xsl:for-each select="/msg_out:PreAuthAnet/msg_out:ChildAccount/msg_out:ChildProducts">
          <msg_out:ChildProducts>
            <msg_out:ProductObj>
              <xsl:value-of select="msg_out:ProductObj"/>
            </msg_out:ProductObj>
            <msg_out:Amount>
              <xsl:value-of select="msg_out:Amount"/>
            </msg_out:Amount>
            <msg_out:Tax>
              <xsl:value-of select="msg_out:Tax"/>
            </msg_out:Tax>
          </msg_out:ChildProducts>
        </xsl:for-each>
      </msg_out:ChildAccount>
    </msg_out:PreAuthAnet>
  </xsl:template>
</xsl:stylesheet>

由于 新手。

1 个答案:

答案 0 :(得分:0)

AFAICT,你想做:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msg_out="http://www.portal.com/schemas/CRMSync">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/msg_out:PreAuthAnet">
    <msg_out:PreAuthAnet>
        <xsl:for-each select="msg_out:ChildAccount">
            <msg_out:ChildAccount>
                <msg_out:ChildAccountObj>
                    <xsl:value-of select="msg_out:AccountObj"/>
                </msg_out:ChildAccountObj>
                <msg_out:VIN>
                    <xsl:value-of select="msg_out:Vin"/>
                </msg_out:VIN>
                <msg_out:ParentAccountObj>
                    <xsl:value-of select="../msg_out:AccountObj"/>
                </msg_out:ParentAccountObj>
                <msg_out:Status>
                    <xsl:value-of select="../msg_out:Status"/>
                </msg_out:Status>
                <msg_out:ReasonCode>
                    <xsl:value-of select="../msg_out:ReasonCode"/>
                </msg_out:ReasonCode>
                <msg_out:Days>
                    <xsl:value-of select="../msg_out:Days"/>
                </msg_out:Days>
                <msg_out:PayType>
                    <xsl:value-of select="../msg_out:PayType"/>
                </msg_out:PayType>
                <xsl:for-each select="msg_out:ChildProducts">
                    <msg_out:ChildProducts>
                        <msg_out:ProductObj>
                            <xsl:value-of select="msg_out:ProductObj"/>
                        </msg_out:ProductObj>
                        <msg_out:Amount>
                            <xsl:value-of select="msg_out:Amount"/>
                        </msg_out:Amount>
                        <msg_out:Tax>
                            <xsl:value-of select="msg_out:Tax"/>
                        </msg_out:Tax>
                    </msg_out:ChildProducts>
                 </xsl:for-each>
            </msg_out:ChildAccount>
         </xsl:for-each>
    </msg_out:PreAuthAnet>
</xsl:template>

</xsl:stylesheet>

或者,更优雅一点:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msg_out="http://www.portal.com/schemas/CRMSync">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/msg_out:PreAuthAnet">
    <xsl:variable name="common">
        <msg_out:ParentAccountObj>
            <xsl:value-of select="msg_out:AccountObj"/>
        </msg_out:ParentAccountObj>
        <msg_out:Status>
            <xsl:value-of select="msg_out:Status"/>
        </msg_out:Status>
        <msg_out:ReasonCode>
            <xsl:value-of select="msg_out:ReasonCode"/>
        </msg_out:ReasonCode>
        <msg_out:Days>
            <xsl:value-of select="msg_out:Days"/>
        </msg_out:Days>
        <msg_out:PayType>
            <xsl:value-of select="msg_out:PayType"/>
        </msg_out:PayType>
    </xsl:variable>
    <msg_out:PreAuthAnet>
        <xsl:for-each select="msg_out:ChildAccount">
            <msg_out:ChildAccount>
                <msg_out:ChildAccountObj>
                    <xsl:value-of select="msg_out:AccountObj"/>
                </msg_out:ChildAccountObj>
                <msg_out:VIN>
                    <xsl:value-of select="msg_out:Vin"/>
                </msg_out:VIN>
                <xsl:copy-of select="$common"/>
                <xsl:for-each select="msg_out:ChildProducts">
                    <msg_out:ChildProducts>
                        <msg_out:ProductObj>
                            <xsl:value-of select="msg_out:ProductObj"/>
                        </msg_out:ProductObj>
                        <msg_out:Amount>
                            <xsl:value-of select="msg_out:Amount"/>
                        </msg_out:Amount>
                        <msg_out:Tax>
                            <xsl:value-of select="msg_out:Tax"/>
                        </msg_out:Tax>
                    </msg_out:ChildProducts>
                 </xsl:for-each>
            </msg_out:ChildAccount>
         </xsl:for-each>
    </msg_out:PreAuthAnet>
</xsl:template>

</xsl:stylesheet>

注意

当你这样做时:

<xsl:for-each select="/msg_out:PreAuthAnet/msg_out:ChildAccount/msg_out:ChildProducts">

您从根目录开始,在整个输入文档中选择所有 ChildProducts个元素。