XSLT - multiple elements values are coming in single read - xslt

I am trying to read the number element value using below xsl.
XML:
<message channel-id="64E523E5-7A27-4343-A62A-AA8FBD24EB59">
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<insertResponse>
<sys_id>4342e2a24fb142005b0e4fe18110c7f3</sys_id>
<number>INC0010038</number>
</insertResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
</message>
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<message>
<send-parameters>
<agent-parameter>
<name>IncidentNumber</name>
<value><xsl:value-of select="message/s:Envelope/s:Body/t:insertResponse/t:number" /></value>
</agent-parameter>
</send-parameters>
</message>
</xsl:template>
</xsl:stylesheet>
The output i am getting is as below.
<?xml version="1.0" encoding="UTF-8"?>
<message>
<send-parameters>
<agent-parameter>
<name>IncidentNumber</name>
<value>4342e2a24fb142005b0e4fe18110c7f3
INC0010038</value>
</agent-parameter>
</send-parameters>
</message>
The value element is getting value from both sys-id and number tags 4342e2a24fb142005b0e4fe18110c7f3INC0010038. I want to read only number tag from the xml. I am sure that i am missing something very basic here. Please help me with that.
Regards,
Kiran

Try out the following:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
exclude-result-prefixes="env xsl">
<xsl:template match="/">
<message>
<send-parameters>
<agent-parameter>
<name>IncidentNumber</name>
<value>
<xsl:value-of select="message/env:Envelope/env:Body/insertResponse/number" />
</value>
</agent-parameter>
</send-parameters>
</message>
</xsl:template>
</xsl:stylesheet>
As stated by Micheal you are missing namespace definition in your stylesheet. Once you do that your code is good to go

Related

XML To Text through XSLT

This is the original xml file
<?xml version="1.0" encoding="utf-8"?>
<Report xsi:schemaLocation="FPPD2_AL2.srdl http://sunserver/ReportServer?%2FSunSystems%2FExample%20Reports%2FProcess%2FFPPD2_AL2.srdl&rs%3AFormat=XML&rc%3ASchema=True" Name="FPPD2_AL2.srdl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="FPPD2_AL2.srdl">
<Detail_2_1>
<Detail_2_1_Group_Collection>
<Item RowNumber="1" transactionDate="2019-11-04" description_1="PI -Local" BankSortCode="" transactionReference="T0004" DebitCredit_Description="Credit" baseAmount_baseCreditAmount="236.32" baseAmount_baseDebitAmount="0.00" baseAmount_amount_10="-236.32" SupBankAccountName="C Voice Bank Account" TransactionAmount_TransactionCreditAmount="-413.56" TransactionAmount_Amount="-413.56" CurrencyCode_3="USD" baseAmount_amount_1="236.32" SupBankAccountNumber="01234567" SupBankSortCode="123456" DebitCredit_Code="C" ReportingAmount_ReportingCreditAmount="-413.56" ReportingAmount_ReportingDebitAmount="0.00" BankProcessDate_1="20191106" AccountCode_1="CVOI001" ProfileCode_param="BACS">
<subreport>
<Report Name="FPPD2_s1.srdl">
<Detail_2_1>
<Detail_2_1_Group_Collection>
<Item PaymentAccount="52100" BankAccountName="Own Bank Account" BankAccountNumber="0011223344" />
</Detail_2_1_Group_Collection>
</Detail_2_1>
</Report>
</subreport>
</Item>
</Detail_2_1_Group_Collection>
</Detail_2_1>
</Report>
I was trying to get the value of the PaymentAccount ("52100"). Here;s my code so far, but it does not return anything
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns2="FPPD2_AL2.srdl"
xmlns:sr1="FPPD2_s1.srdl">
<xsl:output method="text"/>
<xsl:template match="ns2:Report">
<xsl:value-of select="ns2:Detail_2_1/ns2:Detail_2_1_Group_Collection/ns2:Item/ns2:subreport/ns2:Report/ns2:Detail_2_1/ns2:Detail_2_1_Group_Collection/Item[Name='PaymentAccount']"/>
</xsl:template>
</xsl:stylesheet>
I am pretty new to XSLT so I don't know if there is anything wrong with my syntax or structure?
Thank you
Instead of:
.../Item[Name='PaymentAccount']
use:
.../ns2:Item/#PaymentAccount

Extract XML Multiple Namespace with XSL

I have this XML with namespace and i need to extract on segment "NewDataSet"
I have a xsl code but it's not works
<?xml version="1.0" encoding="UTF-8"?>
<Listado_OrdenesResponse xmlns='http://tempuri.org/' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<Listado_OrdenesResult>
<diffgr:diffgram xmlns:diffgr='urn:schemas-microsoft-com:xml-diffgram-v1' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata'>
<NewDataSet xmlns=''>
<RowNum diffgr:id='RowNum1' msdata:rowOrder='0'>
<MATNR>10000101</MATNR> <AUFNR>731200000047</AUFNR>
<MENGE>385</MENGE>
<MEINS>G</MEINS>
</RowNum>
<RowNum diffgr:id='RowNum2' msdata:rowOrder='1'>
<MATNR>45000528</MATNR>
<AUFNR>731200000047</AUFNR>
<MENGE>540</MENGE>
<MEINS>KG</MEINS>
</RowNum>
</NewDataSet>
</diffgr:diffgram>
</Listado_OrdenesResult>
</Listado_OrdenesResponse>
I need to extract like this segment , NewDataSet.
<NewDataSet> <RowNum>
<MATNR>10000101</MATNR>
<AUFNR>731200000047</AUFNR>
</RowNum>
<RowNum>
<MATNR>45000528</MATNR>
<AUFNR>731200000047</AUFNR>
</RowNum>
</NewDataSet>
<!-- Need To Extract -->
I have this code but the return is not as expected.
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2018 (https://www.liquid-technologies.com) -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:template match="//NewDataSet">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
When I run the above XSL, I got this XML.
I will have the segment without namespace
<?xml version="1.0" encoding="UTF-8"?>
<RowNum xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
diffgr:id="RowNum1"
msdata:rowOrder="0">
<MATNR>10000101</MATNR>
<AUFNR>731200000047</AUFNR>
<MENGE>385</MENGE>
<MEINS>G</MEINS>
</RowNum>
<RowNum xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
diffgr:id="RowNum2"
msdata:rowOrder="1">
<MATNR>45000528</MATNR>
<AUFNR>731200000047</AUFNR>
<MENGE>540</MENGE>
<MEINS>KG</MEINS>
</RowNum>
Can you Help me?
In XSLT 2.0, use <xsl:copy-of select="XXXX" copy-namespaces='no'/>.
In XSLT 1.0 you need to use a variant of the identity template, copying elements using <xsl:element name="{local-name()}" namespace="namespace-uri()"/>.
Please don't ask XSLT questions without saying which version you are using, as many things are easier with XSLT 2.0 or 3.0!

XSLT Help : Copy elements <choose>

With my limited knowledge of XSL programming I have come up with this code:
(Post updated: XSLT is now working - Updated for the benefit of others if at all!)
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:nm="http://fsag.de/BusinessTransaction"
xmlns:env="http://www.w3.org/2001/12/soap-envelope"
exclude-result-prefixes="env">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/nm:Responses">
<xsl:copy-of select="/"/>
</xsl:template>
<xsl:template match="/Envelope">
<nm:Responses xmlns:nm="http://fsag.de/BusinessTransaction">
<BTEMessages/>
<SystemContext/>
<Error>
<xsl:copy-of select="Body"/>
</Error>
</nm:Responses>
</xsl:template>
</xsl:stylesheet>
The purpose of the is to process the file (at the end of the post) and get the result below:
Required result for 'Error' file :
<?xml version="1.0" encoding="UTF-8"?>
<nm:Response xmlns:nm="http://fsag.de/BusinessTransaction">
<BTEMessages/>
<SystemContext/>
<Error>
<Body>
<Response status="1">
<Description>DESC</Description>
<Errors>
<Error>500</Error>
</Errors>
</Response>
</Body>
</Error>
</nm:Response>
Required result for 'Response' file:
<?xml version="1.0" encoding="UTF-8"?>
<nm:Response xmlns:nm="http://fsag.de/BusinessTransaction">
<BTEMessages>
<InOrderContext xmlns:n0="http://fsag.de/BusinessTransaction">
<SendTimestamp>01-06-2017:0506230000</SendTimestamp>
</InOrderContext>
<SystemContext>
<Client>FDP</Client>
<CorrelationId>34553FTG25543SFD134</CorrelationId>
<BusinessContext>RECONCL</BusinessContext>
</SystemContext>
</BTEMessages>
</nm:Response>
*File 1: Error *
<Envelope >
<Body>
<Response status="1">
<Description>DESC</Description>
<Errors>
<Error>500</Error>
</Errors>
</Response>
</Body>
</Envelope>
File 2: Response:
<?xml version="1.0" encoding="UTF-8"?>
<nm:Response xmlns:nm="http://fsag.de/BusinessTransaction">
<BTEMessage>
<InOrderContext>
<SendTimestamp>01-06-2017:0506230000</SendTimestamp>
</InOrderContext>
<SystemContext>
<Client>FDP</Client>
<CorrelationId>34553FTG25543SFD134</CorrelationId>
<BusinessContext>RECONCL</BusinessContext>
</SystemContext>
</BTEMessage>
</nm:Response >
When i try to run the XSL is failing. Please any help here?
XSL updated with working version.

How to discard repetition structure in for each loop in XSLt 2.0.

I need to map a repetitive structure to a single field in the output xml in such a way that if Success Case matches, only the required field should be mapped and In the Alternate scenario, that field should be made empty in the outgoing request.
I am facing trouble in handling the Success Flow and Alternate flow.
Success Case Input:
<Message>
<MessageName>Hello World</MessageName>
<Data>
<Partner>
<PartnerType>A</PartnerType>
<ParnterId>01</ParnterId>
</Partner>
<Partner>
<PartnerType>B</PartnerType>
<ParnterId>02</ParnterId>
</Partner>
<Partner>
<PartnerType>C</PartnerType>
<ParnterId>03</ParnterId>
</Partner>
<Partner>
<PartnerType>D</PartnerType>
<ParnterId>04</ParnterId>
</Partner>
</Data>
</Message>
Success Case Expected Output:
<Request>
<RequestName>Hello World</RequestName>
<PartnerAIdentifier>01</PartnerAIdentifier>
</Request>
Success Case Current Output:
<?xml version="1.0" encoding="UTF-8"?>
<Request>
<RequestName>Hello World</RequestName>
<PartnerAIdentifier>01</PartnerAIdentifier>
<PartnerAIdentifier/>
<PartnerAIdentifier/>
<PartnerAIdentifier/>
</Request>
Alternate Case Input:
<Message>
<MessageName>Hello World</MessageName>
<Data>
<Partner>
<PartnerType>B</PartnerType>
<ParnterId>02</ParnterId>
</Partner>
<Partner>
<PartnerType>C</PartnerType>
<ParnterId>03</ParnterId>
</Partner>
<Partner>
<PartnerType>D</PartnerType>
<ParnterId>04</ParnterId>
</Partner>
</Data>
</Message>
Alternate Case Expected Output:
<Request>
<RequestName>Hello World</RequestName>
<PartnerAIdentifier></PartnerAIdentifier>
</Request>
Alternate Case Current Output:
<?xml version="1.0" encoding="UTF-8"?>
<Request>
<RequestName>Hello World</RequestName>
<PartnerAIdentifier/>
<PartnerAIdentifier/>
<PartnerAIdentifier/>
</Request>
My XSLt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Message">
<Request>
<RequestName>
<xsl:value-of select="MessageName" />
</RequestName>
<xsl:for-each select="Data/Partner">
<xsl:choose>
<xsl:when test="PartnerType ='A'">
<PartnerAIdentifier>
<xsl:value-of select="ParnterId" />
</PartnerAIdentifier>
</xsl:when>
<xsl:otherwise>
<PartnerAIdentifier>
</PartnerAIdentifier>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</Request>
</xsl:template>
</xsl:stylesheet>
Problem:
If the Input request have a Partner element with type A, then it's Identifier should be mapped to outgoing request, and if the input request does not contain that Partner element then an single empty field should be made. But since I have to use "for-each" , So is there any additional logic which I can use to discard the repetition structure.
If you only want one PartnerAIdentifier output, there is no need to use an xsl:for-each here. You can put the test condition on the PartnerType in a single xsl:value-of
<xsl:value-of select="Data/Partner[PartnerType='A']/ParnterId" />
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Message">
<Request>
<RequestName>
<xsl:value-of select="MessageName" />
</RequestName>
<PartnerAIdentifier>
<xsl:value-of select="Data/Partner[PartnerType='A']/ParnterId" />
</PartnerAIdentifier>
</Request>
</xsl:template>
</xsl:stylesheet>
So, if there is no PartnerType then the PartnerAIdentifier will be empty as required.

Format-number from ###.0 to ###.00

I working with a project in BizTalk where use xslt to convert from and edifact file to an UBL file.
The edifact file contains price values of ###.0 and that do not work. I want to change it to ###.00 using format-number. But I cannot make it work.
This is what I have made so far:
<cbc:Value>
<xsl:variable name="SumOfNodes" select="edi:PRI/edi:C509/C50902"/>
<xsl:value-of select="format-number($SumOfNodes, '0.00')"/>
</cbc:Value>
I am using this stylesheet:
<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:edi="http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006/MEDIAMARKT"
xmlns:ubl="urn:oasis:names:specification:ubl:schema:xsd:Order-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
exclude-result-prefixes="msxsl edi">
Any ideas on how to solve this??
This may be due to undeclared namespace in your XSLT. Please check whether namespaces are declared correctly in your XSLT. Unless you show the full XSLT coding with XML coding, we cannot able to provide solution. However please refer the below Sample XML and XSLT with the output
XSLT:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="chapter">
<Value>
<xsl:variable name="num" select="number"/>
<xsl:value-of select="format-number($num,'00.00')"/>
</Value>
</xsl:template>
</xsl:stylesheet>
Sample XML
<?xml version="1.0"?>
<chapter>
<number>45</number>
</chapter>
Output
<?xml version='1.0' ?>
<Value>45.00</Value>