-3

我有以下由 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>

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

4

1 回答 1

1

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

  1. 删除 xml intros '<?xml version="1.0" encoding="UTF-8"?>' 例如使用 sed 或其他工具
  2. 将结果放在您自己的根元素中
  3. 进行所需的 xslt 转换

#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

于 2012-09-09T20:41:10.653 回答