xsl不将换行输出到文本输出中而不重复全局数据

时间:2014-09-02 15:01:38

标签: xml xslt

我有外部提供商发送的xml数据。我们需要将其输出到固定宽度的文本文件,以便加载到另一个系统中。数据包括一个名为Stream全局元素,应该为每个Voucher元素输出。但它不是!

这是xml:

<VocaDocument xmlns:cmn="http://www.voca.com/schemas/common"  xmlns="http://www.voca.com/schemas/messaging" xmlns:iso="http://www.voca.com/schemas/common/iso" xmlns:env="http://www.voca.com/schemas/envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.voca.com/schemas/messaging http://www.voca.com/schemas/messaging/Voca_AUDDIS_AdviceofDDI_v1.0.xsd">
  <Data>
      <Document>
          <StreamStart>
              <Stream>
           <AgencyBankParameter>234</AgencyBankParameter>
          <BankName>LLOYDS BANK PLC</BankName>
          <BankCode>0004</BankCode>
          <AgencyBankName>BANK OF CYPRUS UK LTD</AgencyBankName>
          <AgencyBankCode>0234</AgencyBankCode>
          <StreamCode>01</StreamCode>
          <VoucherSortCode>SC300037</VoucherSortCode>
          <VoucherAccountNumber>46990760</VoucherAccountNumber>              
          </Stream>
          </StreamStart>
          <DDIVouchers>
              <Voucher>
                  <TransactionCode>NEW</TransactionCode> 
                  <OriginatorIdentification>
                      <ServiceUserNumber>123456</ServiceUserNumber> 
                  </OriginatorIdentification>
               </Voucher>              
               <Voucher>
                  <TransactionCode>OLD</TransactionCode> 
                  <OriginatorIdentification>
                      <ServiceUserNumber>789012</ServiceUserNumber> 
                  </OriginatorIdentification>
                  <ContactDetails>
            <PhoneNumber>020 83395862</PhoneNumber>
            <FaxNumber> FAX</FaxNumber>
              <Address>
                  <cmn:AddresseeName>RANALD LESLIE</cmn:AddresseeName>
                  <cmn:PostalName>NUFFIELD HEALTH </cmn:PostalName>
                  <cmn:AddressLine>NUFFIELD HOUSE</cmn:AddressLine>
                  <cmn:TownName>SURBITON</cmn:TownName>
                  <cmn:CountyIdentification> </cmn:CountyIdentification>
                  <cmn:CountryName>UNITED KINGDOM</cmn:CountryName>
                  <cmn:ZipCode>KT6 4BN</cmn:ZipCode>
                  </Address>
        </ContactDetails>
          <ProcessingDate>2014-08-19</ProcessingDate>
          <BankAccount><FirstLastVoucherCode>FirstLast</FirstLastVoucherCode><AgencyBankCode>0234</AgencyBankCode><SortCode>SC300037</SortCode><AccountNumber>46990760</AccountNumber><TotalVouchers>1</TotalVouchers></BankAccount>
               </Voucher>


          </DDIVouchers>
      </Document>
  </Data>
</VocaDocument>

我有以下xsl,我是在StackOverflow的帮助下构建的。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
xmlns:my="http://www.voca.com/schemas/messaging" 
xmlns:cmn="http://www.voca.com/schemas/common">
<xsl:output method="text" encoding="UTF-8" />

<xsl:template match="/my:MyRoot">
    <xsl:for-each select="VocaDocument/Data/Document/DDIVouchers/Voucher">
        <xsl:value-of select="../../StreamStart/Stream/AgencyBankParameter" />
        <xsl:value-of select="../../StreamStart/Stream/BankName" />

        <xsl:value-of select="ContactDetails/PhoneNumber" />
        <xsl:value-of select="ContactDetails/FaxNumber" />
        <xsl:value-of select="ContactDetails/Address/cmn:AddresseeName" />
        <xsl:value-of select="ContactDetails/Address/cmn:PostalName" />
        <xsl:value-of select="ContactDetails/Address/cmn:AddressLine" />
        <xsl:value-of select="ContactDetails/Address/cmn:TownName" />
        <xsl:value-of select="ContactDetails/Address/cmn:CountyIdentification" />
        <xsl:value-of select="ContactDetails/Address/cmn:CountryName" />
        <xsl:value-of select="ContactDetails/Address/cmn:ZipCode" />
        <xsl:value-of select="ProcessingDate" />

        <xsl:value-of select="BankAccount/SortCode" />
        <xsl:value-of select="BankAccount/AccountNumber" />
        <xsl:value-of select="BankAccount/TotalVouchers" />
        <xsl:text>&#xD;&#xA;</xsl:text>
    </xsl:for-each>
</xsl:template> 

</xsl:stylesheet>

我有两个问题: 1)Stream元素应该在输出的每一行的开头重复,而它们不是 - 我在开始时得到一个,然后只有<Voucher>个记录。 stamnge事情是没有额外的命名空间声明工作!

2)我想在每行末尾的texty文件中输入换行符/回车符,但它不起作用。我试过这个<xsl:text>&#xD;&#xA;</xsl:text>和这个:<xsl:text>&#10;</xsl:text>,但都没有。

1 个答案:

答案 0 :(得分:1)

通过这种方式尝试:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:msg="http://www.voca.com/schemas/messaging" 
xmlns:cmn="http://www.voca.com/schemas/common" >
<xsl:output method="text" encoding="UTF-8" />

<xsl:template match="/">
    <xsl:for-each select="msg:VocaDocument/msg:Data/msg:Document/msg:DDIVouchers/msg:Voucher">
        <xsl:value-of select="../../msg:StreamStart/msg:Stream/msg:AgencyBankParameter" />
        <xsl:value-of select="../../msg:StreamStart/msg:Stream/msg:BankName" />

        <xsl:value-of select="msg:BankAccount/msg:SortCode" />
        <xsl:value-of select="msg:BankAccount/msg:AccountNumber" />
        <xsl:value-of select="msg:BankAccount/msg:TotalVouchers" />

        <!-- NOTE HOW TO EXTRACT AN ADDRESS ELEMENT THAT HAS ITS OWN NAMESPACE -->
        <xsl:value-of select="msg:ContactDetails/msg:Address/cmn:AddresseeName" />

        <xsl:text>&#xD;&#xA;</xsl:text>
    </xsl:for-each>
</xsl:template> 

</xsl:stylesheet>

请注意,默认情况下,子元素会继承其父级的名称空间。