如何使用XSLT将单个xml消息转换为通用XML文档

时间:2012-09-09 16:28:18

标签: xml xslt

我有以下由Tibco创建的xml消息

<?xml version="1.0" encoding="UTF-8"?>
<SalesInData>
    <COMPANYID>AIN2003</COMPANYID>
    <ENDDATE>20120825</ENDDATE>
    <FILENAME>2-SG4_2244_20120815.xml</FILENAME>
    <SalesInRecord>
        <ROW_NUM>7</ROW_NUM>
        <BUY_QTY>5</BUY_QTY>
        <HP_PRODUCT_ID>Apj45685</HP_PRODUCT_ID>
        <LOCATION_ID/>
        <LOCATION_NAME>XYZ XY</LOCATION_NAME>
        <UNIT_BUY_PRICE>999999999</UNIT_BUY_PRICE>
    </SalesInRecord>
    <STARTDATE>20120819</STARTDATE>
    <SUBMITDATE>20120827</SUBMITDATE>
</SalesInData>
<?xml version="1.0" encoding="UTF-8"?>
<SalesInData>
    <COMPANYID>AIN2003</COMPANYID>
    <ENDDATE>20120825</ENDDATE>
    <FILENAME>2-SG4_2244_20120815.xml</FILENAME>
    <SalesInRecord>
        <ROW_NUM>1</ROW_NUM>
        <BUY_QTY>5</BUY_QTY>
        <HP_PRODUCT_ID>Apj45678</HP_PRODUCT_ID>
        <LOCATION_ID>2-PQL-3401</LOCATION_ID>
        <LOCATION_NAME>XYZ el</LOCATION_NAME>
        <UNIT_BUY_PRICE>999999999</UNIT_BUY_PRICE>
    </SalesInRecord>
    <STARTDATE>20120819</STARTDATE>
    <SUBMITDATE>20120827</SUBMITDATE>
</SalesInData>
<?xml version="1.0" encoding="UTF-8"?>
<SalesInData>
    <COMPANYID>AIN2003</COMPANYID>
    <ENDDATE>20120825</ENDDATE>
    <FILENAME>2-SG4_2244_20120815.xml</FILENAME>
    <SalesInRecord>
        <ROW_NUM>1</ROW_NUM>
        <BUY_QTY>5</BUY_QTY>
        <HP_PRODUCT_ID>Apj45678</HP_PRODUCT_ID>
        <LOCATION_ID>2-PQL-3401</LOCATION_ID>
        <LOCATION_NAME>XYZ el</LOCATION_NAME>
        <UNIT_BUY_PRICE>999999999</UNIT_BUY_PRICE>
    </SalesInRecord>
    <STARTDATE>20120819</STARTDATE>
    <SUBMITDATE>20120827</SUBMITDATE>
</SalesInData>

但预期输出将为:

<?xml version="1.0" encoding="UTF-8"?>
<SalesInData>
    <COMPANYID>AIN2003</COMPANYID>
    <ENDDATE>20120825</ENDDATE>
    <FILENAME>2-SG4_2244_20120815.xml</FILENAME>
    <SalesInRecord>
        <ROW_NUM>7</ROW_NUM>
        <BUY_QTY>5</BUY_QTY>
        <HP_PRODUCT_ID>Apj45685</HP_PRODUCT_ID>
        <LOCATION_ID/>
        <LOCATION_NAME>XYZ XY</LOCATION_NAME>
        <UNIT_BUY_PRICE>999999999</UNIT_BUY_PRICE>
        <GOODS_RECIVED_DATE/>
    </SalesInRecord>
    <SalesInRecord>
        <ROW_NUM>1</ROW_NUM>
        <BUY_QTY>5</BUY_QTY>
        <HP_PRODUCT_ID>Apj45678</HP_PRODUCT_ID>
        <LOCATION_ID>2-PQL-3401</LOCATION_ID>
        <LOCATION_NAME>XYZ el</LOCATION_NAME>
        <UNIT_BUY_PRICE>999999999</UNIT_BUY_PRICE>
        <GOODS_RECIVED_DATE/>
    </SalesInRecord>
    <SalesInRecord>
        <ROW_NUM>1</ROW_NUM>
        <BUY_QTY>5</BUY_QTY>
        <HP_PRODUCT_ID>Apj45678</HP_PRODUCT_ID>
        <LOCATION_ID>2-PQL-3401</LOCATION_ID>
        <LOCATION_NAME>XYZ el</LOCATION_NAME>
        <UNIT_BUY_PRICE>999999999</UNIT_BUY_PRICE>
        <GOODS_RECIVED_DATE/>
    </SalesInRecord>
    <STARTDATE>20120819</STARTDATE>
    <SUBMITDATE>20120827</SUBMITDATE>
</SalesInData>

但我想将使用XSLT在tibco中创建的单个xml记录转换为单个xml文档,如上所示。请提供可以执行任务的代码。还有一些其他元素可以为null。

1 个答案:

答案 0 :(得分:1)

您的输入文件没有格式良好的XML,因此没有xslt处理器可以使用它。我看到的唯一方法是首先生成一个xml文件,然后在下一步中生成一个简单的xsl处理。

  1. 删除xml intros'&lt;?xml version =“1.0”encoding =“UTF-8”?&gt;'例如,使用sed或其他工具
  2. 将结果放在您自己的根元素
  3. 执行所需的xslt转换
  4. #simple bash example
    sed -e 's/<?xml version="1.0" encoding="UTF-8"?>//g' YOUR_INPUT > temp.file
    echo '<?xml version="1.0" encoding="UTF-8"?>' > temp2.file
    echo "<MyStart>" >> temp2.file
    cat temp.file >> temp2.file
    echo "</MyStart>" >> temp2.file
    xsltproc YOUR_STYLESHEET temp2.file > YOUR_RESULT