2013-11-04 46 views
1

我有以下XML文件,每個SALES標記中都有重複的DateSold標記。 我需要那些被替換爲DateSold和TimeSold。或者將日期和時間部分合併爲一個標籤。用XSL重命名或合併重複XML標記

源XML:

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <SALES> 
     <InvoiceID>A13A30000011</InvoiceID> 
     <LineID>1</LineID> 
     <UPC>058030020130</UPC> 
     <Desc>PS WQ VIT E 200IU 100'S</Desc> 
     <DateSold>2013-10-30</DateSold> 
     <DateSold>10:02:42</DateSold> 
     <QTY>000001</QTY> 
     <UnitRetail>000006.99</UnitRetail> 
     <UnitCost>000003.37</UnitCost> 
    </SALES> 
    <SALES> 
     <InvoiceID>A13A30000021</InvoiceID> 
     <LineID>2</LineID> 
     <UPC>063601699165</UPC> 
     <Desc>GENTEAL GEL DROPS 15ML</Desc> 
     <DateSold>2013-10-30</DateSold> 
     <DateSold>10:03:15</DateSold> 
     <QTY>000001</QTY> 
     <UnitRetail>000010.99</UnitRetail> 
     <UnitCost>000007.44</UnitCost> 
    </SALES> 
</root> 

輸出將是:

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <SALES> 
     <InvoiceID>A13A30000011</InvoiceID> 
     <LineID>1</LineID> 
     <UPC>058030020130</UPC> 
     <Desc>PS WQ VIT E 200IU 100'S</Desc> 
     <DateSold>2013-10-30</DateSold> 
     <TimeSold>10:02:42</TimeSold> 
     <QTY>000001</QTY> 
     <UnitRetail>000006.99</UnitRetail> 
     <UnitCost>000003.37</UnitCost> 
    </SALES> 
    <SALES> 
     <InvoiceID>A13A30000021</InvoiceID> 
     <LineID>2</LineID> 
     <UPC>063601699165</UPC> 
     <Desc>GENTEAL GEL DROPS 15ML</Desc> 
     <DateSold>2013-10-30</DateSold> 
     <TimeSold>10:03:15</TimeSold> 
     <QTY>000001</QTY> 
     <UnitRetail>000010.99</UnitRetail> 
     <UnitCost>000007.44</UnitCost> 
    </SALES> 
</root> 

這是確定的,如果它被合併爲

<SALES> 
    <InvoiceID>A13A30000021</InvoiceID> 
    <LineID>2</LineID> 
    <UPC>063601699165</UPC> 
    <Desc>GENTEAL GEL DROPS 15ML</Desc> 
    <DateSold>2013-10-30 10:03:15</TimeSold> 
    <QTY>000001</QTY> 
    <UnitRetail>000010.99</UnitRetail> 
    <UnitCost>000007.44</UnitCost>  
</SALES> 
+0

請包括你已經嘗試了一些什麼信息,以及在哪裏卡住了。本網站不適用於請求爲您編寫代碼的請求。 – IMSoP

+0

' 2013-10-30 10:03:15'形成不好 – Hash

回答

0

嗯,你不告訴你已經嘗試了什麼到目前爲止,所以我只會給一些更一般的建議。

與往常一樣,如果你想保留大部分結構完整,先從身份模板到所有輸入複製到輸出結構:

<xsl:template match="@*|node()"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

然後作出這樣對待你想標記一些特殊的模板處理不同。 針對您的問題有大量可能的解決方案,其中之一將匹配DateSold元素,並將其編寫爲DateSoldTimeSold,具體取決於它是否爲sales元素中的第1個或第2個子元素(如果它們始終以這種方式訂購)。

另一種方法是給Sales元素相匹配,選擇兩個子元素,並結合/他們不同寫入到輸出文件,並在其他元素複製與apply-templates調用(然後再調用你的身份模板)。

關於如何具體執行此操作,請顯示您在XSLT上嘗試或閱讀過的內容。網上有很多可用的資源。一個好的起點可能是www.w3schools.com

他們有每個xslt標籤的例子。 apply-templates的一個是this

1

我終於找到了解決方案。以下xslt將每個銷售代碼中的第二個Datesold標籤替換爲TimeSold。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()" /> 
    </xsl:copy> 
</xsl:template> 
<xsl:template match="SALES/DateSold[2]"> 
    <TimeSold> 
     <xsl:apply-templates select="@*|node()" /> 
    </TimeSold> 
</xsl:template> 
</xsl:stylesheet> 

我得到的最終結果爲:

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <SALES> 
    <InvoiceID>A13A30000011</InvoiceID> 
    <LineID>1</LineID> 
    <UPC>058030020130</UPC> 
    <Desc>PS WQ VIT E 200IU 100'S</Desc> 
    <DateSold>2013-10-30</DateSold> 
    <TimeSold>10:02:42</TimeSold> 
    <QTY>000001</QTY> 
    <UnitRetail>000006.99</UnitRetail> 
    <UnitCost>000003.37</UnitCost> 
    </SALES> 
    <SALES> 
    <InvoiceID>A13A30000021</InvoiceID> 
    <LineID>2</LineID> 
    <UPC>063601699165</UPC> 
    <Desc>GENTEAL GEL DROPS 15ML</Desc> 
    <DateSold>2013-10-30</DateSold> 
    <TimeSold>10:03:15</TimeSold> 
    <QTY>000001</QTY> 
    <UnitRetail>000010.99</UnitRetail> 
    <UnitCost>000007.44</UnitCost> 
    </SALES> 
</root>