I'm new to XSLT. I've been learning quite a bit over the last month. I'm trying to create an XSLT that looks at a particular node of the source document and inserts a variable into that node. Basically I'm taking 2 XML documents and generating a new document.
The problem is the node of in interest in the source document has 42 optional elements. I need to insert this variable into a specific location based on the schema of the source document. For example the source document is structured like so:
Code:
<Top>
<Element>
<ChildElement>
<OPTIONAL_1> Data </OPTIONAL_1>
<OPTIONAL_2> Data </OPTIONAL_2>
<OPTIONAL_3> Data </OPTIONAL_3>
..............................
..............................
<OPTIONAL_42> Data </OPTIONAL_42>
</ChildElement>
</Element>
</Top>
The variable that I will be inserting is simply another XML document. It looks something like this:
Code:
<xsl:variable name="VAR">
<XML_Top>
<XML_Data1> DATA 1 </XML_Data1>
<XML_Data2> DATA 2 </XML_Data2>
<XML_Data2> DATA 2 </XML_Data3>
</XML_Top>
</xsl:variable>
This variable needs to be inserted as the 30th optional element in the source document. (Or the correct relative location based on which optional elements are present). So the results should look like this:
Code:
<Top>
<Element>
<ChildElement>
<OPTIONAL_1> Data </OPTIONAL_1>
<OPTIONAL_2> Data </OPTIONAL_2>
<OPTIONAL_3> Data </OPTIONAL_3>
..............................
..............................
<OPTIONAL_29> Data </OPTIONAL_29>
<XML_Top>
<XML_Data1> DATA 1 </XML_Data1>
<XML_Data2> DATA 2 </XML_Data2>
<XML_Data2> DATA 2 </XML_Data3>
</XML_Top>
<OPTIONAL_31> Data </OPTIONAL_31>
..........................
..........................
<OPTIONAL_42> Data </OPTIONAL_42>
</ChildElement>
</Element>
</Top>
For any given instance of the source document, there could be any number of those optional elements. Based on the schema, this variable can only be in a specific location in the source document. If all of the elements were required, this is very simple to do. But since all of them are optional, I do not know how to locate where the variable should be inserted. All the other elements/attributes and data in the source document must be maintained in the output. I'm on a windows platform and this will eventually run on a Saxon XSLT 2.0 compatible processor. Any help would be appreciated!