You are currently viewing XSLT <xsl:for-each> Element with Example. (Useful for B1if- SAP B1)
SAP Business One

XSLT Element with Example. (Useful for B1if- SAP B1)

You can extract that data from XML file using <xsl:for-each> Element of XSLT. This is also use full to get multiple records from the XML.

For example:

If you want to insert Business Partner using B1if then you need to pass XML file that contain the BP details as well as Address details. There is multiple address like billing address, shipping address, Main address etc. Here you can pass multiple row for the address with the request Body SOAP and using Xform Atom, you can get the data from the XML.

The Simple Example

XML data (Useful to insert Business Partner- SOAP Request Format):

<?xml version=”1.0″ encoding=”UTF-8″?>

<SOAP-ENV:Envelope xmlns:SOAP-ENV=”http://schemas.xmlsoap.org/soap/envelope/“>

<SOAP-ENV:Body>

<Data>

<BusinessPartners>

<row>

<CardCode>C10021</CardCode>

<CardName>ABC AG</CardName>

<CardType>C</CardType>

<Notes>Inserted using B1if</Notes>

<FederalTaxID>111111</FederalTaxID>

<Currency>CHF</Currency>

<FreeText>Processing testing</FreeText>

</row>

</BusinessPartners>

<BPAddresses>

<Address>

<!– multiple such elements are allowed –>

<AddressName>Shipping</AddressName>

<Street>Street 555</Street>

<ZipCode>12345</ZipCode>

<City>XXXX</City>

<Country>XX</Country>

<AddressType>bo_ShipTo</AddressType>

<AddressName2>ABC AG Main</AddressName2>

<AddressName3>Nr. XYZ</AddressName3>

<TypeOfAddress>bo_ShipTo</TypeOfAddress>

</Address>

<Address>

<AddressName>Billing</AddressName>

<Street>Street 555</Street>

<ZipCode>12345</ZipCode>

<City>XXXX</City>

<Country>XX</Country>

<AddressType>bo_BillTo</AddressType>

<AddressName2>ABC AG Main</AddressName2>

<AddressName3>Nr. XYZ</AddressName3>

<TypeOfAddress>bo_BillTo</TypeOfAddress>

</Address>

</BPAddresses>

</Data>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

XSLT (with for-each loop) – You can also use following template for Xform for DI API (BP insert using B1if)

<?xml version=”1.0″ encoding=”UTF-8″?>

<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform“>

<xsl:template name=”transform”>

<BusinessPartners xmlns=””>

<row>

<CardCode>

<xsl:value-of select=”//CardCode[text()]”></xsl:value-of>

</CardCode>

<CardName>

<xsl:value-of select=”//CardName[text()]”></xsl:value-of>

</CardName>

<CardType>

<xsl:value-of select=”//CardType[text()]”></xsl:value-of>

</CardType>

<Notes>

<xsl:value-of select=”//Notes[text()]”></xsl:value-of>

</Notes>

<FederalTaxID>

<xsl:value-of select=”//FederalTaxID[text()]”></xsl:value-of>

</FederalTaxID>

<Currency>

<xsl:value-of select=”//Currency[text()]”></xsl:value-of>

</Currency>

<FreeText>

<xsl:value-of select=”//FreeText[text()]”></xsl:value-of>

</FreeText>

</row>

</BusinessPartners>

<BPAddresses xmlns=””>

<xsl:for-each select=”BPAddresses/Address”>

<!– multiple such elements are allowed –>

<AddressName>

<xsl:value-of select=”//AddressName[text()]”></xsl:value-of>

</AddressName>

<Street>

<xsl:value-of select=”//Street[text()]”></xsl:value-of>

</Street>

<ZipCode>

<xsl:value-of select=”//ZipCode[text()]”></xsl:value-of>

</ZipCode>

<City>

<xsl:value-of select=”//City[text()]”></xsl:value-of>

</City>

<Country>

<xsl:value-of select=”//Country[text()]”></xsl:value-of>

</Country>

<AddressType>

<xsl:value-of select=”//AddressType[text()]”></xsl:value-of>

</AddressType>

<AddressName2>

<xsl:value-of select=”//AddressName2[text()]”></xsl:value-of>

</AddressName2>

<AddressName3>

<xsl:value-of select=”//AddressName3[text()]”></xsl:value-of>

</AddressName3>

<TypeOfAddress>

<xsl:value-of select=”//TypeOfAddress[text()]”></xsl:value-of>

</TypeOfAddress>

</xsl:for-each>

</BPAddresses>

</xsl:template>

</xsl:stylesheet>

The Final output:

C10021 ABC AG C Inserted using B1if 111111 CHF Processing testing Shipping Street 555 12345 XXXX XX bo_ShipTo ABC AG Main Nr. XYZ bo_ShipTo Billing Street 555 12345 XXXX XX bo_BillTo ABC AG Main Nr. XYZ bo_BillTo

This example shows that how to select the data from the XML. You can also used https://www.w3schools.com/xml/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_for-each_title for testing purpose.