2015-09-08 90 views
-1

我使用WSO2 ESB 4.8.1,並且我想將JSON轉換爲XML,但默認轉換對我來說還不夠。我有這個輸入(變換)XML:使用XSLT將XML標記添加到XML

<?xml version="1.0" encoding="UTF-8"?> 
       <locations> 
    <location> 
     <id>7eaf7</id> 
     <name>Biaggio Cafe</name> 
     <tags>bar,restaurant,food,establishment</tags> 
    </location> 
    <location> 
     <id>3ef98</id> 
     <name>Doltone House</name> 
     <tags>food,establishment</tags> 
    </location> 
     </locations> 

我想要得到這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
    <Entries items="2"> 
    <Item idx = "0"> 
     <id>7eaf7</id> 
      <name>Biaggio Cafe</name> 
      <tags>bar,restaurant,food,establishment</tags> 
    </Item> 
    <Item idx = "1"> 
      <id>3ef98</id> 
      <name>Doltone House</name> 
      <tags>food,establishment</tags> 
    </Item> 
    </Entries> 

是否有可能使用XSLT來做到這一點?也許還有其他方法可以做到這一點?

+4

這可以用XSLT很容易做到。你有什麼嘗試? – f1sh

回答

1

下面也可以使用。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="locations"> 
    <Entries items="{count(location)}"> 
     <xsl:for-each select="location"> 
     <Item idx="{position() - 1}"> 
      <id> 
      <xsl:value-of select="id"/> 
      </id> 
      <name> 
      <xsl:value-of select="name"/> 
      </name> 
      <tags> 
      <xsl:value-of select="tags"/> 
      </tags> 
     </Item> 
     </xsl:for-each> 
    </Entries> 
    </xsl:template> 
</xsl:stylesheet> 
0

試試這個。我沒有測試過它,但它應該(接近)你所需要的。

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

<xsl:template match="locations"> 
    <xsl:element name="Entries"> 
    <xsl:attribute name="items"> 
     <xsl:value-of select="count(location)"/> 
    </xsl:attribute> 
    <xsl:for-each select="location"> 
     <xsl:element name="Item"> 
     <xsl:attribute name="idx"> 
      <xsl:value-of select="position()"/> 
     </xsl:attribute> 
     <id><xsl:value-of select="id"/></id> 
     <name><xsl:value-of select="name"/></name> 
     <tags><xsl:value-of select="tags"/></tags> 
     </xsl:element> 
    </xsl:for-each> 
    </xsl:element> 
</xsl:template> 
+1

爲什麼你使用'id/.'? '.'表示當前節點,所以在這裏沒有效果,只需寫'id'即可。你可以用' ... ...'替換你的'xsl:element'和'xsl:attribute',這很容易閱讀。您可以使用apply-templates替換for-each以減少嵌套。 – Abel

1

你可以使用xsl:copy

transformer.xsd

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()" /> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="locations"> 
     <Entries items="{count(location)}"> 
      <xsl:for-each select="location"> 
       <Item idx="{position() - 1}"> 
        <xsl:apply-templates select="@*|node()" /> 
       </Item> 
      </xsl:for-each> 
     </Entries> 
    </xsl:template> 
</xsl:stylesheet> 

import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerConfigurationException; 
import javax.xml.transform.TransformerException; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.stream.StreamResult; 
import javax.xml.transform.stream.StreamSource; 

public class Xslt { 

    public static void main(String[] args) { 
     String dataXML = "data.xml"; 
     String inputXSL = "transformer.xsd"; 
     String outputXML = "out.xml"; 

     Xslt st = new Xslt(); 
     try { 
      st.transform(dataXML, inputXSL, outputXML); 
     } catch (TransformerConfigurationException e) { 
      System.err.println("TransformerConfigurationException"); 
      System.err.println(e); 
     } catch (TransformerException e) { 
      System.err.println("TransformerException"); 
      System.err.println(e); 
     } 
    } 

    public void transform(String dataXML, String inputXSL, String outputXML) 
      throws TransformerConfigurationException, TransformerException { 

     TransformerFactory factory = TransformerFactory.newInstance(); 
     StreamSource xslStream = new StreamSource(inputXSL); 
     Transformer transformer = factory.newTransformer(xslStream); 
     StreamSource in = new StreamSource(dataXML); 
     StreamResult out = new StreamResult(outputXML); 
     transformer.transform(in, out); 
     System.out.println("The generated XML file is:" + outputXML); 
    } 
} 

Data.xm升

<?xml version="1.0" encoding="UTF-8"?> 
<locations> 
    <location> 
     <id>7eaf7</id> 
     <name>Biaggio Cafe</name> 
     <tags>bar,restaurant,food,establishment</tags> 
    </location> 
    <location> 
     <id>3ef98</id> 
     <name>Doltone House</name> 
     <tags>food,establishment</tags> 
    </location> 
</locations> 

out.xml

<?xml version="1.0" encoding="UTF-8"?> 
<Entries items="2"> 
    <Item idx="0"> 
     <id>7eaf7</id> 
     <name>Biaggio Cafe</name> 
     <tags>bar,restaurant,food,establishment</tags> 
    </Item> 
    <Item idx="1"> 
     <id>3ef98</id> 
     <name>Doltone House</name> 
     <tags>food,establishment</tags> 
    </Item> 
</Entries> 
+0

同樣,在這裏,您可以替換'xsl:attribute',並使用' ...和' ......,效果相同,但(更)更具可讀性。您可以使用apply-templates和匹配的模板替換for-each,以減少嵌套。 – Abel

+0

謝謝阿貝爾。編輯我的答案:) –

+0

Thx,這是我需要的。 – Kacu