使用XSLT将XML转换为XML(sybase输出)

时间:2016-10-07 10:16:26

标签: xml xslt transformation

我有以下问题。我不是XSLT中最伟大的专家,我想成功的是将XML从Sybase转换为以下内容:

Sybase的输出:

awk -vFS="," -vOFS="," -v columnStart=6 -v columnEnd=6 -v rowStart=3 -v rowEnd=3 \
     'NR>=rowStart&&NR<=rowEnd{for(i=columnStart; i<=columnEnd; i++) \
           $i="New_"$i; print }' file
user3,fuser3,luser3,user3@test.com,data,New_user3

输出我想要的方式 标题保持不变,条目部分不同

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="C:\Users\H50S5OB\Desktop\style.xsl"?>
<root>
<row>
    <Cpty_Id>1</Cpty_Id>
    <SENDER>xxx</SENDER>
    <KUNDE>123</KUNDE>
    <DEPOT>123</DEPOT>
    <ACCOUNT_DEBIT>123</ACCOUNT_DEBIT>
    <ACCOUNT_CREDIT>123</ACCOUNT_CREDIT>
    <CREATED>07.10.2016</CREATED>
    <DEALTYPE>FEES</DEALTYPE>
    <STATEMENT_FROM>30.09.2016</STATEMENT_FROM>
    <STATEMENT_TO>30.09.2016</STATEMENT_TO>
    <BUYSELL>V</BUYSELL>
    <WPNAME>AT0123456789</WPNAME>
    <NOMINAL>1.0</NOMINAL>
    <PRICE>117,155</PRICE>
    <DEAL_STARTDATE>07.10.2016</DEAL_STARTDATE>
    <DEAL_ENDDATE>28.06.2017</DEAL_ENDDATE>
    <FEES_PERCENT>2.5941420000000002</FEES_PERCENT>
    <FEES_AMOUNT>150.0</FEES_AMOUNT>
    <FEES_CREDIT>3.9100000000000001</FEES_CREDIT>
    <FEES>3.9100000000000001</FEES>
</row>
<row>
    <Cpty_Id>1</Cpty_Id>
    <SENDER>xxx</SENDER>
    <KUNDE>123</KUNDE>
    <DEPOT>123</DEPOT>
    <ACCOUNT_DEBIT>123</ACCOUNT_DEBIT>
    <ACCOUNT_CREDIT>123</ACCOUNT_CREDIT>
    <CREATED>07.10.2016</CREATED>
    <DEALTYPE>FEES</DEALTYPE>
    <STATEMENT_FROM>30.09.2016</STATEMENT_FROM>
    <STATEMENT_TO>30.09.2016</STATEMENT_TO>
    <BUYSELL>V</BUYSELL>
    <WPNAME>AT0123456789</WPNAME>
    <NOMINAL>1.0</NOMINAL>
    <PRICE>117,155</PRICE>
    <DEAL_STARTDATE>07.10.2016</DEAL_STARTDATE>
    <DEAL_ENDDATE>28.06.2017</DEAL_ENDDATE>
    <FEES_PERCENT>2.5941420000000002</FEES_PERCENT>
    <FEES_AMOUNT>150.0</FEES_AMOUNT>
    <FEES_CREDIT>3.9100000000000001</FEES_CREDIT>
    <FEES>3.9100000000000001</FEES>
</row>
</root>

到目前为止,这是我的XSLT(不是很多,对不起)

   <CONFIRM>
    <Cpty_Id>1</Cpty_Id>
    <SENDER>xxx</SENDER>
    <KUNDE>123</KUNDE>
    <DEPOT>123</DEPOT>
    <ACCOUNT_DEBIT>123</ACCOUNT_DEBIT>
    <ACCOUNT_CREDIT>123</ACCOUNT_CREDIT>
    <CREATED>07.10.2016</CREATED>
    <DEALTYPE>FEES</DEALTYPE>
    <STATEMENT_FROM>30.09.2016</STATEMENT_FROM>
    <STATEMENT_TO>30.09.2016</STATEMENT_TO>
    <ENTRY>
        <BUYSELL>V</BUYSELL>
        <WPNAME>AT0123456789</WPNAME>
        <NOMINAL>1.0</NOMINAL>
        <PRICE>117,155</PRICE>
        <DEAL_STARTDATE>07.10.2016</DEAL_STARTDATE>
        <DEAL_ENDDATE>28.06.2017</DEAL_ENDDATE>
        <FEES_PERCENT>2.5941420000000002</FEES_PERCENT>
        <FEES_AMOUNT>150.0</FEES_AMOUNT>
    </ENTRY>
    <ENTRY>
        <BUYSELL>E</BUYSELL>
        <WPNAME>AT0123456789</WPNAME>
        <NOMINAL>1.0</NOMINAL>
        <PRICE>117,155</PRICE>
        <DEAL_STARTDATE>07.10.2016</DEAL_STARTDATE>
        <DEAL_ENDDATE>28.06.2017</DEAL_ENDDATE>
        <FEES_PERCENT>2.5941420000000002</FEES_PERCENT>
        <FEES_AMOUNT>150.0</FEES_AMOUNT>
    </ENTRY>
    <SUM>
        <FEES_CREDIT>3.9100000000000001</FEES_CREDIT>
        <FEES>3.9100000000000001</FEES>
    </SUM>
   </CONFIRM>

也许,有人可以帮我实现我想要的。 非常感谢你。

1 个答案:

答案 0 :(得分:0)

以此为出发点:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/root">
    <CONFIRM>
        <!-- header -->
        <xsl:for-each select="row[1]">
            <xsl:copy-of select="Cpty_Id | SENDER | KUNDE | DEPOT |ACCOUNT_DEBIT | ACCOUNT_CREDIT | CREATED | DEALTYPE | STATEMENT_FROM | STATEMENT_TO"/>
        </xsl:for-each>
        <!-- entries -->
        <xsl:for-each select="row">
            <ENTRY>
                <xsl:copy-of select="BUYSELL | WPNAME | NOMINAL | PRICE | DEAL_STARTDATE |DEAL_ENDDATE | FEES_PERCENT | FEES_AMOUNT"/>
            </ENTRY>
        </xsl:for-each>
        <!-- footer -->
        <xsl:for-each select="row[1]">
            <SUM>
                <xsl:copy-of select="FEES_CREDIT | FEES "/>
            </SUM>
        </xsl:for-each>
   </CONFIRM>
</xsl:template>

</xsl:stylesheet>

这假设所有常见数据(ENTRY元素之前和之后)对于所有row都是相同的,并且可以从第一个row获取。

我怀疑SUM部分需要是别的,但我遵循了你预期的输出。