Reputation: 17
I need to add an additional element at the end of the record. The siblings of the added element are only optional, which can be present or not. As long as the <Property>
element will be added at the end. There are 2 scenarios that can be done: 1st scenario, if <Property>
element is already present in the testfile, it should retain as is but I need to add another occurrence of <User>
element under the <Property>
element. 2nd scenario, if the element is not present I need to add this <Property>
at the end or it should be placed after all the elements of <Data>
. The other elements are only optional.
I need to add this additional part:
<Property>
<User>
<Value listID="AAA">Sample testing</Value>
</User>
</Property>
1st scenario example: <Property>
is present in the testfile
INPUT
<Record>
<Data>
<Date>2017-04-25</Date>
<Name>John Kledd</Name>
<Address>
<BuildingNumber>4603</BuildingNumber>
</Address>
<Property>
<User>
<Value listID="123">Example Only</Value>
</User>
</Property>
</Data>
</Record>
EXPECTED OUTPUT
<Record>
<Data>
<Date>2017-04-25</Date>
<Name>John Kledd</Name>
<Address>
<BuildingNumber>4603</BuildingNumber>
</Address>
<Property>
<User>
<Value listID="123">Example Only</Value>
</User>
<User>
<Value listID="AAA">Sample testing</Value>
</User>
</Property>
</Data>
</Record>
2nd scenario example: <Property>
is not present in the testfile
INPUT
<Record
<Data>
<Date>2017-04-25</Date>
<Name>John Kledd</Name>
<Address>
<BuildingNumber>4603</BuildingNumber>
</Address>
</Data>
</Record>
EXPECTED OUTPUT
<Record>
<Data>
<Date>2017-04-25</Date>
<Name>John Kledd</Name>
<Address>
<BuildingNumber>4603</BuildingNumber>
</Address>
<Property>
<User>
<Value listID="AAA">Sample testing</Value>
</User>
</Property>
</Data>
</Record>
Here is my XSLT I used for both scenarios:
XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- This part deletes the Property element -->
<xsl:template match="Data/Property"/>
<xsl:template match="Data/Address">
<xsl:copy-of select="."/>
<xsl:choose>
<xsl:when test="not(following-sibling::Property)">
<xsl:element name="Property">
<xsl:element name="User">
<xsl:element name="Value">
<xsl:attribute name="name">AAA</xsl:attribute>
<xsl:value-of select="'Sample Testing'"/>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="Property">
<xsl:element name="User">
<xsl:element name="Value">
<xsl:attribute name="name"><xsl:value-of select="../UserArea/Property/NameValue/@name"/></xsl:attribute>
<xsl:value-of select="../Property/User/Value"/>
</xsl:element>
</xsl:element>
<xsl:element name="User">
<xsl:element name="Value">
<xsl:attribute name="name">AAA</xsl:attribute>
<xsl:value-of select="'Sample Testing'"/>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
My XSLT is working for both scenarios, however, it didn't work if I removed the <Address>
element. I am using XSLT v2.0, apology if it is too long. Thank you for your help.
Regards,
Upvotes: 0
Views: 1919
Reputation: 117140
Couldn't you do simply:
XSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="default-user">
<User>
<Value listID="AAA">Sample testing</Value>
</User>
</xsl:variable>
<xsl:template match="Data[not(Property)]">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<Property>
<xsl:copy-of select="$default-user"/>
</Property>
</xsl:copy>
</xsl:template>
<xsl:template match="Property">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:copy-of select="$default-user"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0