2017-04-12 27 views
0

我有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>  

需要你的幫助......

+1

你給我們提出了要求,但如果您有任何問題,你還沒有告訴我們你已經嘗試什麼,或者。沒有實際的問題。這看起來更像是一個[「你是不是應該使用代碼?」](http://english.stackexchange.com/a/13235)請求。 –

+1

@Daniel:添加了我用來開始轉換的XSLT –

+0

「* XSLT應該足夠通用以適用於任何格式的XML文檔*」沒有這種東西。 XML非常靈活,一個XSLT不可能處理任何XML模式 - 除非處理完全沒有意義(例如標識轉換)。 –

回答

0

這裏ID可以自動生成唯一編號..

我想用generate-id()來得到一個唯一的ID。

所有的父標籤Xpath誰沒有任何價值將進入 <title>標籤。

根據你的目標輸出,它看起來像你想要的祖先除了第一個。

有價值的元素進入<text>標籤。

只使用父母的name()

實際值進入<answer>標籤內。

看起來你已經覆蓋了。

實施例...

XSLT 1。0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:strip-space elements= "*"/> 

    <xsl:template match="/"> 
    <items> 
     <xsl:apply-templates/> 
    </items> 
    </xsl:template> 

    <xsl:template match="text()"> 
    <item> 
     <id><xsl:value-of select="generate-id()"/></id> 
     <title> 
     <xsl:for-each select="ancestor::*[position()>1]"> 
      <xsl:value-of select="concat('/',name())"/> 
      <xsl:if test="(preceding-sibling::*| 
      following-sibling::*)[name()=name(current())]"> 
      <xsl:value-of select="concat('[', 
       count(preceding-sibling::*[name()=name(current())])+1, 
       ']')"/> 
      </xsl:if> 
     </xsl:for-each> 
     <xsl:text>/</xsl:text> 
     </title> 
     <text><xsl:value-of select="name(..)"/></text> 
     <answer> 
     <xsl:value-of select="." /> 
     <xsl:apply-templates/> 
     </answer> 
    </item> 
    </xsl:template> 
</xsl:stylesheet> 

輸出

<items> 
    <item> 
     <id>d0t4</id> 
     <title>/Vehicle/Ordinary/</title> 
     <text>Car</text> 
     <answer>Honda City</answer> 
    </item> 
    <item> 
     <id>d0t7</id> 
     <title>/Vehicle/Luxury[1]/</title> 
     <text>Car</text> 
     <answer>Volkswagon</answer> 
    </item> 
    <item> 
     <id>d0t10</id> 
     <title>/Vehicle/Luxury[2]/</title> 
     <text>Car</text> 
     <answer>Dzire</answer> 
    </item> 
</items> 
+0

OP提到希望''是一個獨特的自動生成_number_; 'generate-id()'不能保證這一點(如你的輸出演示)。一種可能性是對前車輛節點進行計數:'' – ABach

+1

@ABach - 確實,它不能保證是一個數字,但它保證是獨一無二的,前面的兄弟姐妹不能保證。 –

+0

有沒有什麼辦法可以爲ID生成唯一的有序序列號? 我不想要id的任何字符。 –