首页 > 解决方案 > 在将 XML 转换为文本时,第二行在 XSLT 中移动了两个选项卡

问题描述

我有下面的 xml,我写了一个 XSLT 来将输出转换为文本格式,但是在病房的第二行,输出移动了两个选项卡,我不知道为什么。任何人都可以帮忙吗?

xslt有什么问题吗?是什么让第二行发生了变化?

<?xml version="1.0" encoding="UTF-8"?>
<document>
    <businessobjects>
        <entry>
            <HeaderLine>
                <HeaderRow>H</HeaderRow>
                <HeaderBusinessUnit/>
                <CurrentTime>0704202004:47:43</CurrentTime>
                <TotalAmountCredit>-6.084732570000003E6</TotalAmountCredit>
                <TotalAmountDebit>6.194972230000004E6</TotalAmountDebit>
                <LineEntries>948</LineEntries>
            </HeaderLine>
            <LineDetails>
                <Line>L</Line>
                <HeaderBusinessUnit/>
                <FinancialYear>2020</FinancialYear>
                <Period>5</Period>
                <EndDate>20200101</EndDate>
                <Period>5</Period>
                <GLBusinessUnit>UTEP1</GLBusinessUnit>
                <GLAccount/>
                <EntryType>DEBITJOURNALENTRY</EntryType>
                <Amount>10000</Amount>
                <Currency>USD</Currency>
                <CostCenter>14026800</CostCenter>
                <ContractCode>L0498</ContractCode>
                <ContractName>Base Case - AC</ContractName>
                <ContractLineCode>1886</ContractLineCode>
                <PostingCode>1</PostingCode>
            </LineDetails>
            
            
        </entry>
    </businessobjects>
</document>

使用 XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:variable name='newline'>
        <xsl:text>
        </xsl:text>
    </xsl:variable>
    
    <xsl:template match="businessobjects">
       <xsl:for-each select="entry">
           <xsl:for-each select="HeaderLine">
            <xsl:value-of select="concat(HeaderRow,'|',HeaderBusinessUnit,'|',
                CurrentTime,'|',TotalAmountCredit,'|',
                TotalAmountDebit,'|',LineEntries,$newline)"/>
        </xsl:for-each>
           <xsl:for-each select="LineDetails">
               <xsl:value-of select="concat(Line,'|',HeaderBusinessUnit,'|',FinancialYear,
                   '|',Period,'|',EndDate,'|',GLBusinessUnit,'|',GLAccount,'|',EntryType,'|',Amount,'|',Currency,'|',CostCenter,'|',ContractCode,'|',ContractName,'|',ContractLineCode,'|',PostingCode,$newline)"/>
           </xsl:for-each>
       </xsl:for-each>
    </xsl:template>
    
</xsl:stylesheet>

输出

H||0704202004:47:43|-6.084732570000003E6|6.194972230000004E6|948
        L||2020|5|20200101|UTEP1||DEBITJOURNALENTRY|10000|USD|14026800|L0498|Base Case - AC|1886|1
    


    

标签: xmlxslt

解决方案


但在病房的第二行,输出移动了两个选项卡,我不知道为什么。

因为您的$newline变量包含一个换行符和两个制表符:

<xsl:variable name='newline'>
    <xsl:text>
    </xsl:text>
</xsl:variable>

将其更改为:

    <xsl:variable name='newline'>
        <xsl:text>
</xsl:text>
    </xsl:variable>

或(最好是恕我直言)摆脱变量并使用您想要的确切换行符,例如

<xsl:text>&#10;</xsl:text>

推荐阅读