我有XML輸入,XML定製改造使用XSLT
<?xml version="1.0" encoding="UTF-8"?>
<Vehicle>
<Ordinary>
<Car>Honda City</Car>
</Ordinary>
<Luxury>
<Car>Volkswagon</Car>
</Luxury>
<Luxury>
<Car>Dzire</Car>
</Luxury>
我不得不改變使用XSLT到這個XML消息,
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<id>1</id>
<title>/Vehicle/Ordinary/</title>
<text>Car</text>
<answer>Honda City</answer>
</item>
<item>
<id>2</id>
<title>/Vehicle/Luxury[1]/</title>
<text>Car</text>
<answer>Volkswagon</answer>
</item>
<item>
<id>3</id>
<title>/Vehicle/Luxury[2]/</title>
<text>Car</text>
<answer>Dzire</answer>
</item>
- 這裏ID可以自動生成唯一編號..
- 所有父標籤Xpath誰沒有任何價值將進入<標題>標籤。
- 有價值的元素進來<文本>標記。
- 實際值進入<答案>標記。
- XSLT應該足夠通用,以適用於給定示例xml不特定的任何格式的XML文檔&。
我已經開始用下面,
<?xml version="1.0" encoding="windows-1252" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:strip-space elements = "*" />
<xsl:template match="/">
<items>
<xsl:apply-templates/>
</items>
</xsl:template>
<xsl:template match="text()">
<item>
<title>
<xsl:for-each select="ancestor-or-self::*">
<xsl:text>/</xsl:text>
<xsl:value-of select="name()" />
</xsl:for-each>
</title>
<answer>
<xsl:value-of select="." />
<xsl:apply-templates/>
</answer>
</item>
</xsl:template>
</xsl:stylesheet>
它產生以下輸出,
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<id>1</id>
<title>/Vehicle/Ordinary/Car</title>
<answer>Honda City</answer>
</item>
<item>
<id>2</id>
<title>/Vehicle/Luxury[1]/Car</title>
<answer>Volkswagon</answer>
</item>
<item>
<id>2</id>
<title>/Vehicle/Luxury[2]/Car</title>
<answer>Dzire</answer>
</item>
</items>
需要你的幫助......
你給我們提出了要求,但如果您有任何問題,你還沒有告訴我們你已經嘗試什麼,或者。沒有實際的問題。這看起來更像是一個[「你是不是應該使用代碼?」](http://english.stackexchange.com/a/13235)請求。 –
@Daniel:添加了我用來開始轉換的XSLT –
「* XSLT應該足夠通用以適用於任何格式的XML文檔*」沒有這種東西。 XML非常靈活,一個XSLT不可能處理任何XML模式 - 除非處理完全沒有意義(例如標識轉換)。 –