将节点名称作为参数从 java 代码传递给 .xsl 文件,以从对应于其节点名称的 .xml 文件中获取值
passing a node name as an argument to .xsl file from java code to get value from .xml file corresponding to its node name
我已经编写了 java 代码来获取元数据,即来自 .xsd 文件的子节点,并将此元数据作为参数传递给 .xsl 文件,以便根据其在 .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 代码来获取元数据,即来自 .xsd 文件的子节点,并将此元数据作为参数传递给 .xsl 文件,以便根据其在 .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