Declaring a variable
<xsl:variable name="CanonicalHeader" select="/n0:CanonicalPurchaseOrderAck/Header"/>
Fetching the value in XSL tag
<xsl:value-of select="$CanonicalHeader/PurchaseOrderNumber"/>
If condition in XSL
<xsl:value-of select="$CanonicalHeader/Addresses/Name[EntityIdentifierCode1 = 'ST']/IdentificationCode"/>
or
<xsl:value-of select="/ns0:X12_00401_210/ns0:LXLoop1/ns0:L5[L502='FUEL SURCHARGE']/../ns0:L1/L104/text() div 100"/>
If - Else (Example 1)
<xsl:choose>
<xsl:when test="$CanonicalHeader/Addresses/Name[EntityIdentifierCode1 = 'ST']/IdentificationCode != ''">
<xsl:value-of select="$CanonicalHeader/Addresses/Name[EntityIdentifierCode1 = 'ST']/IdentificationCode"/>
</xsl:when>
<xsl:otherwise>
<Error>
Missing EntityIdentifierCode1 ST
</Error>
</xsl:otherwise>
</xsl:choose>
If - Else (Example 2)
<xsl:choose>
<xsl:when test="$CanonicalHeader/Dates[DateTimeQualifier = '004']/Date != ''">
<xsl:value-of select="$CanonicalHeader/Dates[DateTimeQualifier = '004']/Date"/>
</xsl:when>
<xsl:otherwise>
<Error>
Missing DateTimeQualifier 004
</Error>
</xsl:otherwise>
</xsl:choose>
Taking 1st/2nd from n tags
<xsl:variable name="FromAddressCity" select="/ns0:X12_00401_210/ns0:LXLoop1/ns0:N1Loop3/ns0:N4_3/N401"/>
<xsl:value-of select="$FromAddressCity[1]"/>
Current time
<CreatedDateTime>
<xsl:value-of select="current-dateTime()"/>
</CreatedDateTime>
For each loop
<AccessorialCharges>
<xsl:for-each select="./ns0:X12_00401_210/ns0:LXLoop1/ns0:L5">
<AccessorialCharge>
<Code>
<xsl:value-of select="../ns0:L5/L502"/>
</Code>
<Description>
<xsl:value-of select="../ns0:L5/L502"/>
</Description>
<Charge>
<xsl:value-of select="../ns0:L1/L104 div 100"/>
</Charge>
</AccessorialCharge>
</xsl:for-each>
</AccessorialCharges>
Copy and paste the below code into notepad++ for better visualization.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns:ns0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name='Valuee' select="/ns0:X12_00401_110/ns0:B3/B302"/>
<xsl:template match="/">
<ns0:Output>
<ns0:Header>
<ns0:Invoice_Number>
<xsl:value-of select="/ns0:X12_00401_110/ns0:B3/B302"/>
</ns0:Invoice_Number>
<ns0:Vendor_Code>
<xsl:value-of select="'U100004'"/>
</ns0:Vendor_Code>
<ns0:Comm_Date>
<xsl:value-of select="/ns0:X12_00401_110/ns0:B3/B312"/>
</ns0:Comm_Date>
<ns0:Total_Inv_Amt>
<xsl:value-of select="/ns0:X12_00401_110/ns0:B3/B307 div 100"/>
</ns0:Total_Inv_Amt>
<ns0:Inv_Date>
<xsl:value-of select="/ns0:X12_00401_110/ns0:B3/B312"/>
</ns0:Inv_Date>
<ns0:Detail>
<ns0:Invoice_Number>
<xsl:value-of select="/ns0:X12_00401_110/ns0:B3/B302"/>
</ns0:Invoice_Number>
<ns0:Vendor_Code>
<xsl:value-of select="'U100004'"/>
</ns0:Vendor_Code>
<ns0:Comm_Date>
<xsl:value-of select="/ns0:X12_00401_110/ns0:B3/B312"/>
</ns0:Comm_Date>
<ns0:Inv_Date>
<xsl:value-of select="/ns0:X12_00401_110/ns0:B3/B312"/>
</ns0:Inv_Date>
<ns0:Invoice_Line_No>
<xsl:value-of select="/ns0:X12_00401_110/ns0:B3A/B3A02"/>
</ns0:Invoice_Line_No>
<ns0:Frt_Amt>
<xsl:value-of select="/ns0:X12_00401_110/ns0:B3/B307 div 100"/>
</ns0:Frt_Amt>
<ns0:AirWayBill>
<xsl:value-of select="/ns0:X12_00401_110/ns0:LXLoop1/ns0:N1Loop2/ns0:N9_2[N901 = 'AW']/N902/text()"/>
</ns0:AirWayBill>
<ns0:Child_Acct_No>
<xsl:value-of select="/ns0:X12_00401_110/ns0:LXLoop1/ns0:N1Loop2/ns0:N9_2[N901='IT']/N902/text()"/>
</ns0:Child_Acct_No>
<ns0:Parent_Acct_No>
<xsl:value-of select="/ns0:X12_00401_110/ns0:N1Loop1/ns0:N9[N901='14']/N902/text()"/>
</ns0:Parent_Acct_No>
<xsl:choose>
<xsl:when test="contains(./X12_00401_110/ns0:N9/N902,'SALE') or contains(./X12_00401_110/ns0:N9/N902,'Sale')">
<xsl:variable name='ValueUSO' select="substring(./X12_00401_110/ns0:N9/N902,8,3)"/> <!--set value-->
<xsl:choose>
<xsl:when test="($ValueUSO='USO')">
<xsl:variable name='ValueUSO2' select="substring(./X12_00401_110/ns0:N9/N902,12,8)"/> <!--set value-->
<xsl:variable name='CustRef' select="concat('USO-',$ValueUSO2)" />
<ns0:Cust_Ref>
<xsl:value-of select="$CustRef"/>
</ns0:Cust_Ref>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="contains(./X12_00401_110/ns0:N9/N902,'USR') or contains(./X12_00401_110/ns0:N9/N902,'Usr')">
<xsl:variable name='CustRef' select="substring(./X12_00401_110/ns0:N9/N902,8,100)" />
<ns0:Cust_Ref>
<xsl:value-of select="$CustRef"/>
</ns0:Cust_Ref>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
</xsl:choose>
<xsl:for-each select="./ns0:X12_00401_110/ns0:LXLoop1/ns0:L5Loop1/ns0:L1Loop1">
<ns0:Charge>
<ns0:Invoice_Number>
<xsl:value-of select="../../../ns0:B3/B302"/>
</ns0:Invoice_Number>
<ns0:Vendor_Code>
<xsl:value-of select="'U100004'"/>
</ns0:Vendor_Code>
<ns0:Comm_Date>
<xsl:value-of select="../../../ns0:B3/B312"/>
</ns0:Comm_Date>
<ns0:Inv_Date>
<xsl:value-of select="../../../ns0:B3/B312"/>
</ns0:Inv_Date>
<ns0:Charge_Amt>
<xsl:value-of select="ns0:L1/L104 div 100"/>
</ns0:Charge_Amt>
<ns0:Charge_Code>
<xsl:value-of select="ns0:L1/L108"/>
</ns0:Charge_Code>
</ns0:Charge>
</xsl:for-each>
</ns0:Detail>
</ns0:Header>
</ns0:Output>
</xsl:template>
</xsl:stylesheet>