起初我想知道使用xslt是否可以实现这一点,如果是这样的话怎么做:
我有两个xmls。 第一个是关于发票的信息
$(document).ready(function(){
$('#selector').datetimepicker({
viewMode: 'days',
format: 'DD/MM/YYYY'
});
});
,第二个是关于发票项目的具体信息
<INVOICES>
<INVOICE>
<INVOICE_NUMBER>FV2014000572</INVOICE_NUMBER>
<SUPPLIER_NAME>Supplier 1</SUPPLIER_NAME>
</INVOICE>
<INVOICE>
<INVOICE_NUMBER>FV2014000573</INVOICE_NUMBER>
<SUPPLIER_NAME>Supplier 1</SUPPLIER_NAME>
</INVOICE>
</INVOICES>
我想以下列格式将这两个xmls转换为一个:
<INVOICE_ITEMS>
<INVOICE_ITEM>
<ITEM_INVOICE_NUMBER>FV2014000572</ITEM_INVOICE_NUMBER>
<ITEM_NAME>ITEM 1</ITEM_NAME>
</INVOICE_ITEM>
<INVOICE_ITEM>
<ITEM_INVOICE_NUMBER>FV2014000573</ITEM_INVOICE_NUMBER>
<ITEM_NAME>ITEM 1</ITEM_NAME>
</INVOICE_ITEM>
<INVOICE_ITEM>
<ITEM_INVOICE_NUMBER>FV2014000573</ITEM_INVOICE_NUMBER>
<ITEM_NAME>ITEM 2</ITEM_NAME>
</INVOICE_ITEM>
<INVOICE_ITEMS>
如果可能的话,从最终的xml中删除ITEM_INVOICE_NUMBER个元素。
由于
答案 0 :(得分:1)
这个非常基本的样式表应该为您提供如何继续的线索。它将应用于您在输入中提供的第一个XML,并假定存储其他信息的第二个XML名为 data.xml 。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:variable name="invoiceitems" select="document('data.xml')" />
<!-- just copy the element, and process contents. Warning: attributes (if any) won't be processed. -->
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="INVOICE">
<xsl:variable name="current.invoice.number" select="INVOICE_NUMBER" />
<xsl:copy>
<xsl:apply-templates />
<!-- create an INVOICE_ITEMS element, and start to retrieve information from the other XML -->
<INVOICE_ITEMS>
<xsl:apply-templates select="$invoiceitems//INVOICE_ITEM[ITEM_INVOICE_NUMBER/text() = $current.invoice.number]"></xsl:apply-templates>
</INVOICE_ITEMS>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>