使用 XSLT 将 XML 标签添加到 XML

Add XML tags into XML with XSLT

我使用 WSO2 ESB 4.8.1,我想将 JSON 转换为 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 执行此操作?也许还有其他方法可以做到这一点?

试试这个。我还没有测试过,但它应该(接近)你所需要的。

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

你可以使用 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.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> 

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>

以下也可以使用

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