I want to add an element to source xml.
Example:
Source
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<DataArea>
<PurchaseOrder>
<PurchaseOrderLine>
<DocumentReference type="customersReference1">
<DocumentID>
<ID>23423</ID>
</DocumentID>
</DocumentReference>
<Item>
<CustomerItemID>
<!-- ArtNr -->
<ID>444</ID>
</CustomerItemID>
</Item>
<Quantity unitCode="PCE">17.3</Quantity>
</PurchaseOrderLine>
</PurchaseOrder>
</DataArea>
I want to add element
<LineNumber>10</LineNumber>
to
DataArea/PurchaseOrder/PurchaseOrderLine/
So first solution will be copying all data from original xml and then LineNumber like
<xsl:copy>
<xsl:apply-templates select="DocumentReference"/>
<xsl:apply-templates select="Item"/>
<xsl:apply-templates select="Quantity"/>
<!-- ADD HERE LINENUMBER -->
</xsl:copy>
How can I add LineNumber without manually copying all elements?
This is straight-forward, just by adding an extra matching template to the standard XSLT Identity Transform
This just copies the element, and all its children, but adds the new LineNumber element too.
Here is the full XSLT
When applied to your XML, the following is output