我有一個XML這樣的:輸出值XSLT/XPath 2.0中
<?xml version="1.0" encoding="UTF-8"?>
<Section>
<Chapter>
<Cell colname="1">
<Value>A</Value>
</Cell>
<Cell colname="2">
<MyValue>AAA</MyValue>
<MyValue>BBB</MyValue>
</Cell>
<Cell colname="3">
<MyCar>Honda</MyCar>
</Cell>
</Chapter>
<Chapter>
<Cell colname="1">
<Value>C</Value>
</Cell>
<Cell colname="2">
<MyValue>CCC</MyValue>
</Cell>
<Cell colname="3">
<MyCar>Toyota</MyCar>
</Cell>
</Chapter>
</Section>
我喜歡有一個消息(以後將它們轉換代碼)像這樣輸出:
甲 AAA 本田 一個 BBB 本田 ç CCC 豐田
這是我的XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="Section//Chapter"/>
</xsl:template>
<xsl:template match="Chapter">
<xsl:for-each select="Cell[@colname='2']//MyValue">
<xsl:message>
<xsl:value-of select="Cell[@colname='1']/Value"/>
<xsl:value-of select="."/>
<xsl:value-of select="Cell[@colname='3']/MyCar"/>
</xsl:message>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
不幸的是,它不會輸出我想要它做的:(。
我意識到,每一個都會改變上下文,所以餘下的值不會做任何事情。
這是什麼解決方案?
TIA,
約翰
Dimitre:謝謝你,比較你的解決方案與Lukasz的他們是如此的不同,因爲我還在學習你會介意解釋你的方法嗎?再次感謝!。 – JohnX 2012-02-26 00:10:00
@JohnX:當然,我會在接下來的10分鐘內編輯我的答案。 – 2012-02-26 00:12:20
@JohnX:已添加說明。 – 2012-02-26 00:25:42