學習軌道
/
Ballerina
Ballerina
/
練習
/
XML 燃料津貼
XML 燃料津貼

XML 燃料津貼

中等

說明

問題陳述

ABC Corp 的每一位員工都有無限的燃料補貼。員工每次為車輛加油時,都必須送出一筆紀錄。這些紀錄會附加到同一個 XML 檔案中,並在月底統一處理。

身為數位營運團隊的一員,你的任務是讀取這個 XML 檔案,並將結果寫入另一個 XML 檔案。輸出檔案中應該為每位員工包含一筆紀錄,並含有下列詳細資料:

你會收到兩個 string 引數。

  1. 輸入 XML 檔案的路徑。
  2. 輸出 XML 檔案的路徑。

輸入 XML 檔案的內容會符合以下的 XML 結構描述。

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.so2w.org" 
    xmlns:tns="http://www.so2w.org" 
    elementFormDefault="qualified">

    <xs:element name="FuelEvents">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="FuelEvent" type="tns:FuelUpEvent" nillable="true" minOccurs="0" maxOccurs="1000"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="FuelEvent">
        <xs:sequence>
            <xs:element name="odometerReading" type="xs:integer"/>
            <xs:element name="gallons" type="xs:decimal"/>
            <xs:element name="gasPrice" type="xs:decimal"/>
        </xs:sequence>
        <xs:attribute name="employeeId" type="xs:integer" use="required"/>
    </xs:complexType>
</xs:schema>

你的任務是將輸入的 XML 轉換成下列 XML 格式,並把內容寫入指定的路徑。輸出檔案中應該為每位員工包含一筆紀錄,並依 employeeId 由小到大排序。

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
   targetNamespace="http://www.so2w.org"
   xmlns:tns="http://www.so2w.org"
   elementFormDefault="qualified">
 
   <xs:element name="employeeFuelRecords">
       <xs:complexType>
           <xs:sequence>
               <xs:element name="employeeFuelRecord" type="tns:employeeFuelRecord" nillable="true" minOccurs="0" maxOccurs="97"/>
           </xs:sequence>
       </xs:complexType>
   </xs:element>
 
   <xs:complexType name="employeeFuelRecord">
       <xs:sequence>
           <xs:element name="gasFillUpCount" type="xs:integer"/>
           <xs:element name="totalFuelCost" type="xs:decimal"/>
           <xs:element name="totalGallons" type="xs:decimal"/>
           <xs:element name="totalMilesAccrued" type="xs:integer"/>
       </xs:sequence>
       <xs:attribute name="employeeId" type="xs:integer" use="required"/>
   </xs:complexType>
</xs:schema>

限制

  • 輸入 XML 檔案中的加油紀錄數量 n:0 <= n <= 1000
  • 公司的員工數量:97
  • 油價是以每加侖計算
  • 沒有缺漏的加油紀錄

範例 1

  • 輸入:example01_input.xml
<s:FuelEvents xmlns:s="http://www.so2w.org">
    <s:FuelEvent employeeId="2312">
        <s:odometerReading>230</s:odometerReading>
        <s:gallons>18.561</s:gallons>
        <s:gasPrice>4.56</s:gasPrice>
    </s:FuelEvent>
    <s:FuelEvent employeeId="2312">
        <s:odometerReading>500</s:odometerReading>
        <s:gallons>19.345</s:gallons>
        <s:gasPrice>4.89</s:gasPrice>
    </s:FuelEvent>
</s:FuelEvents>
  • 輸出:example01_output.xml
<s:employeeFuelRecords xmlns:s="http://www.so2w.org">
    <s:employeeFuelRecord employeeId="2312">
        <s:gasFillUpCount>2</s:gasFillUpCount>
        <s:totalFuelCost>179.23521</s:totalFuelCost>
        <s:totalGallons>37.906</s:totalGallons>
        <s:totalMilesAccrued>270</s:totalMilesAccrued>
    </s:employeeFuelRecord>
</s:employeeFuelRecords>

範例 2

  • 輸入:example02_input.xml
<s:FuelEvents xmlns:s="http://www.so2w.org">
    <s:FuelEvent employeeId="2413">
        <s:odometerReading>4089</s:odometerReading>
        <s:gallons>21.682</s:gallons>
        <s:gasPrice>3.46</s:gasPrice>
    </s:FuelEvent>
    <s:FuelEvent employeeId="3423">
        <s:odometerReading>6582</s:odometerReading>
        <s:gallons>15.248</s:gallons>
        <s:gasPrice>4.56</s:gasPrice>
    </s:FuelEvent>
    <s:FuelEvent employeeId="2413">
        <s:odometerReading>4127</s:odometerReading>
        <s:gallons>4.221</s:gallons>
        <s:gasPrice>3.40</s:gasPrice>
    </s:FuelEvent>
    <s:FuelEvent employeeId="2413">
        <s:odometerReading>4349</s:odometerReading>
        <s:gallons>11.192</s:gallons>
        <s:gasPrice>4.10</s:gasPrice>
    </s:FuelEvent>
    <s:FuelEvent employeeId="3423">
        <s:odometerReading>6767</s:odometerReading>
        <s:gallons>8.696</s:gallons>
        <s:gasPrice>3.34</s:gasPrice>
    </s:FuelEvent>
    <s:FuelEvent employeeId="2413">
        <s:odometerReading>4547</s:odometerReading>
        <s:gallons>9.197</s:gallons>
        <s:gasPrice>2.90</s:gasPrice>
    </s:FuelEvent>
</s:FuelEvents>
  • 輸出:example02_output.xml
<s:employeeFuelRecords xmlns:s="http://www.so2w.org">
    <s:employeeFuelRecord employeeId="2413">
        <s:gasFillUpCount>4</s:gasFillUpCount>
        <s:totalFuelCost>161.92962</s:totalFuelCost>
        <s:totalGallons>46.292</s:totalGallons>
        <s:totalMilesAccrued>458</s:totalMilesAccrued>
    </s:employeeFuelRecord>
    <s:employeeFuelRecord employeeId="3423">
        <s:gasFillUpCount>2</s:gasFillUpCount>
        <s:totalFuelCost>98.57552</s:totalFuelCost>
        <s:totalGallons>23.944</s:totalGallons>
        <s:totalMilesAccrued>185</s:totalMilesAccrued>
    </s:employeeFuelRecord>
</s:employeeFuelRecords>

提示

透過 GitHub 編輯 連結會在新視窗或分頁中開啟
Ballerina Exercism

準備好開始 XML 燃料津貼 了嗎?

註冊 Exercism,透過 60 個練習 和真人引導來學習並精通 Ballerina,全部免費。