I'm wondering if/how the following can be accomplished in XSLT. If not, what would you use? (I used OmniMark, but I would like to know if this is possible in XSLT.)
Here's an example of the input XML:
<?xml version="1.0" encoding="UTF-8"?>
<fragment>
<firstElem>
<secondElem>D12</secondElem>
</firstElem>
<firstElem>
<secondElem>A3J7-10</secondElem>
</firstElem>
<firstElem>
<secondElem>C2-4</secondElem>
</firstElem>
<firstElem>
<secondElem>QW9R7NP20-25</secondElem>
</firstElem>
</fragment>
What I needed to do was take the text content of the secondElem
element and create as many new firstElem
elements as required. The '-' was a "through", so 'A3J7-10' was really 'A3J7' through 'A3J10' (A3J7, A3J8, A3J9, A3J10). (Sometimes the "through" would be fairly large, like A1B2C1-150 (A1B2C1 through A1B2C150).)
If there was no dash, nothing needed to be done.
Here's an example of the output XML:
<?xml version="1.0" encoding="UTF-8"?>
<fragment>
<firstElem>
<secondElem>D12</secondElem>
</firstElem>
<firstElem>
<secondElem>A3J7</secondElem>
</firstElem>
<firstElem>
<secondElem>A3J8</secondElem>
</firstElem>
<firstElem>
<secondElem>A3J9</secondElem>
</firstElem>
<firstElem>
<secondElem>A3J10</secondElem>
</firstElem>
<firstElem>
<secondElem>C2</secondElem>
</firstElem>
<firstElem>
<secondElem>C3</secondElem>
</firstElem>
<firstElem>
<secondElem>C4</secondElem>
</firstElem>
<firstElem>
<secondElem>QW9R7NP20</secondElem>
</firstElem>
<firstElem>
<secondElem>QW9R7NP21</secondElem>
</firstElem>
<firstElem>
<secondElem>QW9R7NP22</secondElem>
</firstElem>
<firstElem>
<secondElem>QW9R7NP23</secondElem>
</firstElem>
<firstElem>
<secondElem>QW9R7NP24</secondElem>
</firstElem>
<firstElem>
<secondElem>QW9R7NP25</secondElem>
</firstElem>
</fragment>
Is there a way to do this in XSLT?
Thanks!
Here is an XSLT 2.0 solution:
When the above transformation is applied on the provided XML document:
the wanted, correct result is produced:
Do note:
The use of regular expressions.
Sub-expression capturing and the
regex-group()
function.The use of
<xsl:analyze-string>
and<xsl:matching-substring>
.The use of the
to
operator to create the sequence to be used in<xsl:for-each>
The use of
<xsl:for-each>
on a sequence of non-nodes (integers).This stylesheet:
Output: