<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mostafa Magdy</title>
    <description>The latest articles on DEV Community by Mostafa Magdy (@engineermostafa).</description>
    <link>https://dev.to/engineermostafa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F698535%2F8a7e9cb0-6ee4-411b-ab54-782a0215c544.jpeg</url>
      <title>DEV Community: Mostafa Magdy</title>
      <link>https://dev.to/engineermostafa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/engineermostafa"/>
    <language>en</language>
    <item>
      <title>Your First Step To Write XSLT</title>
      <dc:creator>Mostafa Magdy</dc:creator>
      <pubDate>Sun, 15 May 2022 06:36:55 +0000</pubDate>
      <link>https://dev.to/engineermostafa/your-first-step-to-write-xslt-1em</link>
      <guid>https://dev.to/engineermostafa/your-first-step-to-write-xslt-1em</guid>
      <description>&lt;h2&gt;
  
  
  Introduction about XSLT 📖
&lt;/h2&gt;

&lt;p&gt;The XSLT is the abbreviation of eXtensible Stylesheet Language Transformation&lt;br&gt;
xslt is a language of transforming XML documents so we can consider it as a style sheet for XML like CSS is a style sheet for HTML, also the XSLT files extensions end with .xslt or .xsl each of them is a valid extension.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why to use XSLT 🤔
&lt;/h2&gt;

&lt;p&gt;we can transform XML file to another XML file after making some modifications, also we have the ability to convert XML file to HTML page, like this example we have some products in a category and we need to create a table with these products.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;category&amp;gt;
&amp;lt;product&amp;gt;
     &amp;lt;name&amp;gt;watch&amp;lt;/name&amp;gt;
     &amp;lt;price&amp;gt;50.60$&amp;lt;/price&amp;gt;
&amp;lt;/product&amp;gt;
&amp;lt;product&amp;gt;
      &amp;lt;name&amp;gt;IPhone 13&amp;lt;/name&amp;gt;
      &amp;lt;price&amp;gt;5055.999$&amp;lt;/price&amp;gt;
&amp;lt;/product&amp;gt;
&amp;lt;product&amp;gt;
      &amp;lt;name&amp;gt;wallet&amp;lt;/name&amp;gt;
      &amp;lt;price&amp;gt;5.10$&amp;lt;/price&amp;gt;
&amp;lt;/product&amp;gt;
&amp;lt;/category&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bYwdVIWA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnmoblv28zyzkcrm39th.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bYwdVIWA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnmoblv28zyzkcrm39th.png" alt="product - category" width="312" height="176"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  that was done just with XSLT code, so lets go a little bit deep in XSLT 👨‍💻
&lt;/h3&gt;

&lt;h2&gt;
  
  
  XSLT Basic Architecture 🧱
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Elements
&lt;/h3&gt;

&lt;p&gt;We have some element that we can use in xslt language you can find most of them in the &lt;a href="https://www.w3schools.com/xml/xsl_elementref.asp"&gt;reference&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;for-each and value-of&lt;/strong&gt;  are called elements, so as a conclusion every param after &lt;strong&gt;xsl:&lt;/strong&gt; is called element&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;xsl:for-each select="expression"&amp;gt;
  &amp;lt;!-- Content --&amp;gt;
&amp;lt;/xsl:for-each&amp;gt;
&amp;lt;xsl:value-of select="expression" disable-output-escaping="yes|no" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and by the way any word written beside the element within its scope in &amp;lt;&amp;gt; signs is called attribute so if i need to add id for each product i can put id attribute in each product element like that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;product id=1&amp;gt;
     &amp;lt;name&amp;gt;watch&amp;lt;/name&amp;gt;
     &amp;lt;price&amp;gt;50.60$&amp;lt;/price&amp;gt;
&amp;lt;/product&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but how to use these elements and how to tell the editor to include these elements and recognize them, all of these questions's answer is namespace of this language.&lt;/p&gt;

&lt;h3&gt;
  
  
  Namespace
&lt;/h3&gt;

&lt;p&gt;The first thing you should write in xslt file is the namespace and you can write it with two different ways but with the same result at the end.&lt;/p&gt;

&lt;p&gt;Also it is like adding the import package line in java.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;xsl:stylesheet&lt;/strong&gt; and &lt;strong&gt;xsl:transform&lt;/strong&gt; are completely identical in the functions &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Variables
&lt;/h3&gt;

&lt;p&gt;we can declare as usual in programming languages global or local variable but the difference here is once you set the variable's value you can't modify or change that value so it is consider as final in language like Java, PHP and Dart, let's go forward to see how to declare a variable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;xsl:variable name="name" select="expression"&amp;gt; &amp;lt;/xsl:variable&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;xsl:variable name="color" select="'red'" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the name of variable can be set in the name attribute also the value of the variable can be set in select attribute but we have one constrain here that we should put variable's value if it will be a string in quotes like red in the example not just put the value.&lt;/p&gt;

&lt;p&gt;Also we can put the value from xml’s element to variable by adding its path in select attribute, like if we need to put price of the product in a variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;xsl:variable name="price" select="category/product/price/text()" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;text() just to convert value to string like previous example.&lt;/p&gt;

&lt;h3&gt;
  
  
  Loops
&lt;/h3&gt;

&lt;p&gt;Its very easy in other language to write a for loop just write for with specific condition also here its a quite similar but with different keyword &lt;a&gt;xsl:for-each&lt;/a&gt; so it will be like that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;xsl:for-each select="expression"&amp;gt;
  &amp;lt;!-- Content --&amp;gt;
&amp;lt;/xsl:for-each&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will put in the select attribute the path of element that we need to loop in like in our category - product example we should put the path of products like that&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;xsl:for-each select="category/product"&amp;gt;.
  &amp;lt;!-- Content --&amp;gt;
&amp;lt;/xsl:for-each&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so now in this point we are ready to loop over all products with the final step to get the name of the product and the price to put them into table, but how about if we need to just show the product with specific price by checking the price if it is higher than or less than or equal to an arbitrary value? of-course as you thought, so lets go and eat a very delicious meal conditions 😅.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conditions
&lt;/h3&gt;

&lt;p&gt;We have two ways to make a conditions the first one like usual if.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;xsl:if test="expression"&amp;gt;
  ...some output if the expression is true...
&amp;lt;/xsl:if&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as we know now test is attribute, we should put our condition instead of expression in the test attribute,&lt;br&gt;
&lt;strong&gt;but&lt;/strong&gt; as not usual we have constant symbols for comparing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- &amp;amp;gt;  -&amp;gt; means greater than
- &amp;amp;lt;  -&amp;gt; means less than
-  =    -&amp;gt; means equal
- !=    -&amp;gt; means not equal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;like if we need products which prices are greater than 10$, so stuff like that can be done by using loops and conditions together as shown.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;xsl:for-each select="category/product"&amp;gt;
      &amp;lt;xsl:if test="price &amp;amp;gt; 10"&amp;gt;
             &amp;lt;!-- Content --&amp;gt;
      &amp;lt;/xsl:if&amp;gt;
    &amp;lt;/xsl:for-each&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the second way is like switch case in other languages in xslt called choose - when.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;xsl:choose&amp;gt;
  &amp;lt;xsl:when test="expression"&amp;gt;
             &amp;lt;!-- Content 1--&amp;gt;
  &amp;lt;/xsl:when&amp;gt;
  &amp;lt;xsl:otherwise&amp;gt;
             &amp;lt;!-- Content 2 --&amp;gt;
  &amp;lt;/xsl:otherwise&amp;gt;
&amp;lt;/xsl:choose&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;also we can add when statements as we need, now lets implement the condition of products but with choose - when.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;xsl:for-each select="category/product"&amp;gt;
      &amp;lt;xsl:choose&amp;gt;
        &amp;lt;xsl:when test="price &amp;amp;gt; 10"&amp;gt;
                      &amp;lt;!-- display product --&amp;gt;
        &amp;lt;/xsl:when&amp;gt;
        &amp;lt;xsl:otherwise&amp;gt;
                      &amp;lt;!-- hide product --&amp;gt;
        &amp;lt;/xsl:otherwise&amp;gt;
      &amp;lt;/xsl:choose&amp;gt;
 &amp;lt;/xsl:for-each&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but what can we do if we will write the same code in different places to make some logic but in a typical way, yes you are right use functions, lets do it 💪&lt;/p&gt;

&lt;h3&gt;
  
  
  Functions
&lt;/h3&gt;

&lt;p&gt;In c++ to write function you need to write its name and parameters also its return value type, here we just need name and parameter so you can return any type you need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;xsl:function name="functionName"&amp;gt;
    &amp;lt;xsl:param name="param1"/&amp;gt;
    &amp;lt;xsl:param name="param2"/&amp;gt;
    &amp;lt;xsl:value-of select="return value"/&amp;gt;
  &amp;lt;/xsl:function&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so it is very easy to declare a function also it is very easy to call it like that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;xsl:value-of select="functionName('param1','param2')"/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can you tell me what does this function do?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;xsl:function name="arbitraryName"&amp;gt;
    &amp;lt;xsl:param name="param1"/&amp;gt;
    &amp;lt;xsl:param name="param2"/&amp;gt;
    &amp;lt;xsl:value-of select="$param1 + param2"/&amp;gt;
  &amp;lt;/xsl:function&amp;gt;

&amp;lt;xsl:value-of select="arbitraryName('param1','param2')"/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;yes you are right just add 2 numbers.&lt;br&gt;
Great let me tell you congratulations 🎉 you are now ready to see how could we write the file that convert xml to html tables.&lt;/p&gt;

&lt;p&gt;but one step before, do you remember "&amp;lt;!--Content --" that we always wrote in if's body also in for's body? ok great, we could replace this comment with what we want like html tags if we need to convert xml file to html or xml elements whatever want, do you imagine what you will see now in the code? yes exactly just i will add some html tags that construct the tables and put some CSS styling in the tag so it will be easy peasy dude 😃.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- namespace and version --&amp;gt;
&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&amp;gt;


&amp;lt;!-- access the root element  --&amp;gt;
&amp;lt;xsl:template match="/"&amp;gt;
  &amp;lt;html&amp;gt;
    &amp;lt;body&amp;gt;
      &amp;lt;h2 align="center"&amp;gt;My Products Collection&amp;lt;/h2&amp;gt;
        &amp;lt;table border="1" align="center"&amp;gt;
          &amp;lt;tr bgcolor="#9acd32"&amp;gt;
             &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;
             &amp;lt;th&amp;gt;Price&amp;lt;/th&amp;gt;
          &amp;lt;/tr&amp;gt;

&amp;lt;!-- access the path to products  --&amp;gt;
       &amp;lt;xsl:for-each select="category/product"&amp;gt;

&amp;lt;!-- display all product without any conditions  --&amp;gt;
       &amp;lt;tr&amp;gt;
         &amp;lt;td&amp;gt;&amp;lt;xsl:value-of select="name"/&amp;gt;&amp;lt;/td&amp;gt;
         &amp;lt;td&amp;gt;&amp;lt;xsl:value-of select="price"/&amp;gt;&amp;lt;/td&amp;gt;
       &amp;lt;/tr&amp;gt;
      &amp;lt;/xsl:for-each&amp;gt;
    &amp;lt;/table&amp;gt;
  &amp;lt;/body&amp;gt;
  &amp;lt;/html&amp;gt;
&amp;lt;/xsl:template&amp;gt;

&amp;lt;/xsl:stylesheet&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bYwdVIWA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnmoblv28zyzkcrm39th.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bYwdVIWA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnmoblv28zyzkcrm39th.png" alt="product - category" width="312" height="176"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion 😃
&lt;/h2&gt;

&lt;p&gt;What we did with Xslt we can also did with any programming language but in some cases choice of xslt is more powerful and optimized than coding with your language.&lt;/p&gt;

</description>
      <category>xslt</category>
      <category>xml</category>
      <category>html</category>
      <category>css</category>
    </item>
  </channel>
</rss>
