How to make XSL with example

Below is a XSL example used to style or format a 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" indent="yes"/>

  <!--select="DATA_DS/INVOICE_NO" <== these are invoice field paths change them accordingly -->
  
  <xsl:template match="/">
    <Invoice>
      <InvoiceNumber><xsl:value-of select="DATA_DS/INVOICE_NO"/></InvoiceNumber>
      
      <InvoiceDetails>
        <xsl:for-each select="DATA_DS/G_1">
          <Detail>
            <ProfileID><xsl:value-of select="PROFILEID"/></ProfileID>
            <UBLVersionID><xsl:value-of select="UBLVERSIONID"/></UBLVersionID>
            <ID6><xsl:value-of select="ID6"/></ID6>
            <OrgID><xsl:value-of select="ORG_ID"/></OrgID>
  <!-- add more fields -->
          </Detail>      
          <LineItems>
  
            <xsl:for-each select="G_2">
              <LineItem>
                <LineNumber><xsl:value-of select="LINE_NUMBER"/></LineNumber>
                <Description><xsl:value-of select="DESCRIPTION"/></Description>
                <TRANSLATED_DESCRIPTION><xsl:value-of select="TRANSLATED_DESCRIPTION"/></TRANSLATED_DESCRIPTION>
                <UnitCode><xsl:value-of select="UNITCODE"/></UnitCode>
  <!--  add more fields -->

              </LineItem>
            </xsl:for-each>
          </LineItems>
          
        </xsl:for-each>
      </InvoiceDetails>
      
    </Invoice>
  </xsl:template>

</xsl:stylesheet>



above xsl above is used to style below xml.


<?xml version = '1.0' encoding = 'utf-8'?>
<!--Generated by Oracle Analytics Publisher -Dataengine, datamodel:__mshahbaz_TEST_Validation_EINV_VALIDATION_xdm -->
<DATA_DS>
<INVOICE_NO>300000004942992</INVOICE_NO>
<G_1>
<PROFILEID>reporting:1.0</PROFILEID>
<UBLVERSIONID>2.1</UBLVERSIONID>
<ID6>1308-03</ID6>
<ORG_ID>300000001938441</ORG_ID>
</G_2>
<G_2>
<LINE_NUMBER>2</LINE_NUMBER>
<DESCRIPTION>Goods</DESCRIPTION>
<TRANSLATED_DESCRIPTION>NULL Line description is null </TRANSLATED_DESCRIPTION>
<UNITCODE>EA</UNITCODE>
</G_2>
<G_2>
<LINE_NUMBER>3</LINE_NUMBER>
<DESCRIPTION>Goods</DESCRIPTION>
<TRANSLATED_DESCRIPTION>NULL Line description is null </TRANSLATED_DESCRIPTION>
<UNITCODE>EA</UNITCODE>
</G_2>
</G_1>
</DATA_DS>



This is a complex xsl to add attribute in xml tags and if conditions to check values in them


<?xml version="1.0" encoding="WINDOWS-1256"?>

<xsl:stylesheet version="1.0" xmlns:ns1="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/" xmlns:xp20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20" xmlns:bpel="http://docs.oasis-open.org/wsbpel/2.0/process/executable" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="urn:un:unece:uncefact:data:specification:CoreComponentTypeSchemaModule:2" xmlns:ns4="http://www.w3.org/2000/09/xmldsig#" xmlns:bpm="http://xmlns.oracle.com/bpmn20/extensions" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:ns6="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2" xmlns:ns5="urn:oasis:names:specification:ubl:schema:xsd:UnqualifiedDataTypes-2" xmlns:ora="http://schemas.oracle.com/xpath/extension" xmlns:socket="http://www.oracle.com/XSL/Transform/java/oracle.tip.adapter.socket.ProtocolTranslator" xmlns:ns8="http://uri.etsi.org/01903/v1.4.1#" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:mhdr="http://www.oracle.com/XSL/Transform/java/oracle.tip.mediator.service.common.functions.MediatorExtnFunction" xmlns:oraext="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc" xmlns:dvm="http://www.oracle.com/XSL/Transform/java/oracle.tip.dvm.LookupValue" xmlns:ns3="http://uri.etsi.org/01903/v1.3.2#" xmlns:hwf="http://xmlns.oracle.com/bpel/workflow/xpath" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:med="http://schemas.oracle.com/mediator/xpath" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ids="http://xmlns.oracle.com/bpel/services/IdentityService/xpath" xmlns:xdk="http://schemas.oracle.com/bpel/extension/xpath/function/xdk" xmlns:xref="http://www.oracle.com/XSL/Transform/java/oracle.tip.xref.xpath.XRefXPathFunctions" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:atrb="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:bpmn="http://schemas.oracle.com/bpm/xpath" xmlns:tm="http://xmlns.oracle.com/topologyManager/client/xpath/extension" xmlns:ns7="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDataTypes-2" xmlns:ns9="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" xmlns:ldap="http://schemas.oracle.com/xpath/extension/ldap" exclude-result-prefixes="xsi xsl xsd ns1 ns2 ns4 ccts ns6 ns5 ns8 ns3 ns7 ns9 bpws xp20 bpel bpm ora socket mhdr oraext dvm hwf med ids xdk xref bpmn tm ldap atrb">
  
<xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <Invoice>
      <xsl:if test="string-length(/Invoice/G_INVOICE_NUMBER/ProfileID)&gt;0">
        <cbc:ProfileID>
          <xsl:value-of select="/Invoice/G_INVOICE_NUMBER/ProfileID"/>
        </cbc:ProfileID>
      </xsl:if>  

        <cbc:TaxAmount>
          <xsl:attribute name="currencyID">
            <xsl:value-of select="/Invoice/G_INVOICE_NUMBER/Total_Vat_CURR"/>
          </xsl:attribute>
          <xsl:value-of select="/Invoice/G_INVOICE_NUMBER/Total_Vat"/>
        </cbc:TaxAmount>
       
   <!--Line Details -->
  
      <xsl:for-each select="/Invoice/G_INVOICE_NUMBER/G_DESCRIPTION">
        <cac:InvoiceLine>
          <xsl:if test="string-length(line_number)&gt;0">
  
            <cbc:ID>
              <xsl:value-of select="line_number"/>
            </cbc:ID>
            <cbc:InvoicedQuantity>
              <xsl:attribute name="unitCode">
                <xsl:value-of select="Unitcode"/>
              </xsl:attribute>
              <xsl:value-of select="LINE_QUANTITY"/>
            </cbc:InvoicedQuantity>
          </xsl:if>
        </cac:InvoiceLine>
      </xsl:for-each>
      <!-- End of Line Details -->
    </Invoice>
  </xsl:template>
</xsl:stylesheet>

Comments

Popular posts from this blog

opaque schema xsd (standard file used in OIC)

DOCUMENTATION ON SANDBOX and PERSONALIZATION

SQL,PLSQL interview practice and DSA patterns