Best way to use for-each inside if in XSLT - xslt

I have the below XML data as input to my XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<Application>
<Data>
<Data1>
<name>Michale</name>
<age>65</age>
<Info>
<Alias name="M">
<Contactmail>abc#gmail.com</Contactmail>
<ContactPh>8988900009</ContactPh>
</Alias>
<Alias name="Q">
<Contactmail>abc#gmail.com</Contactmail>
<ContactPh>8988900009</ContactPh>
</Alias>
</Info>
</Data1>
<Data1>
<name>Albert</name>
<age>69</age>
<Info>
<Alias name="A">
<Contactmail>xyz#gmail.com</Contactmail>
<ContactPh>89889908709</ContactPh>
</Alias>
<Alias name="P">
<Contactmail>pqr#gmail.com</Contactmail>
<ContactPh>8988988779</ContactPh>
</Alias>
</Info>
</Data1>
</Data>
</Application>
And I want to pass the Data1 block whose Alias name matches with "M", i.e.:
<Application>
<Data>
<Data1>
<name>Michale</name>
<age>65</age>
<Info>
<Alias name=M>
<Contactmail>abc#gmail.com</Contactmail>
<ContactPh>8988900009</ContactPh>
</Alias>
<Alias name=Q>
<Contactmail>abc#gmail.com</Contactmail>
<ContactPh>8988900009</ContactPh>
</Alias>
</Info>
</Data1>
</Data>
</Application>
I am stuck as to how to access an loop(ie Alias) inside a test condition?
Is there any better way to do this xslt?
<xsl:for-each select="./*[local-name() = 'Application']/*[local-name() = 'Data']">
<xsl:if test="">
....
</xsl:if>
</xsl:for-each>

The following template will do the job. The explanations are in the code.
<?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" indent="yes"/>
<xsl:strip-space elements="*" /> <!-- Removes unnecessary space between elements -->
<!-- identity template --> <!-- Copies all nodes not matched by other templates -->
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:template match="Data1[Info/Alias/#name != 'M']" /> <!-- Ignores all Data1 elements which don't have an #name='M' attribute child -->
<xsl:template match="Data1[Info/Alias/#name = 'M']"> <!-- Matches all Data1 elements which have the desired child attribute -->
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Its output is:
<?xml version="1.0"?>
<Application>
<Data>
<Data1>
<name>Michale</name>
<age>65</age>
<Info>
<Alias name="M">
<Contactmail>abc#gmail.com</Contactmail>
<ContactPh>8988900009</ContactPh>
</Alias>
<Alias name="Q">
<Contactmail>abc#gmail.com</Contactmail>
<ContactPh>8988900009</ContactPh>
</Alias>
</Info>
</Data1>
</Data>
</Application>

<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Application">
<xsl:copy>
<xsl:for-each select="Data/Data1">
<xsl:if test="Info/Alias[#name='M']">
<Data>
<Data1>
<xsl:apply-templates/>
</Data1>
</Data>
</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:template>
You may also do like this

Related

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>

xsl to remove specific attribute and add it as a child node instead

I have the following xml:
<Envelope>
<ABC>
<Hierarchy>
<Family>
<History> ... </History>
<Plan>
<Mom Name="Parent1">
<Child Name = "Child11">
<GrandChild Name="GC111" Label="" Sequence = "1">
<Attributes>... </Attributes>
</GrandChild>
</Child>
</Mom>
<Child Name = "ChildIndependent1">
<GrandChild Name="GCIndep12" Label="<Requested><Item1>68</Item1><Item2>69</Item2&g t;</Requested>" Sequence = "2" >
<Attributes>... </Attributes>
</GrandChild> </Child>
</Plan> </Family>
</Hierarchy> </ABC>
</Envelope>
Here is my current xsl i have on the xml to create dummy mom tags around independent child tags. It also converts the & lt; and & gt; to < and >
<xsl:stylesheet version="2.0" >
<xsl:output method="xml" indent="yes" use-character-maps="angle-brackets"/>
<xsl:character-map name="angle-brackets">
<xsl:output-character character="<" string="<"/>
<xsl:output-character character=">" string=">"/>
</xsl:character-map>
<xsl:template match="node()|#*">
<xsl:copy><xsl:apply-templates select="node()|#*"/></xsl:copy>
</xsl:template>
<xsl:template match="//ABC/Hierarchy/Family">
<xsl:element name="Plan">
<xsl:for-each select="//ABC/Hierarchy/Family/Plan/*">
<xsl:if test="self::Mom">
<xsl:copy-of select="."/>
</xsl:if>
<xsl:if test="self::Child">
<xsl:element name="Mom">
<xsl:attribute name="Name">dummy</xsl:attribute>
<xsl:copy-of select="."/>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
I need to modify the above xsl to create the LabelRequested tag with the content of the label attribute as a child to the GrandChild and remove the Label attribute itself as below above xsl already converts the & lt; and & gt; to < and >.
<Child Name = "ChildIndependent1">
<GrandChild Name="GCIndep12" Sequence = "2" >
<LabelRequested>
<Requested><Item1>68</Item1><Item2>69</Item2> </Requested>
</LabelRequested>
<Attributes>... </Attributes>
</GrandChild>
Any idea how I could change the above xsl to do this?
To minimize the example to the problem at hand, consider the following stylesheet:
XSLT 2.0
<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"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="GrandChild">
<xsl:copy>
<xsl:apply-templates select="#* except #Label"/>
<LabelRequested>
<xsl:value-of select="#Label" disable-output-escaping="yes"/>
</LabelRequested>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Applied to your input example, the result will be:
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<ABC>
<Hierarchy>
<Family>
<History> ... </History>
<Plan>
<Mom Name="Parent1">
<Child Name="Child11">
<GrandChild Name="GC111" Sequence="1">
<LabelRequested/>
<Attributes>... </Attributes>
</GrandChild>
</Child>
</Mom>
<Child Name="ChildIndependent1">
<GrandChild Name="GCIndep12" Sequence="2">
<LabelRequested><Requested><Item1>68</Item1><Item2>69</Item2></Requested></LabelRequested>
<Attributes>... </Attributes>
</GrandChild>
</Child>
</Plan>
</Family>
</Hierarchy>
</ABC>
</Envelope>
Note that this assumes your processor supports disable-output-escaping and that the processing chain allows it to execute it. Otherwise you would have to use either XSLT 3.0 or an extension function to parse the markup contained in the Label attribute.

XSLT 1.0 transformation

I'm trying to transform a XML file with XSLT 1.0 but I'm having troubles with this.
Input:
<task_order>
<Q>
<record id="1">
<column name="task_externalId">SPLIT4_0</column>
</record>
<record id="2">
<column name="task_externalId">SPLIT4_1</column>
</record>
</Q>
<task>
<id>SPLIT4</id>
<name>test</name>
</task>
</task_order>
Wanted result:
For each task_order element: When there is more than 1 record-element (SPLIT4 and SPLIT4_1) I need to duplicate the task element and change the orginal task-id with the id from record elements.
<task_order>
<Q>
<record id="1">
<column name="task_externalId">SPLIT4_0</column>
</record>
<record id="2">
<column name="task_externalId">SPLIT4_1</column>
</record>
</Q>
<task>
<id>SPLIT4_0</id>
<name>test</name>
</task>
<task>
<id>SPLIT4_1</id>
<name>test</name>
</task>
</task_order>
Any suggestions?
Thank you
First start off with the Identity Template which will handle copying across all existing nodes
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
Next, to make looking up the columns slightly easier, consider using an xsl:key
<xsl:key name="column" match="column" use="substring-before(., '_')" />
Then, you have a template matching task where you can look up all matching column elements using the key, and create a new task element for each
<xsl:template match="task">
<xsl:variable name="task" select="." />
<xsl:for-each select="key('column', id)">
<!-- Create new task -->
</xsl:for-each>
</xsl:template>
Try this XSTL
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:key name="column" match="column" use="substring-before(., '_')" />
<xsl:template match="task">
<xsl:variable name="task" select="." />
<xsl:for-each select="key('column', id)">
<task>
<id><xsl:value-of select="." /></id>
<xsl:apply-templates select="$task/*[not(self::id)]" />
</task>
</xsl:for-each>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Moving a element sequence value to other element sequence

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>

XSLT copy the entire contents of an XML with 2 (or more) exceptions

I've got an large input XML that I want to make smaller for a particular client. The client should only see information relevant to him. Here is the example input.
<?xml version='1.0' encoding='UTF-8' ?>
<reg>
<global>stuff</global>
<profile>
<profile_data>profile stuff 1</profile_data>
<users>
<u><usr_data>usr options 1</usr_data><n>user-1</n></u>
<u><usr_data>usr options 2</usr_data><n>user-2</n></u>
</users>
</profile>
<profile>
<profile_data>profile stuff 2</profile_data>
<users>
<u><usr_data>usr options 3</usr_data><n>user-3</n></u>
<u><usr_data>usr options 4</usr_data><n>user-4</n></u>
</users>
</profile>
</reg>
This needs to be transformed into a smaller XML that looks like this:
<?xml version="1.0"?>
<reg>
<global>stuff</global>
<profile>
<profile_data>profile stuff 1</profile_data>
<users>
<u><usr_data>usr options 1</usr_data><n>user-1</n></u>
</users>
</profile>
</reg>
I've managed to accomplish this using 2 XSLT transformations applied in series (trans1.xml):
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="name"/>
<xsl:output method="xml"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/reg/profile">
<xsl:if test="./users/*/n=$name">
<xsl:copy-of select="." />
</xsl:if>
</xsl:template>
</xsl:stylesheet>
and (trans2.xml):
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="name"/>
<xsl:output method="xml"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/reg/profile/users/*">
<xsl:if test="./n=$name">
<xsl:copy-of select="." />
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Then:
xsltproc -param name "'user-1'" trans1.xml input.xml > out1.xml
xsltproc -param name "'user-1'" trans2.xml out1.xml > result.xml
How can I convert my 2 XSl stylesheets into 1 to perform these 2 operations in 1 step.
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pName" select="'user-1'"/>
<xsl:template match="node()|#*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="profile">
<xsl:if test="users/u/n = $pName">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>
<xsl:template match="u">
<xsl:if test="n = $pName">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<reg>
<global>stuff</global>
<profile>
<profile_data>profile stuff 1</profile_data>
<users>
<u>
<usr_data>usr options 1</usr_data>
<n>user-1</n>
</u>
<u>
<usr_data>usr options 2</usr_data>
<n>user-2</n>
</u>
</users>
</profile>
<profile>
<profile_data>profile stuff 2</profile_data>
<users>
<u>
<usr_data>usr options 3</usr_data>
<n>user-3</n>
</u>
<u>
<usr_data>usr options 4</usr_data>
<n>user-4</n>
</u>
</users>
</profile>
</reg>
produces the wanted, correct result:
<reg>
<global>stuff</global>
<profile>
<profile_data>profile stuff 1</profile_data>
<users>
<u>
<usr_data>usr options 1</usr_data>
<n>user-1</n>
</u>
</users>
</profile>
</reg>
Explanation:
Proper use and overriding of the identity rule.