2013-09-26 73 views
0

我想为后面的输入XML编写XSLT文件来输出XML,是否有可能XSLT将输入XML的值转换为输出XML中的节点?我怎样才能实现这个?使用XSLT进行XML节点转换的XML值?

输入XML

<?xml version="1.0" encoding="UTF-8"?> 
<Rows> 
<Row><xml_data_name/> <xml_data_value/> </Row> 
<Row><xml_data_name>persons</xml_data_name> <xml_data_value/> </Row> 
<Row><xml_data_name>person</xml_data_name> <xml_data_value/> </Row> 
<Row><xml_data_name>username</xml_data_name> <xml_data_value>JS1</xml_data_value> </Row> 
<Row><xml_data_name>name</xml_data_name> <xml_data_value/> </Row> 
<Row><xml_data_name>name</xml_data_name> <xml_data_value>John</xml_data_value> </Row> 
<Row><xml_data_name>name</xml_data_name> <xml_data_value/> </Row> 
<Row><xml_data_name>family-name</xml_data_name> <xml_data_value/> </Row> 
<Row><xml_data_name>family-name</xml_data_name> <xml_data_value>Smith</xml_data_value> </Row> 
<Row><xml_data_name>family-name</xml_data_name> <xml_data_value/> </Row> 
<Row><xml_data_name>person</xml_data_name> <xml_data_value/> </Row> 
<Row><xml_data_name>person</xml_data_name> <xml_data_value/> </Row> 
<Row><xml_data_name>username</xml_data_name> <xml_data_value>MI1</xml_data_value> </Row> 
<Row><xml_data_name>name</xml_data_name> <xml_data_value/> </Row> 
<Row><xml_data_name>name</xml_data_name> <xml_data_value>Morka</xml_data_value> </Row> 
<Row><xml_data_name>name</xml_data_name> <xml_data_value/> </Row> 
<Row><xml_data_name>family-name</xml_data_name> <xml_data_value/> </Row> 
<Row><xml_data_name>family-name</xml_data_name> <xml_data_value>Ismincius</xml_data_value> </Row> 
<Row><xml_data_name>family-name</xml_data_name> <xml_data_value/> </Row> 
<Row><xml_data_name>person</xml_data_name> <xml_data_value/> </Row> 
<Row><xml_data_name>persons</xml_data_name> <xml_data_value/> </Row> 
<Row><xml_data_name/> <xml_data_value/> </Row> 
</Rows> 

输出XML

<?xml version="1.0" ?> 
<persons> 
    <person username="JS1"> 
    <name>John</name> 
    <family-name>Smith</family-name> 
    </person> 
    <person username="MI1"> 
    <name>Morka</name> 
    <family-name>Ismincius</family-name> 
    </person> 
</persons> 

回答

1

您当然可以使用xsl:元素像

<xsl:template match="Row"> 
    <!-- Note {} brackets in name attribute --> 
    <xsl:element name="{xml_data_name}"> 
     <xsl:value-of select="xml_data_value" /> 
    </xsl:element> 
</xsl:template> 

什么将是更大的问题是结构输出因此因为决定哪些行应该嵌套,哪些行应该转换成属性而不是元素等是不容易的。

0

那么,这是我见过的最奇怪的数据格式之一!你确定你无法得到任何产生这种结果的东西来产生更合理的东西吗?

我认为这个解决方案必须是递归的:你需要一个将一系列行作为输入的函数;它输出一个元素,其名称是序列中第一个元素的名称,没有数据值,其内容是通过递归调用获得的,递归调用将第一行之后的所有行一直传递到下一行,但没有数据值和同名,然后调用它自己来处理该行之后的所有行。不容易,而且肯定比我允许自己回答这些问题需要更多的时间!