复杂XSLT指南

时间:2013-02-20 18:13:02

标签: xml xslt xslt-1.0

我正在解析要转换为文本的XML并提交给FinCEN,后者要求它采用非常特定的格式。基本上,我将获得一个xml文件,其中元素都排列为兄弟,并且它们之间的关联基于文件中的外观顺序。所以我可能有一个看起来像这样的文件:

<bsar:BSAForm xmlns:bsar="http://www.fincen.gov/bsa/bsar/2011-06-01">
<bsar:FilingInstitutionInformation>
    <bsar:PrimaryRegulator>D</bsar:PrimaryRegulator>
    <bsar:FilerName>New Banking Corp.</bsar:FilerName>
    <bsar:TIN>373637383</bsar:TIN>
    <bsar:TINTYPE>A</bsar:TINTYPE>
    <bsar:FIType>
        <ucc:FinInsType xmlns:ucc="http://www.fincen.gov/bsa/ucommon-components/2011-06-01">C</ucc:FinInsType>
        <ucc:OtherSpecify xmlns:ucc="http://www.fincen.gov/bsa/ucommon-components/2011-06-01" />
    </bsar:FIType>
    <bsar:SecuritiesFuturesType>
        <bsar:ClearingBrokerSecurities />
        <bsar:CPOCTA />
        <bsar:FuturesCommissionMerchant />
        <bsar:HoldingCompany />
        <bsar:IntroducingCommodities />
        <bsar:IntroducingSecurities />
        <bsar:InvestmentAdvisor />
        <bsar:InvestmentCompany />
        <bsar:RetailForeign />
        <bsar:SROFutures />
        <bsar:SROSecurities />
        <bsar:Subsidiary />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:SecuritiesFuturesType>
    <bsar:FIIdentification>
        <ucc:FIIDType xmlns:ucc="http://www.fincen.gov/bsa/ucommon-components/2011-06-01">D</ucc:FIIDType>
        <ucc:FIIDNumber xmlns:ucc="http://www.fincen.gov/bsa/ucommon-components/2011-06-01">654646321321</ucc:FIIDNumber>
    </bsar:FIIdentification>
    <bsar:FIAddress>
        <ucc:Address xmlns:ucc="http://www.fincen.gov/bsa/ucommon-components/2011-06-01">Ave X</ucc:Address>
        <ucc:City xmlns:ucc="http://www.fincen.gov/bsa/ucommon-components/2011-06-01">Brooklyn</ucc:City>
        <ucc:State xmlns:ucc="http://www.fincen.gov/bsa/ucommon-components/2011-06-01">NY</ucc:State>
        <ucc:ZIP xmlns:ucc="http://www.fincen.gov/bsa/ucommon-components/2011-06-01">11235</ucc:ZIP>
        <ucc:Country xmlns:ucc="http://www.fincen.gov/bsa/ucommon-components/2011-06-01">US</ucc:Country>
    </bsar:FIAddress>
    <bsar:AlternateName>alternate name</bsar:AlternateName>
</bsar:FilingInstitutionInformation> <!--2A-->

<bsar:FinancialInstitutionInformation>
    <bsar:LegalName>Citibank</bsar:LegalName>
</bsar:FinancialInstitutionInformation> <!--2B-->
<bsar:SuspiciousActivityInformation>
    <bsar:FilingType>A</bsar:FilingType>
    <bsar:ContinuingActivity />
    <bsar:JointReport />
    <ucc:DocumentControlNumber xmlns:ucc="http://www.fincen.gov/bsa/ucommon-components/2011-06-01" />
    <bsar:ViolationAmount />
    <bsar:AmountUnKnownOrNotInvolved>B</bsar:AmountUnKnownOrNotInvolved>
    <bsar:FromSuspiciousActivityDate>12312012</bsar:FromSuspiciousActivityDate>
    <bsar:ToSuspiciousActivityDate>12312012</bsar:ToSuspiciousActivityDate>
    <bsar:CumulativeAmount />
    <bsar:Structuring>
        <bsar:AltersTransactionBSA>A</bsar:AltersTransactionBSA>
        <bsar:AltersTransactionCTR />
        <bsar:CustomerCancels />
        <bsar:MultipleTransactions />
        <bsar:MultipleTransactionsCTR />
        <bsar:SuspiciousInquiry />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:Structuring>
    <bsar:TerroristFinanacing>
        <bsar:KnownTerroristOrg />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:TerroristFinanacing>
    <bsar:Fraud>
        <bsar:ACH />
        <bsar:BusinessLoan />
        <bsar:Check />
        <bsar:ConsumerLoan />
        <bsar:CreditDebitCard />
        <bsar:HealthCare />
        <bsar:Mail />
        <bsar:MassMarketing />
        <bsar:PyramidScheme />
        <bsar:Wire />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:Fraud>
    <bsar:Casinos>
        <bsar:InquiryOnEOB />
        <bsar:MinimalGaming />
        <bsar:IntraCasinoFundTransfer />
        <bsar:CounterChecksorMarkers />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:Casinos>
    <bsar:MoneyLaundering>
        <bsar:ExchangeSmallBig />
        <bsar:PhysicalCondition />
        <bsar:SourceFund />
        <bsar:Designation />
        <bsar:EftOrWireTransfer />
        <bsar:ExchangeCurrency />
        <bsar:GovernmentPaymentReceipt />
        <bsar:MultipleAccounts />
        <bsar:NonCashMonetaryInstr />
        <bsar:StrawMan />
        <bsar:MoneyLaunderingBlackMarket />
        <bsar:OutOfPatterns />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:MoneyLaundering>
    <bsar:IdentificationDocumentation>
        <bsar:ChangeName />
        <bsar:MultiIndiWithSameSimilarName />
        <bsar:QuestionableOrfalseDoc />
        <bsar:RefusedDoc />
        <bsar:MultipleIdentities />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:IdentificationDocumentation>
    <bsar:OtherSuspicoiusActivities>
        <bsar:AccountTakeOver />
        <bsar:BriberyOrGratuity />
        <bsar:CounterfeitInstr />
        <bsar:ElderFinancial />
        <bsar:Embezzlement />
        <bsar:Forgeries />
        <bsar:IdentifyTheft />
        <bsar:LittleNoConcerns />
        <bsar:MisuseFreeLook />
        <bsar:MisusePosition />
        <bsar:SuspectedDomesticCorruption />
        <bsar:SuspectedForeignCorruption />
        <bsar:SuspiciousUseInformationValueSys />
        <bsar:SuspiciousUseMultiLoc />
        <bsar:NoPurpose />
        <bsar:TwoOrMoreWorkingTogether />
        <bsar:UnAuthorizedEInstrusion />
        <bsar:UnLicensedMSB />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:OtherSuspicoiusActivities>
    <bsar:Insurance>
        <bsar:ExcessiveInsurance>A</bsar:ExcessiveInsurance>
        <bsar:ExcessiveOrUnusalCash />
        <bsar:ProceedsFromUnrelated3rd />
        <bsar:LifeSettlement />
        <bsar:PolicyTermination />
        <bsar:UnclearNoInterest />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:Insurance>
    <bsar:SecuritiesFutureOptions>
        <bsar:InsiderTrading />
        <bsar:WashTrading />
        <bsar:Misappropriation />
        <bsar:UnauthorizedPooling />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:SecuritiesFutureOptions>
    <bsar:MortgageFraud>
        <bsar:Appraisal />
        <bsar:Foreclosure />
        <bsar:LoanModification />
        <bsar:ReverseMortgage />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:MortgageFraud>
    <bsar:Product>
        <bsar:BondsNotes>A</bsar:BondsNotes>
        <bsar:CommercialMtgg />
        <bsar:CommercialPaper />
        <bsar:CreditCard />
        <bsar:DebitCard />
        <bsar:ForexTrans />
        <bsar:FutureOptionsOnFu />
        <bsar:HedgeFund />
        <bsar:HomeEquityLoan />
        <bsar:HomeEquityLine />
        <bsar:InsuranceAnuity />
        <bsar:MutualFund />
        <bsar:OptionsOnSec />
        <bsar:PennyStocks />
        <bsar:PrepaidAccess />
        <bsar:ResidentialMtgg />
        <bsar:SecurityFuture />
        <bsar:Stocks />
        <bsar:SwapHybridDer />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:Product>
    <bsar:InstrumentOrPayment>
        <bsar:BankCheck />
        <bsar:ForeignCurrency />
        <bsar:FundsTransfer />
        <bsar:GamingInstrm />
        <bsar:GovernmentPayment />
        <bsar:MoneyOrders />
        <bsar:PersonalCheck />
        <bsar:TravelersCheck />
        <bsar:USCurrency />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:InstrumentOrPayment>
    <bsar:InternalControlNumber>654321654321</bsar:InternalControlNumber>
    <bsar:CommodityTypeRecord>
        <bsar:CommodityType>commodity type</bsar:CommodityType>
    </bsar:CommodityTypeRecord>
    <bsar:ProductInstrumentDescRecord>
        <bsar:ProductInstrumentDesc>product description</bsar:ProductInstrumentDesc>
    </bsar:ProductInstrumentDescRecord>
    <bsar:MarketTradedRecord>
        <bsar:MarketTraded>101</bsar:MarketTraded>
    </bsar:MarketTradedRecord>
    <bsar:IPAddressRecord>
        <bsar:IPAddress>255.255.255.255</bsar:IPAddress>
    </bsar:IPAddressRecord>
    <bsar:CUSIPNumberRecord>
        <bsar:CUSIPNumber>3465431654</bsar:CUSIPNumber>
    </bsar:CUSIPNumberRecord>
</bsar:SuspiciousActivityInformation> <!--3A-->

<bsar:FinancialInstitutionInformation>
    <bsar:LegalName>Bank of America</bsar:LegalName>
</bsar:FinancialInstitutionInformation> <!--2B-->
<bsar:SuspiciousActivityInformation>
    <bsar:FilingType>A</bsar:FilingType>
    <bsar:ContinuingActivity />
    <bsar:JointReport />
    <ucc:DocumentControlNumber xmlns:ucc="http://www.fincen.gov/bsa/ucommon-components/2011-06-01" />
    <bsar:ViolationAmount />
    <bsar:AmountUnKnownOrNotInvolved>B</bsar:AmountUnKnownOrNotInvolved>
    <bsar:FromSuspiciousActivityDate>12312012</bsar:FromSuspiciousActivityDate>
    <bsar:ToSuspiciousActivityDate>12312012</bsar:ToSuspiciousActivityDate>
    <bsar:CumulativeAmount />
    <bsar:Structuring>
        <bsar:AltersTransactionBSA>A</bsar:AltersTransactionBSA>
        <bsar:AltersTransactionCTR />
        <bsar:CustomerCancels />
        <bsar:MultipleTransactions />
        <bsar:MultipleTransactionsCTR />
        <bsar:SuspiciousInquiry />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:Structuring>
    <bsar:TerroristFinanacing>
        <bsar:KnownTerroristOrg />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:TerroristFinanacing>
    <bsar:Fraud>
        <bsar:ACH />
        <bsar:BusinessLoan />
        <bsar:Check />
        <bsar:ConsumerLoan />
        <bsar:CreditDebitCard />
        <bsar:HealthCare />
        <bsar:Mail />
        <bsar:MassMarketing />
        <bsar:PyramidScheme />
        <bsar:Wire />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:Fraud>
    <bsar:Casinos>
        <bsar:InquiryOnEOB />
        <bsar:MinimalGaming />
        <bsar:IntraCasinoFundTransfer />
        <bsar:CounterChecksorMarkers />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:Casinos>
    <bsar:MoneyLaundering>
        <bsar:ExchangeSmallBig />
        <bsar:PhysicalCondition />
        <bsar:SourceFund />
        <bsar:Designation />
        <bsar:EftOrWireTransfer />
        <bsar:ExchangeCurrency />
        <bsar:GovernmentPaymentReceipt />
        <bsar:MultipleAccounts />
        <bsar:NonCashMonetaryInstr />
        <bsar:StrawMan />
        <bsar:MoneyLaunderingBlackMarket />
        <bsar:OutOfPatterns />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:MoneyLaundering>
    <bsar:IdentificationDocumentation>
        <bsar:ChangeName />
        <bsar:MultiIndiWithSameSimilarName />
        <bsar:QuestionableOrfalseDoc />
        <bsar:RefusedDoc />
        <bsar:MultipleIdentities />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:IdentificationDocumentation>
    <bsar:OtherSuspicoiusActivities>
        <bsar:AccountTakeOver />
        <bsar:BriberyOrGratuity />
        <bsar:CounterfeitInstr />
        <bsar:ElderFinancial />
        <bsar:Embezzlement />
        <bsar:Forgeries />
        <bsar:IdentifyTheft />
        <bsar:LittleNoConcerns />
        <bsar:MisuseFreeLook />
        <bsar:MisusePosition />
        <bsar:SuspectedDomesticCorruption />
        <bsar:SuspectedForeignCorruption />
        <bsar:SuspiciousUseInformationValueSys />
        <bsar:SuspiciousUseMultiLoc />
        <bsar:NoPurpose />
        <bsar:TwoOrMoreWorkingTogether />
        <bsar:UnAuthorizedEInstrusion />
        <bsar:UnLicensedMSB />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:OtherSuspicoiusActivities>
    <bsar:Insurance>
        <bsar:ExcessiveInsurance>A</bsar:ExcessiveInsurance>
        <bsar:ExcessiveOrUnusalCash />
        <bsar:ProceedsFromUnrelated3rd />
        <bsar:LifeSettlement />
        <bsar:PolicyTermination />
        <bsar:UnclearNoInterest />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:Insurance>
    <bsar:SecuritiesFutureOptions>
        <bsar:InsiderTrading />
        <bsar:WashTrading />
        <bsar:Misappropriation />
        <bsar:UnauthorizedPooling />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:SecuritiesFutureOptions>
    <bsar:MortgageFraud>
        <bsar:Appraisal />
        <bsar:Foreclosure />
        <bsar:LoanModification />
        <bsar:ReverseMortgage />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:MortgageFraud>
    <bsar:Product>
        <bsar:BondsNotes>A</bsar:BondsNotes>
        <bsar:CommercialMtgg />
        <bsar:CommercialPaper />
        <bsar:CreditCard />
        <bsar:DebitCard />
        <bsar:ForexTrans />
        <bsar:FutureOptionsOnFu />
        <bsar:HedgeFund />
        <bsar:HomeEquityLoan />
        <bsar:HomeEquityLine />
        <bsar:InsuranceAnuity />
        <bsar:MutualFund />
        <bsar:OptionsOnSec />
        <bsar:PennyStocks />
        <bsar:PrepaidAccess />
        <bsar:ResidentialMtgg />
        <bsar:SecurityFuture />
        <bsar:Stocks />
        <bsar:SwapHybridDer />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:Product>
    <bsar:InstrumentOrPayment>
        <bsar:BankCheck />
        <bsar:ForeignCurrency />
        <bsar:FundsTransfer />
        <bsar:GamingInstrm />
        <bsar:GovernmentPayment />
        <bsar:MoneyOrders />
        <bsar:PersonalCheck />
        <bsar:TravelersCheck />
        <bsar:USCurrency />
        <bsar:Other />
        <bsar:OtherSpecify />
    </bsar:InstrumentOrPayment>
    <bsar:InternalControlNumber>654321654321</bsar:InternalControlNumber>
    <bsar:CommodityTypeRecord>
        <bsar:CommodityType>commodity type</bsar:CommodityType>
    </bsar:CommodityTypeRecord>
    <bsar:ProductInstrumentDescRecord>
        <bsar:ProductInstrumentDesc>product description</bsar:ProductInstrumentDesc>
    </bsar:ProductInstrumentDescRecord>
    <bsar:MarketTradedRecord>
        <bsar:MarketTraded>101</bsar:MarketTraded>
    </bsar:MarketTradedRecord>
    <bsar:IPAddressRecord>
        <bsar:IPAddress>255.255.255.255</bsar:IPAddress>
    </bsar:IPAddressRecord>
    <bsar:CUSIPNumberRecord>
        <bsar:CUSIPNumber>3465431654</bsar:CUSIPNumber>
    </bsar:CUSIPNumberRecord>
</bsar:SuspiciousActivityInformation> <!--3A-->
<bsar:SubjectInformation>
    <bsar:Entity />
    <bsar:SubjectInformationUnavailable />
    <bsar:LastNameOrNameOfEntity>Ceriello</bsar:LastNameOrNameOfEntity>
</bsar:SubjectInformation> <!--4A-->
<bsar:SubjectInformation>
    <bsar:Entity />
    <bsar:SubjectInformationUnavailable>B</bsar:SubjectInformationUnavailable>
    <bsar:LastNameOrNameOfEntity>Gutierrez</bsar:LastNameOrNameOfEntity>
</bsar:SubjectInformation> <!--4A-->
<bsar:SubjectInformation>
    <bsar:Entity />
    <bsar:SubjectInformationUnavailable />
    <bsar:LastNameOrNameOfEntity>Roper</bsar:LastNameOrNameOfEntity>
</bsar:SubjectInformation> <!--4A-->

基本上,伪代码看起来像这样:

Output 1A

For each 2A

    Output 2A

    For each 2B before the next 2A

        Output 2B

        For each 3A before the next 2B

            Output 3A; increment counter

                For each 4A before the next 3A

                    Output 4A with above counter

文本基本上需要按照文件中的顺序进行格式化,并带有一些摘要部分(例如,在每个2B之后)。我无法弄清楚如何循环遍历3A的元素,例如没有循环遍历文档中的所有4A记录。对于每个3A,我还需要保留一个计数器,以便我可以输出3A部分和相关4A部分的文本。

最初,我设计了一系列循环,它们遍历文件中的每个元素并为每个元素调用一次,但我意识到将挑选文件中的所有元素而不仅仅是出现的元素在我想要查看该部分的相关(父)元素下面。

然后,我考虑使用catch-all,因此所有元素都将按它们出现在文件中的顺序处理。但是,如果我这样做,我不知道如何使用任何类型的计数器。 (我知道我不能使用实际的计数器,但我的论点是在每个循环中调用一个模板并将position()作为参数传递)

如果您对此有何建议,请与我们联系。我有一些XSL经验,但这与我以前的有点不同。

提前致谢。

编辑:

输出应该看起来像这样:

1A Transmitter info
2A Filing Institution Info
2B Financial Institution Info
3A Suspicious Activity Info
9A Suspicious Activity Summary Info
2B Financial Institution 2 Info
3A Suspicious Activity 2 Info
4A Subject 1 Info
4A Subject 2 Info
4A Subject 3 Info
9B Filing institution   Summary info

这是我尝试过的一些XSL,但我确定无法工作,因为循环遍历整个文档中的所有记录类型,而不仅仅是兄弟元素作为父元素之后的循环:

<xsl:template match="/bsar:BSAForm">
    <xsl:for-each select="bsar:FilingInstitutionInformation"> 
        <xsl:call-template name="FileInstInfo"/> <!--2A-->
        <xsl:for-each select="../bsar:FinancialInstitutionInformation"> 
            <xsl:call-template name="FinInstActivity"/> <!--2B-->
            <xsl:for-each select="../bsar:SuspiciousActivityInformation">
                <xsl:call-template name="SuspActivityInfo"/> <!--3A-->
                <xsl:for-each select="../bsar:SubjectInformation">
                    <xsl:call-template name="Subject"/> <!--4A-->
                </xsl:for-each>
                <xsl:for-each select="../bsar:SuspiciousActivityInformationNarrative">
                    <xsl:call-template name="NarrativeDesc"/> <!--5A-->
                </xsl:for-each>
                <xsl:call-template name="SuspActivityInfoSmry"/> <!--9A-->
            </xsl:for-each>
        </xsl:for-each>
        <xsl:call-template name="FileInstSmry"/> <!--9B-->
    </xsl:for-each>
    <xsl:call-template name="FileSmry"/> <!--9Z-->
</xsl:template>

<xsl:template name="FileInstInfo" match="bsar:FilingInstitutionInformation">

    <xsl:variable name="FIType" select="bsar:FIType"/>
    <xsl:variable name="SecFuType" select="bsar:SecuritiesFuturesType"/>
    <xsl:variable name="FIId" select="bsar:FIIdentification"/>
    <xsl:variable name="FIIdType" select="$FIId/ucc:FIIDType"/>
    <xsl:variable name="Addr" select="bsar:FIAddress"/>
    <xsl:variable name="Country" select="$Addr/ucc:Country"/>
    <xsl:variable name="ZIP" select="translate($Addr/ucc:ZIP, translate($Addr/ucc:ZIP, '0123456789', ''), '')"/>
    <xsl:variable name="FinInstType" select="$FIType/ucc:FinInsType"/>

    <!-- 1-2(2) Record Type -->
    <xsl:text xml:space="preserve">2A</xsl:text>

    <!-- 3-4(2) Part IV Primary Federal Regulator -->
    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="bsar:PrimaryRegulator"/>
        <xsl:with-param name="length" select="2"/>
    </xsl:call-template>

    <!-- 5-154(150) Part IV Filer Name -->
    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="bsar:FilerName"/>
        <xsl:with-param name="length" select="150"/>
    </xsl:call-template>

    <!-- 155-179(25) Part IV TIN -->
    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="translate(bsar:TIN, translate(bsar:TIN, '0123456789', ''), '')"/>
        <xsl:with-param name="length" select="25"/>
    </xsl:call-template>

    <!-- 180(1) Part IV TIN Type -->
    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="bsar:TINTYPE"/>
        <xsl:with-param name="length" select="1"/>
    </xsl:call-template>

    <!-- 181(1) Part IV Type of Financial Institution -->
    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="$FinInstType"/>
        <xsl:with-param name="length" select="1"/>
    </xsl:call-template>

    <!-- 182-231(50) Part IV Type of Financial Institution - Other -->
    <xsl:variable name="FinInstTypeOther">
        <xsl:choose>
            <xsl:when test="$FinInstType = 'Z'">
                <xsl:value-of select="$FIType/ucc:OtherSpecify"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="''"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="$FinInstTypeOther"/>
        <xsl:with-param name="length" select="50"/>
    </xsl:call-template>

    <!-- 232-244(13) Part IV Type of Securities and Futures Institution -->
    <xsl:variable name="SecFuInstType">
        <xsl:choose>
            <xsl:when test="$FinInstType = 'E'">
                <xsl:for-each select="$SecFuType/*">
                    <xsl:if test="position() != last()">
                        <xsl:value-of select="."/>
                    </xsl:if>
                </xsl:for-each>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="''"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="$SecFuInstType"/>
        <xsl:with-param name="length" select="13"/>
    </xsl:call-template>

    <!-- 245-294(50) Part IV Type of Securities and Futures Institution - Other -->
    <xsl:variable name="SecFuInstOtherSpecify">
        <xsl:choose>
            <xsl:when test="contains($SecFuInstType,'Z')">
                <xsl:value-of select="$SecFuType/bsar:OtherSpecify"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="''"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="$SecFuInstOtherSpecify"/>
        <xsl:with-param name="length" select="50"/>
    </xsl:call-template>

    <!-- 295(1) Part IV Financial Institution Identification Type -->
    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="$FIIdType"/>
        <xsl:with-param name="length" select="1"/>
    </xsl:call-template>

    <!-- 296-315(20) Part IV Financial Institution Identification Number -->
    <xsl:variable name="FIIdNumber">
        <xsl:choose>
            <xsl:when test="$FIIdType != ''">
                <xsl:value-of select="$FIId/ucc:FIIDNumber"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="''"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="$FIIdNumber"/>
        <xsl:with-param name="length" select="20"/>
    </xsl:call-template>

    <!-- 316-415(100) Part IV Address -->
    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="$Addr/ucc:Address"/>
        <xsl:with-param name="length" select="100"/>
    </xsl:call-template>

    <!-- 416-465(50) Part IV City -->
    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="$Addr/ucc:City"/>
        <xsl:with-param name="length" select="50"/>
    </xsl:call-template>

    <!-- 466-468(3) Part IV State -->
    <xsl:variable name="State">
        <xsl:choose>
            <xsl:when test="$Country = 'CA' or $Country = 'US' or $Country = 'MX'">
                <xsl:value-of select="$Addr/ucc:State"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="''"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="$State"/>
        <xsl:with-param name="length" select="3"/>
    </xsl:call-template>

    <!-- 469-477(9) Part IV Zip / Postal Code -->
    <xsl:variable name="ZipCode">
        <xsl:choose>
            <xsl:when test="$Country = 'US' and (string-length($ZIP) = 9 and (substring($ZIP, 6, 9) = '0000' or substring($ZIP, 6, 9) = '9999'))">
                <xsl:value-of select="substring($ZIP, 1, 5)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$ZIP"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="$ZipCode"/>
        <xsl:with-param name="length" select="9"/>
    </xsl:call-template>

    <!-- 478-479(2) Part IV Country -->
    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="$Country"/>
        <xsl:with-param name="length" select="2"/>
    </xsl:call-template>

    <!-- 480-629(150) Part IV Alternate Name -->
    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="./bsar:AlternateName"/>
        <xsl:with-param name="length" select="150"/>
    </xsl:call-template>

    <!-- 630-1190(561) Filler -->
    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="''"/>
        <xsl:with-param name="length" select="561"/>
    </xsl:call-template>

    <!-- 1191-1200(10) User Field -->
    <xsl:call-template name="append-pad">
        <xsl:with-param name="text" select="''"/>
        <xsl:with-param name="length" select="10"/>
    </xsl:call-template>

    <xsl:value-of select="$newline" />

</xsl:template>

<xsl:template name="append-pad">
    <!-- recursive template to left justify and append  -->
    <!-- the value with whatever padding is passed in   -->
    <xsl:param name="padding" xml:space="preserve"> </xsl:param>
    <xsl:param name="text"/>
    <xsl:param name="length"/>
    <xsl:choose>
        <xsl:when test="string-length($text) &lt; $length">
            <xsl:call-template name="append-pad">
                <xsl:with-param name="padding" select="$padding"/>
                <xsl:with-param name="text" select="concat($text,$padding)"/>
                <xsl:with-param name="length" select="$length"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="substring($text,1,$length)"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template> 

第一部分应该产生以下输出:(此帖子省略了间隔)

2AD New Banking Corp.373637383ACD654646321321Ave XBrooklynNY11235USalternate name

1 个答案:

答案 0 :(得分:2)

我不确定是否有人真正理解我的问题,但如果有人来到这里遇到类似的问题,我就是这样解决的:

<xsl:template match="/bsar:BSAForm">
    <xsl:call-template name="Transmitter"/>
    <xsl:apply-templates select="bsar:FilingInstitutionInformation" />
    <xsl:call-template name="FileSmry"/>
</xsl:template>

<xsl:template match="bsar:FilingInstitutionInformation">

    <xsl:variable name="FileInstHeader" select="."/>

    <xsl:call-template name="FileInstInfo"/>

    <!--For each 2B before the next 2A-->
    <xsl:for-each select="following-sibling::bsar:FinancialInstitutionInformation[preceding-sibling::bsar:FilingInstitutionInformation[1] = $FileInstHeader]">
        <!--Output 2B-->
        <xsl:call-template name="FinInstActivity"/>
        <xsl:variable name="FinInstHeader" select="."/>
        <!--For each 3A before the next 2B-->
        <xsl:for-each select="following-sibling::bsar:SuspiciousActivityInformation[preceding-sibling::bsar:FinancialInstitutionInformation[1] = $FinInstHeader]">
            <!--Output 3A; Increment counter-->
            <xsl:variable name="TranSeqNum">
                <xsl:number count="bsar:SuspiciousActivityInformation" />
            </xsl:variable>
            <xsl:call-template name="SuspActivityInfo">
                <xsl:with-param name="TranSeqNum" select="$TranSeqNum"/>
            </xsl:call-template>
            <xsl:variable name="SuspActHeader" select="."/>
            <!--For each 4A before the next 3A-->
            <xsl:for-each select="following-sibling::bsar:SubjectInformation[preceding-sibling::bsar:SuspiciousActivityInformation[1] = $SuspActHeader]">
                <!--Output 4A with above counter-->
                <xsl:call-template name="Subject">
                    <xsl:with-param name="TranSeqNum" select="$TranSeqNum"/>
                </xsl:call-template>
            </xsl:for-each>
            <!--Output 9A-->
            <!--<xsl:call-template name="SuspActivityInfoSmry"/>-->
        </xsl:for-each>
    </xsl:for-each>

    <!--Output 9B-->
    <xsl:call-template name="FileInstSmry"/>

</xsl:template>

“Transmitter”记录需要在开头显示一次(显示模板不相关),并且FileInstSmry记录需要在结尾处显示一次。其他所有内容按2A记录分组。调用将为文档中找到的每个标记调用模板。对于每个找到的模板,将调用模板的详细信息(),然后每次运行FilingInsitutionInformation模板时运行内部循环。

FileInstHeader包含父元素,因此每个循环的第一个元素都会运行,直到命中了另一个元素。标头变量设置为下一个父级,循环继续。我还没有想出3A和4A部分的计数器,但如果有人有兴趣,我可以在此时更新。