我编写了java代码来获取元数据,即从.xsd文件中获取子节点,并将该元数据作为参数传递给.xsl文件,以便根据.xml文件中的对应名称获取值,最后生成.csv格式的输出
XML文档
<personal>
<details>
<name>John</name>
<age>50</age>
<country>USA</country>
</details>
<details>
<name>Jams</name>
<age>40</age>
<country>UK</country>
</details>
</personal>
** 它的 xsd**
<xs:element minOccurs="0" maxOccurs="unbounded" name="details">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string" />
<xs:element minOccurs="0" name="age" type="xs:string" />
<xs:element minOccurs="0" name="country" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
XSLT 样式表
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="Elements"/>
<xsl:template match="/">
<xsl:for-each select="//details">
<xsl:value-of select="$Elements"/>
<xsl:value-of select="'
'"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Java代码
public static void main(String args[]) 抛出异常 {
//Read the XML data file and code of stylesheet
File stylesheet = new File("src/style.xsl");
File xmlSource = new File("src/data.xml");
//It enables appln to obtain parser to produces DOM object from XML documents
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xmlSource);
Document doc = builder.parse (new File("src/structure.xsd"));
NodeList list = doc.getElementsByTagName("xs:element");
String cols="";
//loop to print data
for(int i = 0 ; i < list.getLength(); i++){
Element first = (Element)list.item(i);
if(first.hasAttributes() && first.getAttributeNode("type") != null){
String nm = first.getAttribute("name");
cols=cols+nm+",";
}
}
cols=cols.substring(0, cols.length()-1);
System.out.println(cols);
//It process XML data into required format by reading .xsl file.
StreamSource stylesource = new StreamSource(stylesheet);
Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource);
transformer.setParameter("Elements", cols);
//Object that implements this interface contains the information needed to act as source input (XML source or transformation instructions).
Source source = new DOMSource(document);
//Directory in which file need to be save after transformation from xml to csv
Result outputTarget = new StreamResult(new File("C:/Users/Desktop/a.csv"));
//Transforming
transformer.transform(source, outputTarget);
}
实际输出
我得到的 .csv 输出为
name,age,country
name,age,country
预期输出为
John,50,USA
Jams,40,UK
最佳答案
使用此转换(我已为全局参数 $Elements
指定了默认值,但每次从 Java 代码调用转换时都会覆盖该值):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:param name="Elements" select="'name,age,country'"/>
<xsl:template match="details">
<xsl:apply-templates select="
*[contains(concat(',',$Elements,','),
concat(',',name(),',')
)]"/>
<xsl:value-of select="'
'"/>
</xsl:template>
<xsl:template match="details/*">
<xsl:if test="not(position()=1)">,</xsl:if>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档时:
<personal>
<details>
<name>John</name>
<age>50</age>
<country>USA</country>
</details>
<details>
<name>Jams</name>
<age>40</age>
<country>UK</country>
</details>
</personal>
产生了想要的正确结果:
John,50,USA
Jams,40,UK
关于java - 将节点名称作为参数从 java 代码传递给 .xsl 文件,以从与其节点名称对应的 .xml 文件获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37338864/