0

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

2 回答 2

1

你当然可以使用 xsl:element 之类的

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

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

于 2013-09-26T09:06:29.477 回答
0

Well, that's one of the weirdest data formats I've ever seen! Are you sure you can't get whatever produced this to produce something more reasonable?

I think the solution has to be recursion: you want a function that takes a sequence of rows as input; it outputs an element whose name is the name of the first element in the sequence with no data value and whose content is obtained by a recursive call that passes all rows after that first row up to the next row with no data value and the same name, then calls itself to process all rows after that row. Not easy, and certainly takes more time than I allow myself for answering SO questions!

于 2013-09-26T09:32:08.920 回答