使用XSL或Xpath将一个XML infopath源文件转换为另一个XML目标文件

时间:2015-08-05 18:38:51

标签: xml xslt xpath transform infopath

我是XSLT,XPath和XML的新手。我正在设计一个原型,将infopath表格(Expenses Entry)xml文件连接到ERP以生成发票。

下面是infopath表单(简化标题)中的XML源文件:

<?xml version="1.0" encoding="UTF-8"?>
<my:Requester>The Requester</my:Requester>
<my:Date>2015-07-20</my:Date>
<my:grProgram></my:grProgram>
<my:Program>Program A</my:Program>
<my:grSite>
    <my:Site>Site A</my:Site>
    <my:grVolunteer>
        <my:Name>0000003686</my:Name>
        <my:VendorID>0000003686</my:VendorID>
        <my:InvoiceID>BMFF036862015-07-20</my:InvoiceID>
        <my:InvoiceDate>2015-07-20</my:InvoiceDate>
        <my:Comments></my:Comments>
        <my:MoreInformation>Volunteer 1</my:MoreInformation>
        <my:grExpenses>
            <my:Category>Stipend</my:Category>
            <my:Amount>75</my:Amount>
            <my:Account>754100</my:Account>
            <my:SpeedChart>G1002469</my:SpeedChart>
            <my:AccountingDepartment>3059000</my:AccountingDepartment>
            <my:AccountingPCBusUnit>GRT01</my:AccountingPCBusUnit>
            <my:AccountingFundCode>3100</my:AccountingFundCode>
            <my:AccountingProgramCode>C001</my:AccountingProgramCode>
            <my:AccountingProjectID>1002469</my:AccountingProjectID>
            <my:AccountingActivityID>1</my:AccountingActivityID>
        </my:grExpenses>
        <my:grExpenses>
            <my:Category>Miles</my:Category>
            <my:Amount>45</my:Amount>
            <my:Account>750190</my:Account>
            <my:SpeedChart>G1002159</my:SpeedChart>
            <my:AccountingDepartment>3059000</my:AccountingDepartment>
            <my:AccountingPCBusUnit>GRT01</my:AccountingPCBusUnit>
            <my:AccountingFundCode>3900</my:AccountingFundCode>
            <my:AccountingProgramCode>C001</my:AccountingProgramCode>
            <my:AccountingProjectID>1002159</my:AccountingProjectID>
            <my:AccountingActivityID>1</my:AccountingActivityID>
        </my:grExpenses>
        <my:Total>120</my:Total>
    </my:grVolunteer>
        <my:grVolunteer>
        <my:Name>0000003707</my:Name>
        <my:VendorID>0000003707</my:VendorID>
        <my:InvoiceID>SFFF037072015-07-20</my:InvoiceID>
        <my:InvoiceDate>2015-07-20</my:InvoiceDate>
        <my:Comments></my:Comments>
        <my:MoreInformation>Volunteer 2</my:MoreInformation>
        <my:grExpenses>
            <my:Category>Stipend</my:Category>
            <my:Amount>12</my:Amount>
            <my:Account>754100</my:Account>
            <my:SpeedChart>G1002469</my:SpeedChart>
            <my:AccountingDepartment>3059000</my:AccountingDepartment>
            <my:AccountingPCBusUnit>GRT01</my:AccountingPCBusUnit>
            <my:AccountingFundCode>3100</my:AccountingFundCode>
            <my:AccountingProgramCode>C001</my:AccountingProgramCode>
            <my:AccountingProjectID>1002469</my:AccountingProjectID>
            <my:AccountingActivityID>1</my:AccountingActivityID>
        </my:grExpenses>
            <my:grExpenses>
            <my:Category>Miles</my:Category>
            <my:Amount>15</my:Amount>
            <my:Account>750190</my:Account>
            <my:SpeedChart>G1002159</my:SpeedChart>
            <my:AccountingDepartment>3059000</my:AccountingDepartment>
            <my:AccountingPCBusUnit>GRT01</my:AccountingPCBusUnit>
            <my:AccountingFundCode>3900</my:AccountingFundCode>
            <my:AccountingProgramCode>C001</my:AccountingProgramCode>
            <my:AccountingProjectID>1002159</my:AccountingProjectID>
            <my:AccountingActivityID>1</my:AccountingActivityID>
        </my:grExpenses><my:grExpenses>
            <my:Category>Miles</my:Category>
            <my:Amount>45</my:Amount>
            <my:Account>750190</my:Account>
            <my:SpeedChart>G1002164</my:SpeedChart>
            <my:AccountingDepartment>3059000</my:AccountingDepartment>
            <my:AccountingPCBusUnit>GRT01</my:AccountingPCBusUnit>
            <my:AccountingFundCode>3900</my:AccountingFundCode>
            <my:AccountingProgramCode>C001</my:AccountingProgramCode>
            <my:AccountingProjectID>1002164</my:AccountingProjectID>
            <my:AccountingActivityID>1</my:AccountingActivityID>
        </my:grExpenses><my:Total>72</my:Total>
    </my:grVolunteer></my:grSite>
<my:grSite>
    <my:Site>WI</my:Site>
    <my:grVolunteer>
        <my:Name>0000002967</my:Name>
        <my:VendorID>0000002967</my:VendorID>
        <my:InvoiceID>CCFW029672015-07-20</my:InvoiceID>
        <my:InvoiceDate>2015-07-20</my:InvoiceDate>
        <my:Comments></my:Comments>
        <my:MoreInformation>Volunteer 3</my:MoreInformation>
        <my:grExpenses>
            <my:Category>Stipend</my:Category>
            <my:Amount>78</my:Amount>
            <my:Account>754100</my:Account>
            <my:SpeedChart>3140110F25</my:SpeedChart>
            <my:AccountingDepartment>3140110</my:AccountingDepartment>
            <my:AccountingPCBusUnit></my:AccountingPCBusUnit>
            <my:AccountingFundCode>2500</my:AccountingFundCode>
            <my:AccountingProgramCode>C001</my:AccountingProgramCode>
            <my:AccountingProjectID></my:AccountingProjectID>
            <my:AccountingActivityID></my:AccountingActivityID>
        </my:grExpenses>
        <my:Total>78</my:Total>
    </my:grVolunteer>
</my:grSite></my:grSCorpProgrFields>

这是XML目标文件:

<?xml version="1.0" encoding="UTF-8"?>
<VCHR_HDR_STG>
    <BUSINESS_UNIT>BU</BUSINESS_UNIT>
    <VOUCHER_ID>NEXT</VOUCHER_ID>
    <VOUCHER_STYLE>REG</VOUCHER_STYLE>
    <INVOICE_ID>BMFF036862015-07-20</INVOICE_ID>
    <INVOICE_DT>07/20/2015</INVOICE_DT>
    <VENDOR_ID>0000003686</VENDOR_ID>
    <GROSS_AMT>120</GROSS_AMT>
    <DESCR254_MIXED>Volunteer 1</DESCR254_MIXED>
    <VCHR_SRC>XLS</VCHR_SRC>
    <VCHR_LINE_STG>
    <BUSINESS_UNIT>BU</BUSINESS_UNIT>
    <VOUCHER_LINE_NUM>1</VOUCHER_LINE_NUM>
    <DESCR>Stipend</DESCR>
    <MERCHANDISE_AMT>75</MERCHANDISE_AMT>
    <VOUCHER_ID>NEXT</VOUCHER_ID>
    <VCHR_DIST_STG>
        <BUSINESS_UNIT>BU</BUSINESS_UNIT>
        <VOUCHER_LINE_NUM>1</VOUCHER_LINE_NUM>
        <DISTRIB_LINE_NUM>1</DISTRIB_LINE_NUM>
        <ACCOUNT >754100</ACCOUNT >
        <DEPTID>3059000</DEPTID>
        <MERCHANDISE_AMT>75</MERCHANDISE_AMT>
        <BUSINESS_UNIT_PC>GRT01</BUSINESS_UNIT_PC>
        <ACTIVITY_ID>1</ACTIVITY_ID>
        <FUND_CODE>3100</FUND_CODE>
        <PROGRAM_CODE >C001</PROGRAM_CODE >
        <PROJECT_ID>1002469</PROJECT_ID>
        <VOUCHER_ID>NEXT</VOUCHER_ID>
    </VCHR_DIST_STG>
    </VCHR_LINE_STG>
    <VCHR_LINE_STG>
    <BUSINESS_UNIT>BU</BUSINESS_UNIT>
    <VOUCHER_LINE_NUM>2</VOUCHER_LINE_NUM>
    <DESCR>Miles</DESCR>
    <MERCHANDISE_AMT>45</MERCHANDISE_AMT>
    <VOUCHER_ID>NEXT</VOUCHER_ID>
    <VCHR_DIST_STG>
        <BUSINESS_UNIT>BU</BUSINESS_UNIT>
        <VOUCHER_LINE_NUM>2</VOUCHER_LINE_NUM>
        <DISTRIB_LINE_NUM>2</DISTRIB_LINE_NUM>
        <ACCOUNT >750190</ACCOUNT >
        <DEPTID>3059000</DEPTID>
        <MERCHANDISE_AMT>45</MERCHANDISE_AMT>
        <BUSINESS_UNIT_PC>GRT01</BUSINESS_UNIT_PC>
        <ACTIVITY_ID>1</ACTIVITY_ID>
        <FUND_CODE>3900</FUND_CODE>
        <PROGRAM_CODE >C001</PROGRAM_CODE >
        <PROJECT_ID>1002159</PROJECT_ID>
        <VOUCHER_ID>NEXT</VOUCHER_ID>
    </VCHR_DIST_STG>
    </VCHR_LINE_STG>
</VCHR_HDR_STG>
<VCHR_HDR_STG>
    <BUSINESS_UNIT>BU</BUSINESS_UNIT>
    <VOUCHER_ID>NEXT</VOUCHER_ID>
    <VOUCHER_STYLE>REG</VOUCHER_STYLE>
    <INVOICE_ID>SFFF037072015-07-20</INVOICE_ID>
    <INVOICE_DT>07/20/2015</INVOICE_DT>
    <GROSS_AMT>72</GROSS_AMT>
    <VENDOR_ID>0000002250</VENDOR_ID>
    <DESCR254_MIXED>Volunteer 2</DESCR254_MIXED>
    <VCHR_SRC>XLS</VCHR_SRC>
    <VCHR_LINE_STG>
    <BUSINESS_UNIT>BU</BUSINESS_UNIT>
    <VOUCHER_LINE_NUM>1</VOUCHER_LINE_NUM>
    <DESCR>Stipend</DESCR>
    <MERCHANDISE_AMT>12</MERCHANDISE_AMT>
    <VOUCHER_ID>NEXT</VOUCHER_ID>
    <VCHR_DIST_STG>
        <BUSINESS_UNIT>BU</BUSINESS_UNIT>
        <VOUCHER_LINE_NUM>1</VOUCHER_LINE_NUM>
        <DISTRIB_LINE_NUM>1</DISTRIB_LINE_NUM>
        <ACCOUNT >754100</ACCOUNT >
        <DEPTID>3059000</DEPTID>
        <MERCHANDISE_AMT>12</MERCHANDISE_AMT>
        <BUSINESS_UNIT_PC>GRT01</BUSINESS_UNIT_PC>
        <ACTIVITY_ID>1</ACTIVITY_ID>
        <FUND_CODE>3100</FUND_CODE>
        <PROGRAM_CODE >C001</PROGRAM_CODE >
        <PROJECT_ID>1002469</PROJECT_ID>
        <VOUCHER_ID>NEXT</VOUCHER_ID>
       </VCHR_DIST_STG>
    </VCHR_LINE_STG>
    <VCHR_LINE_STG>
    <BUSINESS_UNIT>BU</BUSINESS_UNIT>
    <VOUCHER_LINE_NUM>2</VOUCHER_LINE_NUM>
    <DESCR>Miles</DESCR>
    <MERCHANDISE_AMT>15</MERCHANDISE_AMT>
    <VOUCHER_ID>NEXT</VOUCHER_ID>
    <VCHR_DIST_STG>
        <BUSINESS_UNIT>BU</BUSINESS_UNIT>
        <VOUCHER_LINE_NUM>2</VOUCHER_LINE_NUM>
        <DISTRIB_LINE_NUM>2</DISTRIB_LINE_NUM>
        <ACCOUNT >750190</ACCOUNT >
        <DEPTID>3059000</DEPTID>
        <MERCHANDISE_AMT>15</MERCHANDISE_AMT>
        <BUSINESS_UNIT_PC>GRT01</BUSINESS_UNIT_PC>
        <ACTIVITY_ID>1</ACTIVITY_ID>
        <FUND_CODE>3900</FUND_CODE>
        <PROGRAM_CODE >C001</PROGRAM_CODE >
        <PROJECT_ID>1002159</PROJECT_ID>
        <VOUCHER_ID>NEXT</VOUCHER_ID>
       </VCHR_DIST_STG>
    </VCHR_LINE_STG>
    <VCHR_LINE_STG>
    <BUSINESS_UNIT>BU</BUSINESS_UNIT>
    <VOUCHER_LINE_NUM>3</VOUCHER_LINE_NUM>
    <DESCR>Miles</DESCR>
    <MERCHANDISE_AMT>45</MERCHANDISE_AMT>
    <VOUCHER_ID>NEXT</VOUCHER_ID>
    <VCHR_DIST_STG>
        <BUSINESS_UNIT>BU</BUSINESS_UNIT>
        <VOUCHER_LINE_NUM>3</VOUCHER_LINE_NUM>
        <DISTRIB_LINE_NUM>3</DISTRIB_LINE_NUM>
        <ACCOUNT >750190</ACCOUNT >
        <DEPTID>3059000</DEPTID>
        <MERCHANDISE_AMT>45</MERCHANDISE_AMT>
        <BUSINESS_UNIT_PC>GRT01</BUSINESS_UNIT_PC>
        <ACTIVITY_ID>1</ACTIVITY_ID>
        <FUND_CODE>3900</FUND_CODE>
        <PROGRAM_CODE >C001</PROGRAM_CODE >
        <PROJECT_ID>1002164</PROJECT_ID>
        <VOUCHER_ID>NEXT</VOUCHER_ID>
       </VCHR_DIST_STG>
    </VCHR_LINE_STG>
</VCHR_HDR_STG>
<VCHR_HDR_STG>
    <BUSINESS_UNIT>BU</BUSINESS_UNIT>
    <VOUCHER_ID>NEXT</VOUCHER_ID>
    <VOUCHER_STYLE>REG</VOUCHER_STYLE>
    <INVOICE_ID>CCFW029672015-07-20</INVOICE_ID>
    <INVOICE_DT>07/20/2015</INVOICE_DT>
    <VENDOR_ID>0000002962</VENDOR_ID>
    <GROSS_AMT>78</GROSS_AMT>
    <DESCR254_MIXED>Volunteer 3</DESCR254_MIXED>
    <VCHR_SRC>XLS</VCHR_SRC>
    <VCHR_LINE_STG>
    <BUSINESS_UNIT>BU</BUSINESS_UNIT>
    <VOUCHER_LINE_NUM>1</VOUCHER_LINE_NUM>
    <DESCR>Stipend</DESCR>
    <MERCHANDISE_AMT>78</MERCHANDISE_AMT>
    <VOUCHER_ID>NEXT</VOUCHER_ID>
    <VCHR_DIST_STG>
        <BUSINESS_UNIT>BU</BUSINESS_UNIT>
        <VOUCHER_LINE_NUM>1</VOUCHER_LINE_NUM>
        <DISTRIB_LINE_NUM>1</DISTRIB_LINE_NUM>
        <ACCOUNT >754100</ACCOUNT >
        <DEPTID>3140110</DEPTID>
        <MERCHANDISE_AMT>78</MERCHANDISE_AMT>
        <BUSINESS_UNIT_PC></BUSINESS_UNIT_PC>
        <ACTIVITY_ID></ACTIVITY_ID>
        <FUND_CODE>2500</FUND_CODE>
        <PROGRAM_CODE ></PROGRAM_CODE >
        <PROJECT_ID></PROJECT_ID>
        <VOUCHER_ID>NEXT</VOUCHER_ID>
    </VCHR_DIST_STG>
    </VCHR_HDR_STG>
</VCHR_HDR_STG>

对于每个志愿者,我们生成一个凭证标题(VCHR_HDR_STG),对于每个志愿者费用类别(StipendMiles),我们生成一个子凭证行节点({{ 1}})和子分布(VCHR_HDR_STG)。

XMl目标文件的某些标记/项目默认为常量,如Next,BU,REG和XLS。

这是XML数据源和XML之间的映射。 输出:

VCHR_DIST_STG

BUSINESS_UNIT(by default to 'BU') VOUCHER_ID(by default to 'NEXT') VOUCHER_STYLE(by default to'NEXT') InvoiceID > INVOICE_ID InvoiceDate > INVOICE_DT VendorID > VENDOR_ID Total > GROSS_AMT MoreInformation > DESCR254_MIXED VCHR_SRC(by default to'XLS') VOUCHER_LINE_NUM相等,我们可以使用志愿者的费用行数来增加DISTRIB_LINE_NUMVOUCHER_LINE_NUM

DISTRIB_LINE_NUM

1 个答案:

答案 0 :(得分:0)

因此,您需要通过XSLT样式集一个XML转换为另一个XML:

XSLT样式表基本上基于事件:您必须声明您期望的节点(使用<xsl:template>)并在其中,您要生成的文本以及子您期望的事件(使用<xsl:apply-templates>)。

在这里,我给你一个你必须完成的骨架样式表:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="declare-here-the-input-namespace-uri">

<xsl:output method="xml" version="1.0" encoding="UTF-8"/>

<!--The input root node-->
<xsl:template match="/my:grSCorpProgrFields">
<VCHR_HDR_STG>
    <xsl:apply-templates select="my:grSite"></xsl:apply-templates>
</VCHR_HDR_STG>
</xsl:template>

<xsl:template match="my:grSite">
    <VCHR_HDR_STG>
        <BUSINESS_UNIT>BU</BUSINESS_UNIT>
        <VOUCHER_ID>NEXT</VOUCHER_ID>
        <VOUCHER_STYLE>REG</VOUCHER_STYLE>
        <INVOICE_ID><xsl:value-of select="my:InvoiceID"/></INVOICE_ID>
        <INVOICE_DT><xsl:value-of select="my:InvoiceDate"/></INVOICE_DT>
        ...
    </VCHR_HDR_STG>
</xsl:template>

</xsl:stylesheet>

(请注意,我已经离开了xmlns:my命名空间的声明,并且都不确切知道输入XML的根节点是什么。)

完成XSL样式表后,必须通过正确的转换API使用它,这取决于您使用的平台/语言。