我正在嘗試學習XSLT,但我最好以實例爲榜樣。我想爲模式轉換執行一個簡單的模式。我如何在一次通行證中執行此轉換(我目前的解決方案使用兩次通行證並失去了客戶的原始訂單)?如何在單個for-each中選擇多個節點XSLT
來源:
<?xml version="1.0" encoding="UTF-8"?>
<sampleroot>
<badcustomer>
<name>Donald</name>
<address>Hong Kong</address>
<age>72</age>
</badcustomer>
<goodcustomer>
<name>Jim</name>
<address>Wales</address>
<age>22</age>
</goodcustomer>
<goodcustomer>
<name>Albert</name>
<address>France</address>
<age>51</age>
</goodcustomer>
</sampleroot>
要:
<?xml version="1.0" encoding="UTF-8"?>
<records>
<record id="customer">
<name>Donald</name>
<address>Hong Kong</address>
<age>72</age>
<customertype>bad</customertype>
</record>
<record id="customer">
<name>Jim</name>
<address>Wales</address>
<age>22</age>
<customertype>good</customertype>
</record>
<record id="customer">
<name>Albert</name>
<address>France</address>
<age>51</age>
<customertype>good</customertype>
</record>
</records>
我已經這樣解決了壞方式(我失去客戶的訂單,我想我要解析的文件多次:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/sampleroot">
<records>
<xsl:for-each select="goodcustomer">
<record id="customer">
<name><xsl:value-of select="name" /></name>
<address><xsl:value-of select="address" /></address>
<age><xsl:value-of select="age" /></age>
<customertype>good</customertype>
</record>
</xsl:for-each>
<xsl:for-each select="badcustomer">
<record id="customer">
<name><xsl:value-of select="name" /></name>
<address><xsl:value-of select="address" /></address>
<age><xsl:value-of select="age" /></age>
<customertype>bad</customertype>
</record>
</xsl:for-each>
</records>
</xsl:template>
</xsl:stylesheet>
請有人幫助我正確的XSLT構造,我只需要使用一個單一的解析(每個只有一個)?
感謝,
克里斯
好問題Chris(+1)。看到我的答案是一個好的解決方案。 :) – 2010-05-05 17:42:21