XML to XML using XSLT remove duplicates - xslt

Below is an input xml file:
<assets>
<item>
<file_name>file123</file_name>
<description>testing</description>
<created>date</created>
<metadata>
<guest>name</guest>
<webinfo>test</webinfo>
<albumorder>3</albumorder>
<albumorder>3</albumorder>
</metadata>
</item>
</assets>
From the above xml metadata/albumorder is having duplicate. I want to keep only one albumorder element. How to remove the duplicate elements.
Result file should be like:
<assets>
<item>
<file_name>file123</file_name>
<description>testing</description>
<created>date</created>
<metadata>
<guest>name</guest>
<webinfo>test</webinfo>
<albumorder>3</albumorder>
</metadata>
</item>
</assets>

The following copies only elements, which don't have a same named preceeding sibling element:
<?xml version="1.0" encoding="UTF-8"?>
<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:template match="*">
<xsl:if test="not(preceding-sibling::*[name(.) = name(current())])">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

There is pattern called "XSL identity template" or "XSL identity transformation", that will keep all XML except for rules you define to change certain elements.
kiwiwings already pointed out the solution, I would just integrate it in the identity template, otherwise you would loose XML comments, attributes, and processing instructions (none of them are present in your XML, but someone else may have them).
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="comment()|processing-instruction()|text()">
<xsl:copy/>
</xsl:template>
<xsl:template match="*">
<xsl:if test="not(preceding-sibling::*[name(.) = name(current())])">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:apply-templates select="comment()|processing-instruction()|text()|*"/>
</xsl:copy>
</xsl:if>
</xsl:template>

Related

XSLT to replace a namespace and also add a new (unused) namespace

I want to replace the namespace of the following XML Document
<?xml version="1.0" encoding="UTF-8"?>
<ns0:Document xmlns:ns0="http://mydata.com/H2H/Automation">
<CstmrCdtTrfInitn>
<GrpHdr>
</GrpHdr>
</CstmrCdtTrfInitn>
</ns0:Document>
with the following
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CstmrCdtTrfInitn>
<GrpHdr>
</GrpHdr>
</CstmrCdtTrfInitn>
</Document>
Any idea about XSLT which can convert this?
I have tried the following XSL, but it is adding the namespace with second Node and also not able to remove first namespace.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" indent="yes"/>
<xsl:template match="/*">
<xsl:element name="{local-name()}" namespace="http://www.w3.org/2001/XMLSchema-instance">
<xsl:copy-of select="./*" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Your requirement can be a bit tricky: replacing the default namespace of the Document element is straightforward. But adding the unused xslns:xsi namespace in XSLT-1.0 requires the EXSLT extension and a special technique explained by Michael Kay in reply to this question. It involves creating an unused element in a global variable whose namespace is then copied in the template replacing the default namespace. In XSLT-2.0 and above this would be easier (see below).
The EXSLT extension is not available in all XSLT-1.0 processors. But it is necessary to create a node-set from the variable.
So all namespaces are to be defined in the xsl:stylesheet element, and then the root element (here ns0:Document) is matched by a template and replaced with its local-name() part with the new default namespace added, followed by copying the "dummy" namespace of the element defined in the variable.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://mydata.com/H2H/Automation" xmlns:urn="urn:iso:std:iso" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ext="http://exslt.org/common">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<!-- identity template (except elements)-->
<xsl:template match="node()[not(self::*)]|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:variable name="nsXSI">
<xsl:element name="xsi:dummy" namespace="http://www.w3.org/2001/XMLSchema-instance" />
</xsl:variable>
<xsl:template match="ns0:*|*">
<xsl:element name="{local-name()}" namespace="urn:iso:std:iso">
<xsl:copy-of select="ext:node-set($nsXSI)/*/namespace::xsi" />
<xsl:apply-templates select="node() | #*" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output should be as expected, even in XSLT-1.0:
<Document xmlns="urn:iso:std:iso" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CstmrCdtTrfInitn>
<GrpHdr>
</GrpHdr>
</CstmrCdtTrfInitn>
</Document>
The simplified solution requires an XSLT-2.0 capable processor. Then you can use the xsl:namespace instruction as follows and don't need the "dummy" variable:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://mydata.com/H2H/Automation">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<!-- identity template (except elements)-->
<xsl:template match="node()[not(self::element())]|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:template match="ns0:*|*">
<xsl:element name="{local-name(.)}" namespace="urn:iso:std:iso">
<xsl:namespace name="xsi">http://www.w3.org/2001/XMLSchema-instance</xsl:namespace>
<xsl:apply-templates select="node() | #*" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
The output is the same.
The above XSLT-2.0 solution could be further simplified by using XSLT-3.0+'s xsl:mode to replace the identity template with
<xsl:mode on-no-match="shallow-copy"/>
Is there a reason why you cannot do simply:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://mydata.com/H2H/Automation"
exclude-result-prefixes="ns0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{local-name()}" namespace="urn:iso:std:iso">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="/ns0:Document">
<Document xmlns="urn:iso:std:iso" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:apply-templates/>
</Document>
</xsl:template>
</xsl:stylesheet>

XSL - Make all node values with namespace available in CDATA sections

I am looking for XSL to transform provided input to expected output.I have just provided sample but actual input xml had more than 1000 nodes and as too many nodes not able to use CDATA section in XSL, could you please help.
Input:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Output:
<note>
<to><![CDATA[Tove]]></to>
<from><![CDATA[Jani]]></from>
<heading><![CDATA[Reminder]]></heading>
<body><![CDATA[Don't forget me this weekend!]]></body>
</note>
You can achieve this by using the cdata-section-elements attribute of the xsl:output element which specifies all the elements that should be output in CDATA sections.
So use the following XSLT-1.0:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" cdata-section-elements="to from heading body" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
<!-- Identity template -->
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
See that the cdata-section-elements denotes the elements to from heading body to enclose their content in a CDATA section. The identity template just copies all of the file with regard to this.
If your elements are in a namespace, you have to prefix the element's names in the cdata-section-elements with the appropriate namespace-prefix.
For example, if you have the following XML with a namespace on the root element, all children nodes are in that namespace, too.
<?xml version="1.0" encoding="utf-8"?>
<Bank xmlns="http://xxyy.x.com" Operation="Create">
<Customer type="random">
<CustomerId>Id10</CustomerId>
<CountryCode>CountryCode19</CountryCode>
<LanguageCode>LanguageCode20</LanguageCode>
<AddressArray>
<Address type="primary">
<StreetAddress>179 Alfred St</StreetAddress>
<City>Fortitude Valley</City>
<County>GR</County>
<Country>India</Country>
</Address>
</AddressArray>
</Customer>
</Bank>
Use this XSLT (pay attention to the namespace declaration on the xsl:stylesheet element):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:ns0="http://xxyy.x.com">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" cdata-section-elements="ns0:CustomerId ns0:CountryCode ns0:LanguageCode ns0:StreetAddress ns0:City ns0:County ns0:Country" omit-xml-declaration="yes" />
<!-- Identity template -->
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Make sure that the namespace of the XSLT matches the namespace of the XML, here both are http://xxyy.x.com, but in your sample XML it is xxyy.x.com.
EDIT 2:
If you have a large amount of elements you can either add them all to cdata-section-elements (maybe by constructing it by another stylesheet) or use the solution I found here: Wrapping all elements in CDATA. It is kind of a hack.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
<xsl:variable name="CDATABegin" select="'<![CDATA['" />
<xsl:variable name="CDATAEnd" select="']]>'" />
<!-- Identity template -->
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:template match="text()[normalize-space()]">
<xsl:value-of select="$CDATABegin" disable-output-escaping="yes"/>
<xsl:value-of select="." disable-output-escaping="yes"/>
<xsl:value-of select="$CDATAEnd" disable-output-escaping="yes"/>
</xsl:template>
</xsl:stylesheet>
This wraps all non-empty text() nodes in CDATA sections. But here, too, you'd have to mention all elements in template matching rules containing the CDATA wrapping code.
Which variant would be easier to apply depends on the greater scenario.

XSLT fn:normalize-space() deletes all xml-tags

I'm a novice when it comes to XSLT and I've tried to find the information but not really sure what to search after.
I'm trying to remove the whitespace before and after the value in each node with the fn-normalize-space() function. It works, but also removes all XML tags.
What am I doing wrong?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" indent="yes" method="xml" omit-xml-declaration="no"/>
<xsl:template match="/">
<root>
<order>
<customer> The Company </customer>
</order>
</root>
</xsl:template>
</xsl:stylesheet>
What I want to achieve here is to remove the whitespace before and after the value in the <customer> tag, but at the same time preserve the XML tags.
You have to use it with an XPath as Martin Honnen described. This code should output an exact copy of your xml without whitespaces:
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
If you want to apply it on a specific XPath and not on all textnodes you would use it like:
<xsl:template match="nodename">
<xsl:value-of select="normalize-space(specificNodename)"/>
</xsl:template>

XSLT XPath, Need to change node value to based on number value of a sibling node

I am trying to change a node value to one of two different things by checking to see if the the following node contains an odd or even number.
This is the source XML.
<?xml version="1.0" encoding="UTF-8"?>
<FILE>
<CLAIM>
<PERSON>
<PROVIDER>
<HEADER>
<FLAG>PPON</FLAG>
<IDNO>11612</IDNO>
</HEADER>
</PROVIDER>
</PERSON>
</CLAIM>
</FILE>
And the XSLT I'm trying is:
<?xml version='1.0' ?>
<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"
omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="FLAG/node()">
<xsl:choose>
<xsl:when test="number(../IDNO) mod 2 = 0">EVEN</xsl:when>
<xsl:otherwise>ODD</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
And what I want to the output to be is
<?xml version="1.0" encoding="UTF-8"?>
<FILE>
<CLAIM>
<PERSON>
<PROVIDER>
<HEADER>
<FLAG>EVEN</FLAG>
<IDNO>11612</IDNO>
</HEADER>
</PROVIDER>
</PERSON>
</CLAIM>
</FILE>
I know I'm not getting the IDNO in my when test because my code gives me ODD all the time, also while debugging I tried putting the value of the when test into FLAG and I get NaN. But I can't come up with the correct syntax to get the IDNO into the test.
And yes, I'm new to XSLT, so maybe this is a dumb question, but I've tried many different things and searched this site and others for the answer with no luck.
Thanks in advance for your help.
DWL
You current template matches on a child of FLAG
<xsl:template match="FLAG/node()">
Now, when you use .. this is looking for the parent, so .. will match FLAG in this context, and so ../IDNO is looking for a child of FLAG called IDNO, which does not exist.
You need to go one more level up. Try this instead
<xsl:template match="FLAG/text()">
<xsl:choose>
<xsl:when test="number(../../IDNO) mod 2 = 0">EVEN</xsl:when>
<xsl:otherwise>ODD</xsl:otherwise>
</xsl:choose>
</xsl:template>
It might be easier to not check the axis of the node, but rather the node itself:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://www.mycompany.com/services/core/file" exclude-result-prefixes="a">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="IDNO">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:choose>
<xsl:when test="number(.) mod 2 = 0">EVEN</xsl:when>
<xsl:otherwise>ODD</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

XSLT: Adding a node!

How do I encapsulate nodes around my XML blocks using XSLT?
For example, I have the following XML file.
<?xml version="1.0" encoding="iso-8859-1"?>
<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" omit-xml-declaration="yes" />
<xsl:template match="/">
<Root>
<VOBaseCollection>
<xsl:apply-templates select="Root/Location" />
</VOBaseCollection>
</Root>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
My input XML file looks like this.
<Root>
<Location><Name>Pennsylvania</Name><Type>State</Type></Location>
</Root>
I wish the output to look like this.
<Root><Container>
<Location><Name>Pennsylvania</Name><Type>State</Type></Location>
</Container>
</Root>
I wish to make sure that a node called <CONTAINER> gets applied every time, it copies over information from Root/Location. What changes do I need to do to my XSLT file?
Summarizing all the answers in comments, this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="#*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Location">
<Container>
<xsl:call-template name="identity"/>
</Container>
</xsl:template>
</xsl:stylesheet>
Result:
<Root>
<Container>
<Location>
<Name>Pennsylvania</Name>
<Type>State</Type>
</Location>
</Container>
</Root>
I am just guessing, and in guess mode it seems that you want this:
EDIT: helped by another guess by Mads Hansen...
Add this to the identity template you already have:
<xsl:template match="Location">
<CONTAINER><xsl:apply-templates/></CONTAINER>
</xsl:template>