Insert XML element using XSLT in exisiting node - xslt

I have the following XML document:
<root someAttribute="someValue" />
Now I want to add a tag using XSLT, so that the document will look like this:
<root someAttribute="someValue">
<item>TEXT</item>
</root>
If I repeat to use the XSLT once more it should just add another item:
<root someAttribute="someValue">
<item>TEXT</item>
<item>TEXT</item>
</root>
It sound's so easy, does it not? Here is the best I got after trying a ton of things:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
<xsl:param name="message" />
<xsl:output method="xml" encoding="utf-8"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:copy-of select="*"/>
<item>
<xsl:value-of select="$message" />
</item>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
It does /nearly/ what I have asked for, except that it "forgets" the attributes of the root element. I have found a number of other solutions here on stackoverflow and elsewhere that have in common with my solution that they loose the attributes of the root element. How can I fix that?

You're currently transforming only child nodes, not attributes.
<xsl:template match="root">
<xsl:copy>
<xsl:copy-of select="node()|#*"/> <!-- now does attrs too -->
<item>
<xsl:value-of select="$message" />
</item>
</xsl:copy>
</xsl:template>

Related

XSL how to exclude some children in output?

Beginner XSL question..and I know there are similar questions and answers posted, but I can't figure out how to apply them to my XSLT...
My source XML looks like (this is just a fragment of a much larger XML file)
<?xml version="1.0" encoding="UTF-8"?>
<COLLECTION><Release NAME="Release" TYPE="Unknown" STATUS="0">
<Transaction>
<TransactionNumber>4</TransactionNumber>
<ReleaseNumber>4</ReleaseNumber>
<PrimaryObjectID>OR:wt.part.WTPart:121581:416986630-1502721046884-982634822-1-0-0-127#ODIGettingStarted.tri.co.uk</PrimaryObjectID>
<CreatedBy>orgadmin</CreatedBy>
<CreatedDate>2017-09-27 08:34:31 EDT</CreatedDate>
<Locale>en_US</Locale>
<Destination>CRP1</Destination>
</Transaction>
</Release>
I want to exclude the Locale and Destination nodes from the output.
My complete solution will be more complex that requires me to split the XML into three, hence my use of but the relevant code I have so far is :-
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "2.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:param name ="outputFileDir" select="'file:///D:/workspace/TPHMOT_xsl/TPHMOT_xsl/xsl_output'"/>
<xsl:template match ="/">
<xsl:result-document href="{$outputFileDir}/ESI_ItemMasters_1.xml" method="xml" indent="yes">
<COLLECTION>
<xsl:apply-templates select="COLLECTION/Release"/>
</COLLECTION>
</xsl:result-document>
<xsl:result-document href="{$outputFileDir}/ESI_ConfigurableItem_1.xml" method="xml" indent="yes">
<COLLECTION>
<xsl:apply-templates select="COLLECTION/Release"/>
</COLLECTION>
</xsl:result-document>
<xsl:result-document href="{$outputFileDir}/ESI_GenericBOM_1.xml" method="xml" indent="yes">
<COLLECTION>
<xsl:apply-templates select="COLLECTION/Release"/>
</COLLECTION>
</xsl:result-document>
</xsl:template>
<xsl:template match="Release">
<xsl:copy-of select="self::node()"/>
</xsl:template>
</xsl:stylesheet>
This outputs
<?xml version="1.0" encoding="UTF-8"?>
<COLLECTION>
<Release NAME="Release" TYPE="Unknown" STATUS="0">
<Transaction>
<TransactionNumber>4</TransactionNumber>
<ReleaseNumber>4</ReleaseNumber>
<PrimaryObjectID>OR:wt.part.WTPart:121581:416986630-1502721046884-982634822-1-0-0-127#ODIGettingStarted.tri.co.uk</PrimaryObjectID>
<CreatedBy>orgadmin</CreatedBy>
<CreatedDate>2017-09-27 08:34:31 EDT</CreatedDate>
<Locale>en_US</Locale>
<Destination>CRP1</Destination>
</Transaction>
</Release>
</COLLECTION>
How can I adapt my XSL to exclude Locale and Destination child nodes?
Many thanks in advance for any help offered!
Instead of copying your complete element in
<xsl:template match="Release">
<xsl:copy-of select="self::node()"/>
</xsl:template>
you only need to use the identity transformation
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
and then empty template(s) to prevent the elements you don't want from being copied:
<xsl:template match="Locale | Destination"/>

Insert list of elements inside XML tags

I am trying to obtain a list of all the elements with values that aren't in the (Line 1, Line2), and then insert them into the tags similar to the test.
Right now I can retrieve all the elements, but I'm having trouble restricting this to just my desired values. And then I'm unsure how to match and do a for each on elements outside my match criteria. Any advice would be greatly appreciated!
Given the Following XML:
<?xml version="1.0" encoding="UTF-8"?>
<Request>
<Header>
<Line1>Element1</Line1>
<Line2>Element2</Line2>
</Header>
<ElementControl>
<Update>
<Element>test</Element>
</Update>
</ElementControl>
<Member>
<Identifier>123456789</Identifier>
<Contact>
<Person>
<Gender>MALE</Gender>
<Title>Mr</Title>
<Name>JOHN DOE</Name>
</Person>
<HomePhone/>
<eMailAddress/>
<ContactAddresses>
<Address>
<AddressType>POS</AddressType>
<Line1>100 Fake Street</Line1>
<Line2/>
<Line3/>
<Line4/>
<Suburb>Jupiter</Suburb>
<State>OTH</State>
<PostCode>9999</PostCode>
<Country>AUS</Country>
</Address>
</ContactAddresses>
</Contact>
</Member>
</Request>
Current XSL for getting elements
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()">
<xsl:for-each select="node()[text() != '']">
<xsl:value-of select="local-name()"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
<xsl:apply-templates select="node()"/>
</xsl:template>
</xsl:stylesheet>
My WIP xml for inserting the result xml tags is below. I'm unsure how to insert the results of the above xsl into this,
<xsl:template match="Element">
<xsl:copy-of select="."/>
<Element>Value1</Element>
</xsl:template>
And ultimate desired output:
<?xml version="1.0" encoding="UTF-8"?>
<Request>
<Header>
<Line1>Element1</Line1>
<Line2>Element2</Line2>
</Header>
<ElementControl>
<Update>
<Element>Identifier</Element>
<Element>Gender</Element>
<Element>Title</Element>
<Element>Name</Element>
<Element>AddressType</Element>
<Element>Line1</Element>
<Element>Suburb</Element>
<Element>State</Element>
<Element>PostCode</Element>
<Element>Country</Element>
</Update>
</ElementControl>
<Member>
<Identifier>123456789</Identifier>
<Contact>
<Person>
<Gender>MALE</Gender>
<Title>Mr</Title>
<Name>JOHN DOE</Name>
</Person>
<HomePhone/>
<eMailAddress/>
<ContactAddresses>
<Address>
<AddressType>POS</AddressType>
<Line1>100 Fake Street</Line1>
<Line2/>
<Line3/>
<Line4/>
<Suburb>Jupiter</Suburb>
<State>OTH</State>
<PostCode>9999</PostCode>
<Country>AUS</Country>
</Address>
</ContactAddresses>
</Contact>
</Member>
</Request>
I would change the current template to use mode attribute, so it is only used in specific cases, rather than matching all elements. You should also change it to output elements, not text, like so:
<xsl:template match="node()" mode="copy">
<xsl:for-each select=".//node()[text() != '']">
<Element>
<xsl:value-of select="local-name()"/>
</Element>
</xsl:for-each>
</xsl:template>
Then you can call it like this....
<xsl:template match="ElementControl/Update">
<xsl:apply-templates select="../../Member" mode="copy" />
</xsl:template>
Try this XSLT. Note the use of the identity template to copy all other existing elements unchanged
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()" mode="copy">
<xsl:for-each select=".//node()[text() != '']">
<Element>
<xsl:value-of select="local-name()"/>
</Element>
</xsl:for-each>
</xsl:template>
<xsl:template match="ElementControl/Update">
<xsl:apply-templates select="../../Member" mode="copy" />
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Remove parent node if child node is empty

I am trying to transform a given XML using xslt. The caveat is that I would have to delete a parent node if a given child node is not present. I did do some template matching, but I am stuck. Any help would be appreciated.
The input xml :
<?xml version="1.0" encoding="UTF-8"?>
<main>
<item>
<value>
<item>
<value>ABC</value>
<key>test1</key>
</item>
<item>
<value>XYZ</value>
<key>test2</key>
</item>
<item>
<value></value>
<key>test3</key>
</item>
</value>
</item>
<item>
<value />
<key>test4</key>
</item>
<item>
<value>PQR</value>
<key>test5</key>
</item>
</main>
Expected Output:
<?xml version="1.0" encoding="UTF-8"?>
<main>
<item>
<value>
<item>
<value>ABC</value>
<key>test1</key>
</item>
<item>
<value>XYZ</value>
<key>test2</key>
</item>
</value>
</item>
<item>
<value>PQR</value>
<key>test5</key>
</item>
</main>
The issue is if I use template matching e.g.
<xsl:template match="item[not(value)]"/> as mentioned in deleting the parent node if child node is not present in xml using xslt, then it completely removes everything as main/item/value is also empty.
What I need is remove if element is empty but only do if element has no child element.
You should first start with the XSLT identity template
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
Then, all you need is a template that matches the item element where all descendent leaf value elements are empty.
<xsl:template match="item[not(descendant::value[not(*)][normalize-space()])]" />
So, the template matches it, but doesn't output it.
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="item[not(descendant::value[not(*)][normalize-space()])]" />
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I think you want to remove the element of it has no children at all (whether those children be elements or text nodes). Try inserting this template:
<xsl:template match="item">
<xsl:if test="exists(value/node())">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:if>
</xsl:template>
If I read this correctly, you want to do:
XSLT 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"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item[not(value[node()])]"/>
</xsl:stylesheet>
This will remove any item that does not have a value child with some content.

Need to search for a node. if not present need to enter an empty node using xslt

The below is the Response for a Web Service.
<Envelope>
<Body>
<searchItemResponse>
<status>
<statusCode>Success</statusCode>
</status>
<itemList>
<itemWithWarehouses>
<item>
<originOfData>SME</originOfData>
<itemNumbers>
<shortNumber>115632</shortNumber>
<tssArticleNumber>PT0401450-T46N</tssArticleNumber>
<relatedItems>
<alternateItemsNumber>ORAR00428-N7083</alternateItemsNumber>
</relatedItems>
</item>
<warehouseItems>
.....................
</warehouseItems>
<warehouseItems>
.....................
</warehouseItems>
</itemWithWarehouses>
</itemList>
</searchItemResponse>
</Body>
</Envelope>
In some cases the below node is missing.
<relatedItems>
<alternateItemsNumber>ORAR00428-N7083</alternateItemsNumber>
</relatedItems>
I need to make sure if the node is not there, i need to insert the node like shown below
<relatedItems>
<alternateItemsNumber/>
</relatedItems>
I need an XSLT for this. Please help me. Thanks
You can use a template looking for any itemNumbers not having a relatedItems element and then add the empty elements:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" version="2.0" xpath-default-namespace="" exclude-result-prefixes="#all">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template match="itemNumbers[not(relatedItems)]">
<xsl:copy>
<xsl:apply-templates select="*"/>
<xsl:element name="relatedItems">
<xsl:element name="alternateItemsNumber"/>
</xsl:element>
</xsl:copy>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

XSLT Add nested element to the last node based on the existing element

I have the following XML, the XML has multiple occurance of 'item' elements, and there will be single occurance of 'info' element
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="http://temo.com/tempe.xsd">
<di>
<md>2013-07-09T09:43:00</md>
</di>
<list>
<item>
<Name>test</Name>
<section block1="true">
<block1>
<move>1</move>
<info>
<item1>test item 1</item1>
<item2>false</item2>
<item3>1</item3>
</info>
</block1>
<block2>
...
</block2>
</section>
</item>
</list>
<option>
...
</option>
</root>
and I want to convert it to the below format, i.e., if 'move' element is present then add new elements in the last position of 'info' element should be created
<item4>
<item5>1</item5>
</item4>
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="http://temo.com/tempe.xsd">
<di>
<md>2013-07-09T09:43:00</md>
</di>
<list>
<item>
<Name>test</Name>
<section block1="true">
<block1>
<move>1</move>
<info>
<item1>test item 1</item1>
<item2>false</item2>
<item3>1</item3>
<item4>
<item5>1</item5>
</item4>
</info>
</block1>
<block2>
...
</block2>
</section>
</item>
<item>
...
</item>
</list>
<option>
...
</option>
</root>
I am using the following XSLT to convert to the above format
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes" encoding="utf-8"/>
<xsl:template match="node()|#*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/info/*[position()=last()]">
<xsl:copy>
<xsl:choose>
<xsl:when test="/section/block/move">
<!--Add new element item4-->
<xsl:element name="item4">
<xsl:element name="item5">
<xsl:value-of select="section/block/move"/>
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="identity" />
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Could you please Help me to find the issue in the XSLT?
I am new to XSLT
Thank you :)
You have a default namespace declaration on the input XML and the output XML also needs that namespace for the elements to be inserted so you need
xmlns:tp="http://temo.com/tempe.xsd"
on your xsl:stylesheet root element, then you need to adapt your paths and patterns to use that prefix e.g. instead of info you need tp:info and instead of section/block/move you need tp:section/tp:block/tp:move and so on.
The paths in your XSLT sample seem wrong however you use /section/block/move but your root element is root so /section will never select anything, even if you add the namespace prefix. And your path has block, the input has block1.
Instead of trying to fix your code I wrote a new stylesheet, it is
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://temo.com/tempe.xsd"
xmlns:tp="http://temo.com/tempe.xsd"
exclude-result-prefixes="tp"
version="1.0">
<xsl:param name="to-insert" xml:space="preserve">
<item4>
<item5><xsl:value-of select="//tp:section/tp:block1/tp:move"/></item5>
</item4>
</xsl:param>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tp:item[.//tp:move[. = 1]]//tp:info">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
<xsl:copy-of select="$to-insert"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
It outputs
<root xmlns="http://temo.com/tempe.xsd">
<di>
<md>2013-07-09T09:43:00</md>
</di>
<list>
<item>
<Name>test</Name>
<section block1="true">
<block1>
<move>1</move>
<info>
<item1>test item 1</item1>
<item2>false</item2>
<item3>1</item3>
<item4 xmlns:tp="http://temo.com/tempe.xsd">
<item5>1</item5>
</item4>
</info>
</block1>
<block2>
...
</block2>
</section>
</item>
</list>
<option>
...
</option>
</root>