-1

我正在尝试使用 xslt 来读取输入 xml 的内容并生成输出 xml 。我能够使用 xslt 生成输出 xml,但无法将数据从输入 xml 复制到生成的输出 xml。现在我已经直接硬编码了 xsl 文件中的值。

下面是输入 xml 和预期的输出 xml 以及 XSL 。

需要帮助修改 xsl 以从输入 xml 中读取元素和属性数据,并将它们放入生成的输出 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

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

`

4

1 回答 1

1

不要使用一个匹配“/”的模板并手动创建输出,而是使用身份转换,然后只覆盖您需要的内容。

此外,为了将来参考,您不需要使用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>
于 2013-01-12T04:30:23.347 回答