2013-01-12 139 views
-1

我想使用xslt讀取輸入xml的內容並生成輸出xml。 我能夠使用xslt生成輸出xml,但無法將輸入xml中的數據複製到生成的輸出xml。現在我直接硬編碼了xsl文件中的值。需要幫助使用XSLT進行XML到XML轉換

下面是輸入xml和預期輸出xml以及XSL。

需要幫助來修改在xsl從輸入XML讀取元件&屬性數據,並把它們在corrosponding所生成的輸出XML

輸入XML

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
<soapenv:Body> 
<ns0:Envelope xmlns:ns0="urn:test.com:xi:testsoap2soap"> 
<Header> 
    <m_Header mustUnderstand="0"> 
    <m_SessionID>SessionID1</m_SessionID> 
    </m_Header> 
</Header> 
<Body> 
    <Generate> 
    <iPolicyID>473441</iPolicyID> 
    <sNoticeTypeID>C</sNoticeTypeID> 
    <iAdvDays>0</iAdvDays> 
    <PolicyQuestions arrayType="npts:wspPolicyQuestion[0]" nil="true" offset="[0]" type="SOAP-ENC:Array"> 
     <wspPolicyQuestion> 
      <iQuestionID/> 
      <mQuestion/> 
      <sStateID/> 
      <mAnswer/> 
      <BasePolicyType/> 
      <SysPolicyType/> 
     </wspPolicyQuestion> 
    </PolicyQuestions> 
    <sReasonID>myReason</sReasonID> 
    <mReasonText>myreasontext</mReasonText> 
    <Agencies arrayType="npts:wspAgency[0]" nil="true" offset="[0]" type="SOAP-ENC:Array"> 
     <wspAgency> 
      <iAgencyID/> 
      <Address/> 
      <sName1/> 
      <sAgen800/> 
      <sAgencyRefNbr/> 
     </wspAgency> 
    </Agencies> 
    <iUnderwriterID>0</iUnderwriterID> 
    <iFaxID>7284</iFaxID> 
    <PopupQuestions arrayType="npts:wspPopupQuestion[0]" nil="true" offset="[0]" type="SOAP-ENC:Array"> 
     <wspPopupQuestion> 
      <iPopUpID/> 
      <mText/> 
      <cVarType/> 
      <sVarLen/> 
      <sReasonID/> 
      <mAnswer/> 
      <ISNID/> 
      <BasePolicyType/> 
     </wspPopupQuestion> 
    </PopupQuestions> 
    <ExtraNotices arrayType="npts:wspExtraNotice[0]" nil="true" offset="[0]" type="SOAP-ENC:Array"> 
     <wspExtraNotice> 
      <iExtraNoticeID/> 
      <iAddressID/> 
      <sName1/> 
      <sName2/> 
      <mText/> 
     </wspExtraNotice> 
    </ExtraNotices> 
    <dtMail>2013-01-08</dtMail> 
    <iMailLeadDays>0</iMailLeadDays> 
    <dtEffective>2013-01-25</dtEffective> 
    <iMailingTypeID>77</iMailingTypeID> 
    <bDateAutoCalc>false</bDateAutoCalc> 
    <sAsOfPhrase/> 
    <sFreeformTitle/> 
    <mFreeformText/> 
    <FreeformReceipients>0</FreeformReceipients> 
    <RemittanceSlip arrayType="npts:wspRemittanceSlip[5]" nil="true" offset="[0]" type="SOAP-ENC:Array"> 
     <wspRemittanceSlip> 
      <FieldName>DocType</FieldName> 
      <FieldValue>7</FieldValue> 
     </wspRemittanceSlip> 
     <wspRemittanceSlip> 
      <FieldName>ScanLine</FieldName> 
      <FieldValue>027 0018949170X013091774 01012013 7 00000002559600 1</FieldValue> 
     </wspRemittanceSlip> 
     <wspRemittanceSlip> 
      <FieldName>BPNumber</FieldName> 
      <FieldValue>1009738961</FieldValue> 
     </wspRemittanceSlip> 
     <wspRemittanceSlip> 
      <FieldName>RefNumber</FieldName> 
      <FieldValue>00018949170X01309177</FieldValue> 
     </wspRemittanceSlip> 
     <wspRemittanceSlip> 
      <FieldName>CouponName</FieldName> 
      <FieldValue>ABC Company</FieldValue> 
     </wspRemittanceSlip> 
    </RemittanceSlip> 
    <sRemittanceSlipLayout>remit_slip_bpr.txt</sRemittanceSlipLayout> 
    <sRemittanceSlipTemplate>remit_slip_bpr.pdf</sRemittanceSlipTemplate> 
    <PrintAction>32</PrintAction> 
    <CopyOptions>0</CopyOptions> 
    <CCListOptions>0</CCListOptions> 
    </Generate> 
</Body> 
</ns0:Envelope> 
</soapenv:Body> 
</soapenv:Envelope> 

輸出XML的elemwnts /屬性

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
<SOAP-ENV:Header> 
    <npts:m_Header xmlns:npts="urn:TestService" SOAP-ENV:mustUnderstand="0"> 
     <m_SessionID>SESSIONID1</m_SessionID> 
    </npts:m_Header> 
</SOAP-ENV:Header> 
<SOAP-ENV:Body> 
    <npts:Generate xmlns:npts="urn:TestService"> 
     <iPolicyID>473441</iPolicyID> 
     <sNoticeTypeID>C</sNoticeTypeID> 
     <iAdvDays>0</iAdvDays> 
     <npts:PolicyQuestions xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-SCH="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENC:arrayType="npts:wspPolicyQuestion[0]" SOAP-SCH:nil="true" SOAP-ENC:offset="[0]" SOAP-SCH:type="SOAP-ENC:Array"/> 
     <sReasonID>myreason</sReasonID> 
     <mReasonText>myreasontext</mReasonText> 
     <npts:Agencies xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-SCH="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENC:arrayType="npts:wspAgency[0]" SOAP-SCH:nil="true" SOAP-ENC:offset="[0]" SOAP-SCH:type="SOAP-ENC:Array"/> 
     <iUnderwriterID>0</iUnderwriterID> 
     <iFaxID>728477</iFaxID> 
     <npts:PopupQuestions xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-SCH="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENC:arrayType="npts:wspPopupQuestion[0]" SOAP-SCH:nil="true" SOAP-ENC:offset="[0]" SOAP-SCH:type="SOAP-ENC:Array"/> 
     <npts:ExtraNotices xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-SCH="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENC:arrayType="npts:wspExtraNotice[0]" SOAP-SCH:nil="true" SOAP-ENC:offset="[0]" SOAP-SCH:type="SOAP-ENC:Array"/> 
     <dtMail>2013-01-08</dtMail> 
     <iMailLeadDays>0</iMailLeadDays> 
     <dtEffective>2013-01-25</dtEffective> 
     <iMailingTypeID>77</iMailingTypeID> 
     <bDateAutoCalc>false</bDateAutoCalc> 
     <sAsOfPhrase/> 
     <sFreeformTitle/> 
     <mFreeformText/> 
     <FreeformReceipients>0</FreeformReceipients> 
     <npts:RemittanceSlip xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-SCH="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENC:arrayType="npts:wspRemittanceSlip[5]" SOAP-ENC:offset="[0]" SOAP-SCH:type="SOAP-ENC:Array"> 
      <wspRemittanceSlip> 
       <FieldName>DocType</FieldName> 
       <FieldValue>7</FieldValue> 
      </wspRemittanceSlip> 
      <wspRemittanceSlip> 
       <FieldName>ScanLine</FieldName> 
       <FieldValue>027 0018949170X013091774 01012013 7 00000002559600 1</FieldValue> 
      </wspRemittanceSlip> 
      <wspRemittanceSlip> 
       <FieldName>BPNumber</FieldName> 
       <FieldValue>1009738961</FieldValue> 
      </wspRemittanceSlip> 
      <wspRemittanceSlip> 
       <FieldName>RefNumber</FieldName> 
       <FieldValue>00018949170X01309177</FieldValue> 
      </wspRemittanceSlip> 
      <wspRemittanceSlip> 
       <FieldName>CouponName</FieldName> 
       <FieldValue>ABC Company</FieldValue> 
      </wspRemittanceSlip> 
     </npts:RemittanceSlip> 
     <sRemittanceSlipLayout>remit_slip_bpr.txt</sRemittanceSlipLayout> 
     <sRemittanceSlipTemplate>remit_slip_bpr.pdf</sRemittanceSlipTemplate> 
     <PrintAction>32</PrintAction> 
     <CopyOptions>0</CopyOptions> 
     <CCListOptions>0</CCListOptions> 
    </npts:Generate> 
</SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

XSL用來獲取XML輸出

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
          xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope" 
          xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
          xmlns:npts="urn:TestService" 
          xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
          xmlns:SOAP-SCH="http://www.w3.org/2001/XMLSchema-instance" 
          xmlns:ns0="urn:test.com:xi:testsoap2soap"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
    <xsl:template match="/"> 
<xsl:element name="SOAP-ENV:Envelope"> 
<xsl:element name="SOAP-ENV:Header"> 
<xsl:element name="npts:m_Header"> 
<xsl:attribute name="SOAP-ENV:mustUnderstand"><xsl:value-of select="m_Header/@mustUnderstand"/></xsl:attribute> 
<xsl:element name="m_SessionID">SESSIONID1</xsl:element> 
</xsl:element> 
</xsl:element> 
<xsl:element name="SOAP-ENV:Body"> 
<xsl:element name="npts:Generate"> 
<xsl:element name="iPolicyID">473441</xsl:element> 
<xsl:element name="sNoticeTypeID">C</xsl:element> 
<xsl:element name="iAdvDays">0</xsl:element> 
<xsl:element name="npts:PolicyQuestions"> 
<xsl:attribute name="SOAP-ENC:arrayType">npts:wspPolicyQuestion[0]</xsl:attribute> 
<xsl:attribute name="SOAP-SCH:nil">true</xsl:attribute> 
<xsl:attribute name="SOAP-ENC:offset">[0]</xsl:attribute> 
<xsl:attribute name="SOAP-SCH:type">SOAP-ENC:Array</xsl:attribute> 
</xsl:element> 
<xsl:element name="sReasonID">myreason</xsl:element> 
<xsl:element name="mReasonText">myreasontext</xsl:element> 
<xsl:element name="npts:Agencies"> 
<xsl:attribute name="SOAP-ENC:arrayType">npts:wspAgency[0]</xsl:attribute> 
<xsl:attribute name="SOAP-SCH:nil">true</xsl:attribute> 
<xsl:attribute name="SOAP-ENC:offset">[0]</xsl:attribute> 
<xsl:attribute name="SOAP-SCH:type">SOAP-ENC:Array</xsl:attribute> 
</xsl:element> 
<xsl:element name="iUnderwriterID">0</xsl:element> 
<xsl:element name="iFaxID">728477</xsl:element> 
<xsl:element name="npts:PopupQuestions"> 
<xsl:attribute name="SOAP-ENC:arrayType">npts:wspPopupQuestion[0]</xsl:attribute> 
<xsl:attribute name="SOAP-SCH:nil">true</xsl:attribute> 
<xsl:attribute name="SOAP-ENC:offset">[0]</xsl:attribute> 
<xsl:attribute name="SOAP-SCH:type">SOAP-ENC:Array</xsl:attribute> 
</xsl:element> 
<xsl:element name="npts:ExtraNotices"> 
<xsl:attribute name="SOAP-ENC:arrayType">npts:wspExtraNotice[0]</xsl:attribute> 
<xsl:attribute name="SOAP-SCH:nil">true</xsl:attribute> 
<xsl:attribute name="SOAP-ENC:offset">[0]</xsl:attribute> 
<xsl:attribute name="SOAP-SCH:type">SOAP-ENC:Array</xsl:attribute> 
</xsl:element> 
<xsl:element name="dtMail">2013-01-08</xsl:element> 
<xsl:element name="iMailLeadDays">0</xsl:element> 
<xsl:element name="dtEffective">2013-01-25</xsl:element> 
<xsl:element name="iMailingTypeID">77</xsl:element> 
<xsl:element name="bDateAutoCalc">false</xsl:element> 
<xsl:element name="sAsOfPhrase"></xsl:element> 
<xsl:element name="sFreeformTitle"></xsl:element> 
<xsl:element name="mFreeformText"></xsl:element> 
<xsl:element name="FreeformReceipients">0</xsl:element> 
<xsl:element name="npts:RemittanceSlip"> 
<xsl:attribute name="SOAP-ENC:arrayType">npts:wspRemittanceSlip[5]</xsl:attribute> 
<xsl:attribute name="SOAP-ENC:offset">[0]</xsl:attribute> 
<xsl:attribute name="SOAP-SCH:type">SOAP-ENC:Array</xsl:attribute> 
<xsl:element name="wspRemittanceSlip"> 
<xsl:element name="FieldName">DocType</xsl:element> 
<xsl:element name="FieldValue">7</xsl:element> 
</xsl:element> 
<xsl:element name="wspRemittanceSlip"> 
<xsl:element name="FieldName">ScanLine</xsl:element> 
<xsl:element name="FieldValue">027 0018949170X013091774 01012013 7 00000002559600 1</xsl:element> 
</xsl:element> 
<xsl:element name="wspRemittanceSlip"> 
<xsl:element name="FieldName">BPNumber</xsl:element> 
<xsl:element name="FieldValue">1009738961</xsl:element> 
</xsl:element> 
<xsl:element name="wspRemittanceSlip"> 
<xsl:element name="FieldName">RefNumber</xsl:element> 
<xsl:element name="FieldValue">00018949170X01309177</xsl:element> 
</xsl:element> 
<xsl:element name="wspRemittanceSlip"> 
<xsl:element name="FieldName">CouponName</xsl:element> 
<xsl:element name="FieldValue">ABC Company</xsl:element> 
</xsl:element> 
</xsl:element> 
<xsl:element name="sRemittanceSlipLayout">remit_slip_bpr.txt</xsl:element> 
<xsl:element name="sRemittanceSlipTemplate">remit_slip_bpr.pdf</xsl:element> 
<xsl:element name="PrintAction">32</xsl:element> 
<xsl:element name="CopyOptions">0</xsl:element> 
<xsl:element name="CCListOptions">0</xsl:element> 
</xsl:element> 
</xsl:element> 
</xsl:element> 
</xsl:template> 
</xsl:stylesheet> 

`

+0

你真的想從'soapenv'命名空間前綴改爲'SOAP-ENV'? –

回答

1

,而不是一個模板匹配「/」和手動創建的輸出,使用identity transform,然後只覆蓋你需要什麼。另外,除非您要動態創建名稱/前綴,否則您將不需要使用xsl:element/xsl:attribute,以供將來參考。大多數情況下,您可以直接將元素/屬性寫入樣式表中。

有太多的分析,試圖找出你的輸入/輸出之間的差異,所以這裏是一個例子,應該讓你通過npts:Generate。以此爲基礎,您應該可以自行完成。如果遇到問題,請回過頭來回答一些具體問題,我相信有人會提供幫助。

XSLT 1.0

<xsl:stylesheet version="1.0" 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns0="urn:test.com:xi:testsoap2soap" 
    xmlns:npts="urn:TestService" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="soapenv:*"> 
     <xsl:element name="SOAP-ENV:{local-name()}"> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="*[namespace-uri()='']"> 
     <xsl:element name="{name()}"> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="soapenv:Envelope|soapenv:Body"> 
     <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="ns0:Envelope"> 
     <xsl:element name="SOAP-ENV:{local-name()}"> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="Header|Body" priority="1"> 
     <xsl:element name="SOAP-ENV:{local-name()}"> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="m_Header|Generate" priority="1"> 
     <xsl:element name="npts:{local-name()}"> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="m_Header/@mustUnderstand"> 
     <xsl:attribute name="SOAP-ENV:mustUnderstand"> 
      <xsl:value-of select="."/> 
     </xsl:attribute> 
    </xsl:template> 

</xsl:stylesheet> 

輸出(通過ntps:Generate

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
    <SOAP-ENV:Header> 
     <npts:m_Header xmlns:npts="urn:TestService" SOAP-ENV:mustUnderstand="0"> 
      <m_SessionID>SessionID1</m_SessionID> 
     </npts:m_Header> 
    </SOAP-ENV:Header> 
    <SOAP-ENV:Body> 
     <npts:Generate xmlns:npts="urn:TestService"> 
      <!--Any decendants from this point 
      are unchanged.--> 
      <iPolicyID>473441</iPolicyID> 
      <sNoticeTypeID>C</sNoticeTypeID> 
      <iAdvDays>0</iAdvDays> 
      <PolicyQuestions arrayType="npts:wspPolicyQuestion[0]" 
       nil="true" 
       offset="[0]" 
       type="SOAP-ENC:Array"> 
       <wspPolicyQuestion> 
        <iQuestionID/> 
        <mQuestion/> 
        <sStateID/> 
        <mAnswer/> 
        <BasePolicyType/> 
        <SysPolicyType/> 
       </wspPolicyQuestion> 
      </PolicyQuestions> 
      <sReasonID>myReason</sReasonID> 
      <mReasonText>myreasontext</mReasonText> 
      <Agencies arrayType="npts:wspAgency[0]" 
       nil="true" 
       offset="[0]" 
       type="SOAP-ENC:Array"> 
       <wspAgency> 
        <iAgencyID/> 
        <Address/> 
        <sName1/> 
        <sAgen800/> 
        <sAgencyRefNbr/> 
       </wspAgency> 
      </Agencies> 
      <iUnderwriterID>0</iUnderwriterID> 
      <iFaxID>7284</iFaxID> 
      <PopupQuestions arrayType="npts:wspPopupQuestion[0]" 
       nil="true" 
       offset="[0]" 
       type="SOAP-ENC:Array"> 
       <wspPopupQuestion> 
        <iPopUpID/> 
        <mText/> 
        <cVarType/> 
        <sVarLen/> 
        <sReasonID/> 
        <mAnswer/> 
        <ISNID/> 
        <BasePolicyType/> 
       </wspPopupQuestion> 
      </PopupQuestions> 
      <ExtraNotices arrayType="npts:wspExtraNotice[0]" 
       nil="true" 
       offset="[0]" 
       type="SOAP-ENC:Array"> 
       <wspExtraNotice> 
        <iExtraNoticeID/> 
        <iAddressID/> 
        <sName1/> 
        <sName2/> 
        <mText/> 
       </wspExtraNotice> 
      </ExtraNotices> 
      <dtMail>2013-01-08</dtMail> 
      <iMailLeadDays>0</iMailLeadDays> 
      <dtEffective>2013-01-25</dtEffective> 
      <iMailingTypeID>77</iMailingTypeID> 
      <bDateAutoCalc>false</bDateAutoCalc> 
      <sAsOfPhrase/> 
      <sFreeformTitle/> 
      <mFreeformText/> 
      <FreeformReceipients>0</FreeformReceipients> 
      <RemittanceSlip arrayType="npts:wspRemittanceSlip[5]" 
       nil="true" 
       offset="[0]" 
       type="SOAP-ENC:Array"> 
       <wspRemittanceSlip> 
        <FieldName>DocType</FieldName> 
        <FieldValue>7</FieldValue> 
       </wspRemittanceSlip> 
       <wspRemittanceSlip> 
        <FieldName>ScanLine</FieldName> 
        <FieldValue>027 0018949170X013091774 01012013 7 00000002559600 1</FieldValue> 
       </wspRemittanceSlip> 
       <wspRemittanceSlip> 
        <FieldName>BPNumber</FieldName> 
        <FieldValue>1009738961</FieldValue> 
       </wspRemittanceSlip> 
       <wspRemittanceSlip> 
        <FieldName>RefNumber</FieldName> 
        <FieldValue>00018949170X01309177</FieldValue> 
       </wspRemittanceSlip> 
       <wspRemittanceSlip> 
        <FieldName>CouponName</FieldName> 
        <FieldValue>ABC Company</FieldValue> 
       </wspRemittanceSlip> 
      </RemittanceSlip> 
      <sRemittanceSlipLayout>remit_slip_bpr.txt</sRemittanceSlipLayout> 
      <sRemittanceSlipTemplate>remit_slip_bpr.pdf</sRemittanceSlipTemplate> 
      <PrintAction>32</PrintAction> 
      <CopyOptions>0</CopyOptions> 
      <CCListOptions>0</CCListOptions> 
     </npts:Generate> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 
+0

感謝丹尼爾,將進一步處理您提供的意見,並在出現任何問題時回到論壇。 – user1971595