0

I am trying to write XSLT file for following input XML to output XML, is it possible XSLT to convert the value of input xml as node in output XML? how can I implement this?

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

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

2 Answers 2

1

You could certainly use 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>

What would be greater problem is a structure of output because it is not easy to decide which rows should be nested, which rows should transform into an attribute rather than element etc.

Sign up to request clarification or add additional context in comments.

Comments

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.