I am using xslt 1.0 My input xml is as below
<?xml version="1.0" encoding="UTF-8"?>
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<FirstName>John</FirstName>
<LastName>Peter</LastName>
<Initial>T</Initial>
<Spouse>
<FirstName>Rita</FirstName>
<LastName>Hudson</LastName>
</Spouse>
</Employee>
I an trying to write a xsl to produce below output...
<?xml version="1.0" encoding="UTF-8"?>
<ArrayOfstringVariable xmlns="http://schemas.abc.org/2004/07/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<stringVariable>
<name>ServerName</name>
<value>tmn.eu.com</value>
</stringVariable>
<stringVariable>
<name>EmpFirstName</name>
<value>John</value>
</stringVariable>
<stringVariable>
<name>EmpLastName</name>
<value>Peter</value>
</stringVariable>
<stringVariable>
<name>SpouseFirstName</name>
<value>Rita</value>
</stringVariable>
<stringVariable>
<name>SpouseLastName</name>
<value>Hudson</value>
</stringVariable>
</ArrayOfstringVariable>
The output xml contains ArrayOfstringVariable stringVariable name value pair..
The name is hardcoded and the value is from input xml..
Name value "ServerName" is hardcoded.
I tried with xsl code below but it create name value pair with all the elements from input xml
<?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" omit-xml-declaration="no"
encoding="UTF-8" indent="yes" />
<xsl:template match="Employee">
<ArrayOfstringVariable>
<xsl:apply-templates select="*"/>
</ArrayOfstringVariable>
</xsl:template>
<xsl:template match="*">
<stringVariable>
<name>
<xsl:value-of select="local-name()"/>
</name>
<value>
<xsl:value-of select="."/>
</value>
</stringVariable>
</xsl:template>
</xsl:stylesheet>
Can anyone help me to write xsl to produce above output?
Thanks in advance
The XSLT will be straight forward as most of your elements are hardcoded:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:ns="http://schemas.abc.org/2004/07/">
<xsl:output method="xml" omit-xml-declaration="no" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="Employee">
<ns:ArrayOfstringVariable>
<ns:stringVariable>
<ns:name>ServerName</ns:name>
<ns:value>tmn.eu.com</ns:value>
</ns:stringVariable>
<ns:stringVariable>
<ns:name>EmpFirstName</ns:name>
<ns:value>
<xsl:value-of select="FirstName"/>
</ns:value>
</ns:stringVariable>
<ns:stringVariable>
<ns:name>EmpLastName</ns:name>
<ns:value>
<xsl:value-of select="LastName"/>
</ns:value>
</ns:stringVariable>
<ns:stringVariable>
<ns:name>SpouseFirstName</ns:name>
<ns:value>
<xsl:value-of select="Spouse/FirstName"/>
</ns:value>
</ns:stringVariable>
<ns:stringVariable>
<ns:name>SpouseLastName</ns:name>
<ns:value>
<xsl:value-of select="Spouse/LastName"/>
</ns:value>
</ns:stringVariable>
</ns:ArrayOfstringVariable>
</xsl:template>
</xsl:stylesheet>
Related
I have an input XML, which needs to be filtered if element node exists 'ns1:getGenResponse' (or) validate with xsi:type = "Gen" of 'multiRef' element
If either one of the condition successful then I can process 'multiRef' records.
Input XML:
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<soapenv:Body>
<ns1:getGenResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://service.pen.eewe.en.de>
<ns1:getGenReturn xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="soapenc:Array" soapenc:arrayType="xsd:anyType[2]">
<item href="#id0" />
<item href="#id1" />
</ns1:getGenReturn>
</ns1:getGenResponse>
<multiRef xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:Gen">
<name xsi:type="xsd:string">ulm</name>
<mail xsi:type="xsd:string">ulm#gmail.com</mail>
</multiRef>
<multiRef xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" id="id1" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns3:Gen">
<name xsi:type="xsd:string">ABC</name>
<mail xsi:type="xsd:string">abc#gmail.com</mail>
</multiRef>
</soapenv:Body>
</soapenv:Envelope>
Trying with node exist 'ns1:getGenResponse':
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:response="http://tempuri.org/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
exclude-result-prefixes="soap response"
>
<!-- Output -->
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<getGenResponse>
<xsl:for-each select="//soap:Body[ns1:getGenResponse]/multiRef">
<getGenReturn>
<name>
<xsl:value-of select="name" />
</name>
<mail>
<xsl:value-of select="mail" />
</mail>
</getGenReturn>
</xsl:for-each>
</getGenResponse>
</xsl:template>
</xsl:stylesheet>
With this XSLT, I can generate blank . I can't able to generate my desire output.
I am also trying to validate the data with xsi:type of 'multiRef' element node
with below code, but I am unable to execute the XSLT
**Trying to validate with xsi:type ="Gen"**
<xsl:stylesheet version="1.0" xmlns:response="http://tempuri.org/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="soap response">
<!-- Output -->
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<getGenResponse>
<xsl:for-each select="//soap:Body/multiRef[substring- after(#xsi:type, ':')='Gen']">
<getGenReturn>
<name><xsl:value-of select="name"/></name>
<mail><xsl:value-of select="mail"/></mail>
</getGenReturn>
</xsl:for-each>
</getGenResponse>
</xsl:template>
</xsl:stylesheet>
Output expects:
**Output Expected**
<?xml version="1.0" encoding="UTF-8"?>
<getGenResponse>
<getGenReturn>
<name> ULM </name>
<mail>ulm#gmail.com<mail>
</getGenReturn>
<getGenReturn>
<name>ABC</name>
<mail>abc#gmail.com<mail>
</getGenReturn>
/getGenResponse>
Thank you very much.
yes, I declared xmlns:ns1="http://service.pen.eewe.en.de" in the XSLT. now I am able to execute the code with desired result.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:response="http://tempuri.org/"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
exclude-result-prefixes="soap response" xmlns:ns1="http://service.pen.eewe.en.de"
>
<!-- Output -->
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<getGenResponse>
<xsl:for-each select="//soap:Body[ns1:getGenResponse]/multiRef">
<getGenReturn>
<name>
<xsl:value-of select="name" />
</name>
<mail>
<xsl:value-of select="mail" />
</mail>
</getGenReturn>
</xsl:for-each>
</getGenResponse>
</xsl:template>
</xsl:stylesheet>
Dear Experts, I have Input request with SOAP Env, from that which need to be capture only body of by using XSLT
And at the same time, need to remove namespace on the element and also name space prefix of every element.
Input Request
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:v1="http://xmldefs. ag.com/Applications/eer/V1">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<v1:ProcessDistr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<v1:Group xmlns:ns2="http://xmldefs.ag.com/DD/Commons">
<v1:GroupID>437848</v1: GroupID>
<v1:Status>true</v1:Status>
<v1:Parent>45434554</v1:Parent>
</v1:Group>
</v1:ProcessDistr>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Output Received
<?xml version="1.0" encoding="UTF-8"?>
<v1:ProcessDistr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wsu=http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://xmldefs. ag.com/Applications/eer/V1">
<v1:Group xmlns:ns2="http://xmldefs.ag.com/DD/Commons">
<v1:GroupID>437848</v1: GroupID>
<v1:Status>true</v1:Status>
<v1:Parent>45434554</v1:Parent>
</v1:Group>
</v1:ProcessDistr>
XSLT code
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:v1="http://xmldefs. ag.com/Applications/eer/V1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsltc="http://xml.apache.org/xalan/xsltc" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sap="http://www.sap.com/sapxsl" xmlns:prof="http://ixult.net/ProfileExchange" exclude-result-prefixes ="v1" xmlns:wsu=http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" >
<!-- Output -->
<xsl:output encoding="UTF-8" indent="yes" method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/">
<!-- filter ProcessDistr-->
<xsl:copy-of select="//v1:ProcessDistr"/>
</xsl:template>
</xsl:stylesheet>
I am expecting this output
<?xml version="1.0" encoding="UTF-8"?>
<ProcessDistr >
<Group >
<GroupID>437848</GroupID>
<Status>true</Status>
<Parent>45434554</Parent>
</Group>
</ProcessDistr>
Could you please share me your view on this.
Thank you very much.
With Best Regards,
Sateesh N
The result you want can be achieved simply by:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:v1="http://xmldefs.ag.com/Applications/eer/V1">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="v1:*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="#*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Demo (after removing the superfluous spaces from your input): https://xsltfiddle.liberty-development.net/pPzifqc
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:v1="http://xmldefs. ag.com/Applications/eer/V1"
xmlns:ns2="http://xmldefs.ag.com/DD/Commons"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="xs SOAP-ENV wsu v1 ns2 xsi"
version="2.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="SOAP-ENV:Envelope">
<xsl:apply-templates select="SOAP-ENV:Body/v1:ProcessDistr"/>
</xsl:template>
<xsl:template match="v1:ProcessDistr">
<xsl:element name="ProcessDistr">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="v1:Group">
<xsl:element name="Group">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="v1:GroupID">
<xsl:element name="GroupID">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="v1:Status">
<xsl:element name="Status">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="v1:Parent">
<xsl:element name="Parent">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Try this
I am looking my xslt in below format:
<xml>
<apis>
<name>API Name</name>
<comment> Comment</comment>
<version>12</version>
</apis>
</xml>
XSLT Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:L7j="http://ns.l7tech.com/2012/08/jdbc-query-result" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<xml>
<xsl:apply-templates select="//L7j:col" />
</xml>
</xsl:template>
<xsl:template match="//L7j:col">
<api>
<xsl:element name="{#name}">
<xsl:value-of select="." /></xsl:element>
</api>
</xsl:template>
</xsl:stylesheet>
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<L7j:jdbcQueryResult xmlns:L7j="http://ns.l7tech.com/2012/08/jdbc-query-result">
<L7j:row>
<L7j:col name="name" type="java.lang.String">Policy for service #0b8bab6913cc588557b6973e94d1bfdd, WSTrustSoapService</L7j:col>
<L7j:col name="comment">
<![CDATA[NULL]]>
</L7j:col>
<L7j:col name="version" type="java.lang.Integer">18</L7j:col>
</L7j:row>
<L7j:row>
<L7j:col name="name" type="java.lang.String">Policy for service #0b8bab6913cc588557b6973e94d5893d, UUPRStub</L7j:col>
<L7j:col name="comment">
<![CDATA[NULL]]>
</L7j:col>
<L7j:col name="version" type="java.lang.Integer">16</L7j:col>
</L7j:row>
</L7j:jdbcQueryResult>
If I understand correctly, you want to do:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:L7j="http://ns.l7tech.com/2012/08/jdbc-query-result"
exclude-result-prefixes="L7j">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="/L7j:jdbcQueryResult">
<xml>
<xsl:apply-templates/>
</xml>
</xsl:template>
<xsl:template match="L7j:row">
<apis>
<xsl:apply-templates/>
</apis>
</xsl:template>
<xsl:template match="L7j:col">
<xsl:element name="{#name}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
I have below XML
<?xml version="1.0" encoding="UTF-8"?>
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<FirstName>John</FirstName>
<LastName>Peter</LastName>
<Initial>T</Initial>
</Employee>
In XSLT 1.0 I want to write an XSLT to produce below XML from the xml above. can anyone help to me write this xslt?
<?xml version="1.0" encoding="UTF-8"?>
<ArrayOfstringVariable xmlns="http://schemas.abc.org/2004/07/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<stringVariable>
<name>FirstName</name>
<value>John</value>
</stringVariable>
<stringVariable>
<name>LastName</name>
<value>Peter</value>
</stringVariable>
<stringVariable>
<name>Initial</name>
<value>T</value>
</stringVariable>
</ArrayOfstringVariable>
Following XSLT
<?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" omit-xml-declaration="no"
encoding="UTF-8" indent="yes" />
<xsl:template match="Employee">
<ArrayOfstringVariable>
<xsl:apply-templates select="*"/>
</ArrayOfstringVariable>
</xsl:template>
<xsl:template match="*">
<stringVariable>
<name>
<xsl:value-of select="local-name()"/>
</name>
<value>
<xsl:value-of select="."/>
</value>
</stringVariable>
</xsl:template>
</xsl:stylesheet>
when applied to the example input XML from your question produces the following output:
<?xml version="1.0" encoding="UTF-8"?>
<ArrayOfstringVariable>
<stringVariable>
<name>FirstName</name>
<value>John</value>
</stringVariable>
<stringVariable>
<name>LastName</name>
<value>Peter</value>
</stringVariable>
<stringVariable>
<name>Initial</name>
<value>T</value>
</stringVariable>
</ArrayOfstringVariable>
In case you want to have the namespace in the output XML at the ArrayOfStringVariable element, this can be done with two adjustments: add xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" to the xsl:stylesheet declaration and adjust <ArrayOfstringVariable to <ArrayOfstringVariable xmlns="http://schemas.abc.org/2004/07/" > at the <xsl:template match="Employee"> and also adjust <stringVariable> to <stringVariable xmlns="http://schemas.abc.org/2004/07/"> at the <xsl:template match="*">.
I have got 2 source to the XSLT, which needs to be mapped to the target. Have given below the source and desired output. The first source XML is in a collection which needs to be iterated to fetch the value.
Input Payload:
XML 1:
<ParticipentsCollection>
<Participents>
<Email>PM#y.com</Email>
<Role>PM</Role>
</Participents>
<Participents>
<Email>BM#y.com</Email>
<Role>BM</Role>
</Participents>
<Participents>
<Email>CM#y.com</Email>
<Role>CM</Role>
</Participents>
</ParticipentsCollection>
XML 2:
<Project>
<ID>1</ID>
<Name>XYZ</Name>
<Status>Req Gathering</Status>
</Project>
Desired Output:
<ProjectDetails>
<ID>1</ID>
<Name>XYZ</Name>
<Status>Req Gathering</Status>
<PM>PM#y.com</PM>
<BM>PM#y.com</BM>
<CM>>CM#y.com</CM>
</ProjectDetails>
If you are using XSLT 1.0 use:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="exslt msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="Doc2"><xsl:copy><xsl:copy-of select="document('Untitled2.xml')/Project"></xsl:copy-of></xsl:copy></xsl:param>
<xsl:template match="ParticipentsCollection">
<ProjectDetails>
<xsl:copy-of select="exslt:node-set($Doc2)/Project/*"/>
<xsl:for-each select="Participents">
<xsl:element name="{Role}"><xsl:value-of select="Email"/></xsl:element>
</xsl:for-each>
</ProjectDetails>
</xsl:template>
</xsl:stylesheet>
and if 2.0 use:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="Doc2"><xsl:copy><xsl:copy-of select="document('Untitled2.xml')/Project"></xsl:copy-of></xsl:copy></xsl:param>
<xsl:template match="ParticipentsCollection">
<ProjectDetails>
<xsl:copy-of select="$Doc2/Project/*"/>
<xsl:for-each select="Participents">
<xsl:element name="{Role}"><xsl:value-of select="Email"/></xsl:element>
</xsl:for-each>
</ProjectDetails>
</xsl:template>
</xsl:stylesheet>
I am running this XSLT on XML1 and keeping XML2 in $Doc2 param to get output:
<ProjectDetails>
<ID>1</ID>
<Name>XYZ</Name>
<Status>Req Gathering</Status>
<PM>PM#y.com</PM>
<BM>BM#y.com</BM>
<CM>CM#y.com</CM>
</ProjectDetails>