Moving a element sequence value to other element sequence - xslt

I have a certain requirement where, I need to move the sequence element values to another newly created element according to the the number of values in the original sequence.
Please find my Input XML and the Desired Output XML .
help is highly appreciated
Rule:
Move the value of Addr1 (catalogue/cd11/Location/Addr/Addr1) to
catalogue/cd11/Location/primary/original/Address1/place. primary/original/Address1/place need to be created.
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.altova.com">
<publisher>
<Name id="d123">
<Place>Chicago</Place>
</Name
<catalogue id="d1" >
<cd11 id="d2">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<year>1985</year>
<Location id="d1234">
<Addr id="d234">
<Addr1 id="d565">catherine Av</Addr1>
<Addr2 id="d566">block a</Addr2>
<City id="d567">chicago</City>
</Addr>
<Addr id="d334">
<Addr1 id="d665">Illinois st</Addr1>
<Addr2 id="d666">block a</Addr2>
<City id="d667">chicago</City>
</Addr>
</Location>
</cd11>
</catalogue>
<catalogue id="d3" >
<cd11 id="d4">
<title>Jurassic World</title>
<artist>Chris Pratt</artist>
</cd11>
</catalogue>
</publisher>
</root>
Output XML:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.example.com">
<publisher>
<Name id="d123">
<Place>Chicago</Place>
</Name>
<catalogue id="d1">
<cd11 id="d2">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<year>1985</year>
<Location id="d1234">
<Addr id="d234">
<Addr1 id="d565">catherine Av</Addr1>
<Addr2 id="d566">block a</Addr2>
<City id="d567">chicago</City>
</Addr>
<Addr id="d334">
<Addr1 id="d665">Illinois st</Addr1>
<Addr2 id="d666">block a</Addr2>
<City id="d667">chicago</City>
</Addr>
<primary>
<original>
<test>test value</test>
<Address1>
<place>catherine Av</place>
</Address1>
<Address1>
<place>Illinois st</place>
</Address1>
</original>
</primary>
</Location>
</cd11>
</catalogue>
<catalogue id="d3">
<cd11 id="d4">
<title>Jurassic World</title>
<artist>Chris Pratt</artist>
</cd11>
</catalogue>
</publisher>
</root>
Thanks in advance.

You can write a template for Location elements that inserts the new elements and transforms the Addr1 elements:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xpath-default-namespace="http://www.altova.com" xmlns="http://www.altova.com">
<xsl:output indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="catalogue/cd11/Location">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
<primary>
<original>
<Address1>
<xsl:apply-templates select="Addr/Addr1" mode="convert"/>
</Address1>
</original>
</primary>
</xsl:copy>
</xsl:template>
<xsl:template match="Addr/Addr1" mode="convert">
<place>
<xsl:value-of select="."/>
</place>
</xsl:template>
</xsl:transform>
Online sample at http://xsltransform.net/ncdD7mv.
According to your comment and edit you do not want to copy the elements, instead you want to transform them to a new namespace, so you need to change all uses of xsl:copy of an element to create an element of the same local name but with the new namespace (which will simply work if you have the right xmlns="http://www.example.com" in the XSLT and use xsl:element name="{local-name()}"):
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xpath-default-namespace="http://www.altova.com" xmlns="http://www.example.com">
<xsl:output indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="#* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="catalogue/cd11/Location">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="#* | node()"/>
<primary>
<original>
<Address1>
<xsl:apply-templates select="Addr/Addr1" mode="convert"/>
</Address1>
</original>
</primary>
</xsl:element>
</xsl:template>
<xsl:template match="Addr/Addr1" mode="convert">
<place>
<xsl:value-of select="."/>
</place>
</xsl:template>
</xsl:transform>

Related

blank Attribute with value in XML with XSLT transformation

I have below XMl and I need to replace root="" with Dynamic GUID value. this can be anywhere in XML document. it does not work with below XSLT. This is just the extended text to post successfully.
<ClinicalDocument xmlns="urn:hl7-org:v3">
<templateId root="2.16.840.1.113883.10.20.22.1.2" extension="2015-08-01"/>
<id root=""/>
<code code="34133-9" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LN"
displayName="Summarization of Episode Note"/>
<title>Patient Summary Document</title>
<languageCode code="en-US"/>
<component>
<structuredBody>
<component>
<section>
<templateId root="2.16.840.1.113883.10.20.22.2.6.1"/>
<entry typeCode="DRIV">
<act classCode="ACT" moodCode="EVN">
<templateId root="2.16.840.1.113883.10.20.22.4.30"/>
<templateId root="2.16.840.1.113883.10.20.22.4.30" extension="2015-08-01"/>
<id nullFlavor="UNK"/>
<informant>
<assignedEntity>
<id root="2.16.840.1.113883.3.86.3.1" extension="STHS"/>
<addr nullFlavor="UNK"/>
<telecom nullFlavor="UNK"/>
<assignedPerson>
<name nullFlavor="UNK"/>
</assignedPerson>
<representedOrganization>
<id root="" extension="STHS" displayable="true"/>
<name>STHS</name>
<telecom nullFlavor="UNK"/>
<addr nullFlavor="UNK"/>
</representedOrganization>
</assignedEntity>
</informant>
</act>
</entry>
</section>
</component>
</structuredBody>
</component>
</ClinicalDocument>
I have below XSLT. but it does not work for above highlighted tag.
<xsl:variable name="GUID" select="'FF1122'"/>
<xsl:template match="id/#root[.='']">
<xsl:attribute name="root">
<xsl:value-of select="$GUID"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:isc="http://extension-functions.intersystems.com"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="urn:hl7-org:v3">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="GUID" select="'FF1122'"/>
<xsl:template match="ns1:id/#root[.='']">
<xsl:attribute name="root">
<xsl:value-of select="$GUID"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

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>

xslt: how to normalize numerical element

I would like to add an element to existing XML file with normalized value of existing element in the XML.
Any help would be extremely appreciated.
regards
<?xml version="1.0" encoding="UTF-8"?>
<top>
<Results>
<a>no</a>
<b>10</b>
<c>12</c>
<d>9</d>
</Results>
<Results>
<a>no</a>
<b>8</b>
<c>50</c>
<d>12</d>
</Results>
<Results>
<a>no</a>
<b>6</b>
<c>55</c>
<d>56</d>
</Results>
<Results>
<a>yes</a>
<b>23</b>
<c>32</c>
<d>34</d>
</Results>
</top>
In the sample input xml above, I would like to add "b_nom" elements to each of the results where the value is (b)/(minimum of 'b' grouped with a). The expected output is as below
<?xml version="1.0" encoding="UTF-8"?>
<top>
<Results>
<a>no</a>
<b>10</b>
<b_nom>1.66</b_nom>
<c>12</c>
<d>9</d>
</Results>
<Results>
<a>no</a>
<b>8</b>
<b_nom>1.33</b_nom>
<c>50</c>
<d>12</d>
</Results>
<Results>
<a>no</a>
<b>6</b>
<b_nom>1</b_nom>
<c>55</c>
<d>56</d>
</Results>
<Results>
<a>yes</a>
<b>23</b>
<b_nom>1</b_nom>
<c>32</c>
<d>34</d>
</Results>
</top>
I think you want
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="k1" match="Results" use="a"/>
<xsl:template match="#* | node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="b">
<xsl:call-template name="identity"/>
<xsl:variable name="min">
<xsl:for-each select="key('k1', ../a)">
<xsl:sort select="b" data-type="number"/>
<xsl:if test="position() = 1">
<xsl:value-of select="b"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<b_nom>
<xsl:value-of select="format-number(. div $min, '0.##')"/>
</b_nom>
</xsl:template>
</xsl:stylesheet>
Here is a changed version of the stylesheet that takes a couple of values identifying a group into account:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:data="http:://example.com/data"
exclude-result-prefixes="data">
<data:data xmlns="">
<group>
<key>no</key>
<values>
<value>no</value>
<value>n</value>
<value>0</value>
</values>
</group>
<group>
<key>yes</key>
<values>
<value>yes</value>
<value>y</value>
<value>1</value>
</values>
</group>
</data:data>
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="main-input" select="/"/>
<xsl:variable name="groups" select="document('')/xsl:stylesheet/data:data/group"/>
<xsl:template match="#* | node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="b">
<xsl:call-template name="identity"/>
<xsl:variable name="this" select="."/>
<xsl:variable name="min">
<xsl:for-each select="$main-input//Results[a = $groups/values[value = $this/../a]/value]">
<xsl:sort select="b" data-type="number"/>
<xsl:if test="position() = 1">
<xsl:value-of select="b"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<b_nom>
<xsl:value-of select="format-number(. div $min, '0.##')"/>
</b_nom>
</xsl:template>
</xsl:stylesheet>
That transforms the input
<?xml version="1.0" encoding="UTF-8"?>
<top>
<Results>
<a>no</a>
<b>10</b>
<c>12</c>
<d>9</d>
</Results>
<Results>
<a>n</a>
<b>8</b>
<c>50</c>
<d>12</d>
</Results>
<Results>
<a>0</a>
<b>6</b>
<c>55</c>
<d>56</d>
</Results>
<Results>
<a>yes</a>
<b>23</b>
<c>32</c>
<d>34</d>
</Results>
</top>
into the output
<top>
<Results>
<a>no</a>
<b>10</b>
<b_nom>1.67</b_nom>
<c>12</c>
<d>9</d>
</Results>
<Results>
<a>n</a>
<b>8</b>
<b_nom>1.33</b_nom>
<c>50</c>
<d>12</d>
</Results>
<Results>
<a>0</a>
<b>6</b>
<b_nom>1</b_nom>
<c>55</c>
<d>56</d>
</Results>
<Results>
<a>yes</a>
<b>23</b>
<b_nom>1</b_nom>
<c>32</c>
<d>34</d>
</Results>
</top>

XSLT Convert attribute to element and copy value from an other elemen

I have XML data like this
<ABC version="1.0">
<XYZ>
<ROWSET ROWS="00001">
<ROWDEF>
<COLUMN ID="ACCT_ID" LEN="016" NULL="Y"/>
<COLUMN ID="AGNT_ID" LEN="004" NULL="Y"/>
<COLUMN ID="CUST_EXTR_ID" LEN="024" NULL="Y"/>
<COLUMN ID="PI_ID" LEN="019" NULL="Y"/>
<COLUMN ID="PRIN_ID" LEN="004" NULL="Y"/>
<COLUMN ID="SYS_ID" LEN="004" NULL="Y"/>
</ROWDEF>
<ROW>
<C>6369921501000060</C>
<C>0000</C>
<C>C13093102141063422034238</C>
<C>6369921501000060 </C>
<C>1500</C>
<C>9008</C>
</ROW>
<ROW>
<C>6369921501000061</C>
<C>0001</C>
<C>C13093102141063422034231</C>
<C>6369921501000060 </C>
<C>1501</C>
<C>9001</C>
</ROW>
</ROWSET>
</XYZ>
</ABC>
And I'm trying to convert this into
<?xml version="1.0" encoding="utf-8"?>
<ABC version="1.0">
<XYZ RC="0067">
<ROWSET ROWS="00001">
<ROWDEF>
<ACCT_ID>6369921501000060</ACCT_ID>
<AGNT_ID>0000</AGNT_ID>
<CUST_EXTR_ID>C13093102141063422034238</CUST_EXTR_ID>
<PI_ID>6369921501000060</PI_ID>
<PRIN_ID>1500</PRIN_ID>
<SYS_ID>9008</SYS_ID>
</ROWDEF>
<ROWDEF>
<ACCT_ID>6369921501000061</ACCT_ID>
<AGNT_ID>0001</AGNT_ID>
<CUST_EXTR_ID>C13093102141063422034231</CUST_EXTR_ID>
<PI_ID>6369921501000060</PI_ID>
<PRIN_ID>1501</PRIN_ID>
<SYS_ID>9001</SYS_ID>
</ROWDEF>
</ROWSET>
</XYZ>
</ABC>
I have looked around and tried few things but it is not working.
Can someone help me with this.
Below is my XSLT. Thanks in advance.
<?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="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="COLUMN">
<xsl:element name="{#ID}">
<xsl:copy>
<xsl:apply-templates select="node()"></xsl:apply-templates>
</xsl:copy>
<xsl:apply-templates />
<!--<xsl:call-template name="value"></xsl:call-template>-->
</xsl:element>
</xsl:template>
<!--
<xsl:template match="ROW" name="value">
<xsl:copy>
<xsl:apply-templates select="node()"></xsl:apply-templates>
</xsl:copy>
</xsl:template>
-->
</xsl:stylesheet>
This stylesheet will mostly do what you want, but I cannot fathom how to generate the attribute for <XYZ RC="0067">.
For each ROWSET it comes across it saves the ROWDEF element in a variable, copies all attribute nodes, and then processes each ROW element. The position of each C element in the ROW is caclulated, and the COLUMN in the corresponding position within the stored ROWDEF element is used to fetch an element name from the ID attribute.
<?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:strip-space elements="*"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="ROWSET">
<xsl:variable name="columns" select="ROWDEF/COLUMN"/>
<xsl:copy>
<xsl:apply-templates select="#*" />
<xsl:for-each select="ROW">
<ROWDEF>
<xsl:for-each select="C">
<xsl:variable name="pos" select="position()"/>
<xsl:element name="{$columns[$pos]/#ID}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</ROWDEF>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
output
<?xml version="1.0" encoding="utf-8"?>
<ABC version="1.0">
<XYZ>
<ROWSET ROWS="00001">
<ROWDEF>
<ACCT_ID>6369921501000060</ACCT_ID>
<AGNT_ID>0000</AGNT_ID>
<CUST_EXTR_ID>C13093102141063422034238</CUST_EXTR_ID>
<PI_ID>6369921501000060 </PI_ID>
<PRIN_ID>1500</PRIN_ID>
<SYS_ID>9008</SYS_ID>
</ROWDEF>
<ROWDEF>
<ACCT_ID>6369921501000061</ACCT_ID>
<AGNT_ID>0001</AGNT_ID>
<CUST_EXTR_ID>C13093102141063422034231</CUST_EXTR_ID>
<PI_ID>6369921501000060 </PI_ID>
<PRIN_ID>1501</PRIN_ID>
<SYS_ID>9001</SYS_ID>
</ROWDEF>
</ROWSET>
</XYZ>
</ABC>

Adding a namespace to just one element

How do I only add a namespace to the root element?
My XML:
<Envelope>
<from>
<contents />
</from>
</Envelope>
My desired output:
<Envelope xmlns:tns="Foo">
<from>
<contents />
</from>
</Envelope>
I can only get "xmlns='Foo'" using this, not "xmlns:tns=..":
<xsl:element name="{local-name()}" namespace="Foo" >
<xsl:copy-of select="attribute::*"/>
<xsl:apply-templates />
</xsl:element>
Here is a complete transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tns="Foo">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:element name="{name()}">
<xsl:copy-of select=
"document('')/*/namespace::*[name()='tns']"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<Envelope>
<from>
<contents />
</from>
</Envelope>
the wanted, correct result is produced:
<Envelope xmlns:tns="Foo">
<from>
<contents/>
</from>
</tns:Envelope>