我需要使用xslt從文本文件生成格式良好的XML結構。我是xslt轉型的新手,這對我來說看起來有些挑戰。 示例文本文件如下。我需要省略第一部分並將引號中的值映射到輸出xml文件。從文本文件生成xml
Directory: sample/archive
Name: 20130613-T210002.TXT
---------------------------------------------------------------
"11FCK1GR0026" "G190" "FPB - OK Ship Pt" "A" "11" "XX" "02"
"11FCA1GR0034" "G980" "FPB -San Antonio" "A" "11" "XX" "02"
"11FCA1GR0034" "G160" "FPB -San Antonio" "A" "11" "XX" "02"
下面是所需的輸出XML格式:
<Account>
<Action>A</Action> <!-- 3rd element in the row-->
<org>G190</org> <!-- 2nd element-->
<code>11FCK1GR002611XX</code> <!--concat(1st element, 4th element, 5th element)-->
<FiscalYear>2013</FiscalYear> <!--calculate fiscal year from current date -->
<Info>
<Action>A</Action> <!-- 3rd element in the row-->
<org>G190</org> <!-- 2nd element-->
<code>11FCK1GR002611XX</code><!--concat(1st element, 4th element, 5th element)-->
<Fieldlab>Acc1</Fieldlab> <!-- Static value-->
<FieldVal>11FCK1GR0026 </FieldVal> <!-- if field order is 1, map 1st element -->
<FieldOrd>1</FieldOrd> <!-- Static value-->
<Info>
<Info>
<Action>A</Action>
<org>G190</org>
<code>11FCK1GR002611XX</code>
<Fieldlab>Acc2</Fieldlab>
<FieldVal>11</FieldVal> <!-- if field order is 2, map 5th element -->
<FieldOrd>2</FieldOrd>
<Info>
<Info>
<Action>A</Action> <!-- 4th element in the row-->
<org>G190</org> <!-- 2nd element-->
<code>11FCK1GR002611XX</code>
<Fieldlab>Acc3</Fieldlab>
<FieldVal>xx</FieldVal> <!-- if field order is 3, map 6th element -->
<FieldOrd>3</FieldOrd>
<Info>
<Info>
<Action>A</Action> <!-- 4th element in the row-->
<org>G190</org> <!-- 2nd element-->
<code>11FCK1GR002611XX</code>
<Fieldlab>Acc4</Fieldlab>
<FieldVal>02</FieldVal> <!-- if field order is 4, map 7th element -->
<FieldOrd>4</FieldOrd>
<Info>
<Info>
<Action>A</Action> <!-- 4th element in the row-->
<org>G190</org> <!-- 2nd element-->
<code>11FCK1GR002611XX</code>
<Fieldlab>Acc5</Fieldlab>
<FieldVal>FPB - OK Ship Pt</FieldVal> <!-- if field order is 5, map 3rd element -->
<FieldOrd>5</FieldOrd>
<Info>
</Account>
<Account>
<Action>A</Action> <!-- 3rd element in the row-->
<org>G980</org> <!-- 2nd element-->
<code>111FCA1GR003411XX</code> <!--concat(1st element, 4th element, 5th element)-->
<FiscalYear>2013</FiscalYear>
<Info>
<Action>A</Action> <!-- 3rd element in the row-->
<org>G190</org> <!-- 2nd element-->
<code>111FCA1GR003411XX</code><!--concat(1st element, 4th element, 5th element)-->
<Fieldlab>Acc1</Fieldlab>
<FieldVal>11FCA1GR0034</FieldVal> <!-- if field order is 1, map 1st element -->
<FieldOrd>1</FieldOrd>
<Info>
<Info>
<Action>A</Action>
<org>G190</org>
<code>111FCA1GR003411XX</code>
<Fieldlab>Acc2</Fieldlab>
<FieldVal>11</FieldVal> <!-- if field order is 2, map 5th element -->
<FieldOrd>2</FieldOrd>
<Info>
<Info>
<Action>A</Action> <!-- 4th element in the row-->
<org>G190</org> <!-- 2nd element-->
<code>111FCA1GR003411XX</code>
<Fieldlab>Acc3</Fieldlab>
<FieldVal>xx</FieldVal> <!-- if field order is 3, map 6th element -->
<FieldOrd>3</FieldOrd>
<Info>
<Info>
<Action>A</Action> <!-- 4th element in the row-->
<org>G190</org> <!-- 2nd element-->
<code>111FCA1GR003411XX</code>
<Fieldlab>Acc4</Fieldlab>
<FieldVal>02</FieldVal> <!-- if field order is 4, map 7th element -->
<FieldOrd>4</FieldOrd>
<Info>
<Info>
<Action>A</Action> <!-- 4th element in the row-->
<org>G190</org> <!-- 2nd element-->
<code>111FCA1GR003411XX</code>
<Fieldlab>Acc5</Fieldlab>
<FieldVal>FPB -San Antonio</FieldVal> <!-- if field order is 5, map 3rd element -->
<FieldOrd>5</FieldOrd>
<Info>
</Account>
任何人可以幫我生成此文件。提前致謝!
雖然XSLT 2可以讀取非XML輸入,但您可能會發現使用腳本語言(如Python,Perl或Groovy)可以更輕鬆地完成此操作。 –
如果你想避免學習新的技巧,那可能是真的,但Python(等)代碼不可能比下面顯示的XSLT代碼更簡單。 –
@MichaelKay在一個更合適的語言中編碼並不一定是正確的,我認爲在W3C認爲XSLT是「一種語言」時,建議使用XSLT進行某些操作(字符串處理)是不明智的轉換XML文檔「不是通用文件。 – 2013-06-25 02:01:45