本文介绍了对于XSLT中的每个循环,仅通过第一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将XML转换为以下XML:

Trying to convert XML to CSV for the XML Below:

<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
   <Header>
      <MessageId>{70BF3A9B-9111-48D8-93B4-C6232E74307F}</MessageId>
      <Action>http://tempuri.org/example/find</Action>
   </Header>
   <Body>
      <MessageParts>
         <Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <pain.001.001.02>
               <GrpHdr>
                  <MsgId>AB01029407</MsgId>
                  <CreDtTm>2020-05-07T11:23:08</CreDtTm>
                  <NbOfTxs>2</NbOfTxs>
                  <CtrlSum>4598</CtrlSum>
                  <Grpg>MIXD</Grpg>
                  <InitgPty>
                     <Nm>MY COMPANY Ltd1</Nm>
                     <Id>
                        <OrgId>
                           <TaxIdNb>GB 823825133</TaxIdNb>
                        </OrgId>
                     </Id>
                  </InitgPty>
               </GrpHdr>
               <PmtInf>
                  <PmtInfId>AB01029407</PmtInfId>
                  <PmtMtd>TRF</PmtMtd>
                  <PmtTpInf>
                     <SvcLvl>
                        <Cd>SEPA</Cd>
                     </SvcLvl>
                  </PmtTpInf>
                  <Dbtr>
                     <Nm>MY COMPANY Ltd</Nm>
                     <PstlAdr>
                        <AdrLine>Address Line 1</AdrLine>
                        <AdrLine>Address Line 2</AdrLine>
                        <Ctry>CB</Ctry>
                     </PstlAdr>
                  </Dbtr>
                  <DbtrAcct>
                     <Id>
                        <IBAN>98</IBAN>
                     </Id>
                  </DbtrAcct>
                  <DbtrAgt>
                     <FinInstnId>
                        <BIC>ABC123</BIC>
                     </FinInstnId>
                  </DbtrAgt>
                  <ChrgBr>SLEV</ChrgBr>
                  <CdtTrfTxInf>
                     <PmtId>
                        <EndToEndId>Not-Provided</EndToEndId>
                     </PmtId>
                     <Amt>
                        <InstdAmt Ccy="CAD">2198.00</InstdAmt>
                     </Amt>
                     <CdtrAgt>
                        <FinInstnId>
                           <BIC>SWIFT01</BIC>
                        </FinInstnId>
                     </CdtrAgt>
                     <Cdtr>
                        <Nm>Creditor Name</Nm>
                        <PstlAdr>
                           <AdrLine>tests</AdrLine>
                           <AdrLine>Chicago</AdrLine>
                           <Ctry>US</Ctry>
                        </PstlAdr>
                     </Cdtr>
                     <CdtrAcct>
                        <Id>
                           <IBAN>98</IBAN>
                        </Id>
                     </CdtrAcct>
                     <RmtInf>
                        <Ustrd>1345</Ustrd>
                     </RmtInf>
                  </CdtTrfTxInf>
                  <CdtTrfTxInf>
                     <PmtId>
                        <EndToEndId>Not-Provided</EndToEndId>
                     </PmtId>
                     <Amt>
                        <InstdAmt Ccy="EUR">2400.00</InstdAmt>
                     </Amt>
                     <CdtrAgt>
                        <FinInstnId>
                           <BIC>SWIFT01</BIC>
                        </FinInstnId>
                     </CdtrAgt>
                     <Cdtr>
                        <Nm>Creditor Name1</Nm>
                        <PstlAdr>
                           <AdrLine>tests</AdrLine>
                           <AdrLine>Chicago</AdrLine>
                           <Ctry>US</Ctry>
                        </PstlAdr>
                     </Cdtr>
                     <CdtrAcct>
                        <Id>
                           <IBAN>98</IBAN>
                        </Id>
                     </CdtrAcct>
                     <RmtInf>
                        <Ustrd>123456765</Ustrd>
                     </RmtInf>
                  </CdtTrfTxInf>
               </PmtInf>
            </pain.001.001.02>
         </Document>
      </MessageParts>
   </Body>
</Envelope>

我使用的XSLT是:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://schemas.microsoft.com/dynamics/2011/01/documents/Message"
xmlns:ns2="urn:iso:std:iso:20022:tech:xsd:pain.001.001.02">
<xsl:output method="text"/>
<xsl:template match="/ns1:Envelope">
<xsl:variable name="Delimiter" select="','"></xsl:variable>
<xsl:variable name="header" select="ns1:Body/ns1:MessageParts/ns2:Document/ns2:pain.001.001.02/ns2:GrpHdr" />
<xsl:variable name="pmt" select="ns1:Body/ns1:MessageParts/ns2:Document/ns2:pain.001.001.02/ns2:PmtInf" />
<xsl:variable name="headerText"
select="concat($header/ns2:MsgId,',',
$header/ns2:CreDtTm,',',
$header/ns2:NbOfTxs,',',
$header/ns2:CtrlSum,',',
$header/ns2:Grpg,',',
$header/ns2:InitgPty/ns2:Nm,',',
$header/ns2:InitgPty/ns2:Id/ns2:OrgId/ns2:TaxIdNb)"/>
<xsl:variable name="pmtText"
select="concat($pmt/ns2:PmtInfId,',',
$pmt/ns2:PmtMtd,',',
$pmt/ns2:PmtTpInf/ns2:SvcLvl/ns2:Cd,',',
$pmt/ns2:Dbtr/ns2:Nm,',',
$pmt/ns2:Dbtr/ns2:PstlAdr/ns2:AdrLine[1],',',
$pmt/ns2:Dbtr/ns2:PstlAdr/ns2:AdrLine[2],',',
$pmt/ns2:Dbtr/ns2:PstlAdr/ns2:Ctry,',',
$pmt/ns2:DbtrAcct/ns2:Id/ns2:IBAN,',',
$pmt/ns2:DbtrAgt/ns2:FinInstnId/ns2:BIC,',',
$pmt/ns2:ChrgBr)"/>
<xsl:variable name="CdtTrfTxInfText"
select="concat($pmt/ns2:CdtTrfTxInf/ns2:PmtId/ns2:EndToEndId,',',
$pmt/ns2:CdtTrfTxInf/ns2:Amt/ns2:InstdAmt,',',
$pmt/ns2:CdtTrfTxInf/ns2:CdtrAgt/ns2:FinInstnId/ns2:BIC,',',
$pmt/ns2:CdtTrfTxInf/ns2:Cdtr/ns2:Nm,',',
$pmt/ns2:CdtTrfTxInf/ns2:Cdtr/ns2:PstlAdr/ns2:AdrLine[1],',',
$pmt/ns2:CdtTrfTxInf/ns2:Cdtr/ns2:PstlAdr/ns2:AdrLine[2],',',
$pmt/ns2:CdtTrfTxInf/ns2:Cdtr/ns2:PstlAdr/ns2:Ctry,',',
$pmt/ns2:CdtTrfTxInf/ns2:CdtrAcct/ns2:Id/ns2:IBAN,',',
$pmt/ns2:CdtTrfTxInf/ns2:RmtInf/ns2:Ustrd)"/>
<xsl:for-each select="$pmt/ns2:CdtTrfTxInf">
<xsl:value-of select="$headerText"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="$pmtText"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="$CdtTrfTxInfText"/>
<xsl:text>&#xa;</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

我得到的输出如下:

AB01029407,2020-05-07T11:23:08,2,4598,MIXD,MY COMPANY Ltd1,GB 823825133,AB01029407,TRF,SEPA,MY COMPANY Ltd,地址行1,地址行2 ,CB,98,ABC123,SLEV,未提供,2198.00,SWIFT01,证书名称,测试,芝加哥,美国,98,1345

AB01029407,2020-05-07T11:23:08,2,4598,MIXD,MY COMPANY Ltd1,GB 823825133,AB01029407,TRF,SEPA,MY COMPANY Ltd,Address Line 1,Address Line 2,CB,98,ABC123,SLEV,Not-Provided,2198.00,SWIFT01,Creditor Name,tests,Chicago,US,98,1345

AB01029407,2020-05- 07T11:23:08,2,4598,MIXD,MY COMPANY Ltd1,GB 823825133,AB01029407,TRF,SEPA,MY COMPANY Ltd,地址行1,地址行2,CB,98,ABC123,SLEV,未提供,2198.00 ,SWIFT01,证书名称,测试,芝加哥,美国,98,1345

AB01029407,2020-05-07T11:23:08,2,4598,MIXD,MY COMPANY Ltd1,GB 823825133,AB01029407,TRF,SEPA,MY COMPANY Ltd,Address Line 1,Address Line 2,CB,98,ABC123,SLEV,Not-Provided,2198.00,SWIFT01,Creditor Name,tests,Chicago,US,98,1345

所需输出为:

AB01029407,2020-05-07T11:23:08,2,4598,MIXD,MY COMPANY Ltd1,GB 823825133,AB01029407,TRF,SEPA,MY COMPANY Ltd,Address Line 1,Address Line 2,CB,98,ABC123,SLEV,Not-Provided,2198.00,SWIFT01,Creditor Name,tests,Chicago,US,98,1345
AB01029407,2020-05-07T11:23:08,2,4598,MIXD,MY COMPANY Ltd1,GB 823825133,AB01029407,TRF,SEPA,MY COMPANY Ltd,Address Line 1,Address Line 2,CB,98,ABC123,SLEV,Not-Provided,2400.00,SWIFT01,Creditor Name1,tests,Chicago,US,98,123456765

能否请您更正此错误,无法理解我在哪里做错了。

Could you please correct this, unable to understand where i am doing wrong.

谢谢

推荐答案

我发现这些无休止的 concat()语句不可读。我建议您将它们更改为以下形式的显式指令:

I find these endless concat() statements unreadable. I would suggest you change them to explicit instructions in the form of:

<xsl:variable name="headerText">
    <xsl:value-of select="$header/ns2:MsgId"/>
    <xsl:text>,</xsl:text>
    <!-- ... -->
</xsl:variable>

某些缩进也有助于提高可读性。

Some indentation would also be useful for improving readability.

无论如何,您的错误是在 xsl之外定义了 $ CdtTrfTxInfText 变量: -每条指令。这将使用第一个 CdtTrfTxInf 节点中的值填充变量-然后您只需为每次 CdtTrfTxInf 重复这些值

Anyway, your mistake is defining the $CdtTrfTxInfText variable outside of the xsl:for-each instruction. This populates the variable with values from the first CdtTrfTxInf node - and then you simply repeat these values for each occurrence of CdtTrfTxInf.

尝试摆脱变量,直接从 xsl:for-each 指令:

Try getting rid of the variable and getting the values directly from the current node within the xsl:for-each instruction:

<xsl:for-each select="$pmt/ns2:CdtTrfTxInf">
    <xsl:value-of select="$headerText"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="$pmtText"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="concat(
    ns2:PmtId/ns2:EndToEndId,',',
    ns2:Amt/ns2:InstdAmt,',',
    ns2:CdtrAgt/ns2:FinInstnId/ns2:BIC,',',
    ns2:Cdtr/ns2:Nm,',',
    ns2:Cdtr/ns2:PstlAdr/ns2:AdrLine[1],',',
    ns2:Cdtr/ns2:PstlAdr/ns2:AdrLine[2],',',
    ns2:Cdtr/ns2:PstlAdr/ns2:Ctry,',',
    ns2:CdtrAcct/ns2:Id/ns2:IBAN,',',
    ns2:RmtInf/ns2:Ustrd)"/>
    <xsl:text>&#xa;</xsl:text>
</xsl:for-each>

这篇关于对于XSLT中的每个循环,仅通过第一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:27