使用XSLT将XML值转换为XML节点?

使用XSLT将XML值转换为XML节点?,xslt,Xslt,我正在尝试编写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</

我正在尝试编写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 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>

人
人
用户名JS1
名称
叫约翰
名称
姓
姓史密斯
姓
人
人
用户名MI1
名称
名字叫莫卡
名称
姓
姓伊斯明丘斯
姓
人
人
输出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 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>

约翰
史密斯
摩卡
伊斯曼纽斯

您当然可以使用xsl:element-like

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


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

好吧,这是我见过的最奇怪的数据格式之一!你确定你不能让这一切变得更合理吗

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