2 tables to 1 table , flat ,flat to flat structure logic, in xslt





chat GPT did not give output in single shot, after asking many questions , we got answer.

<!--<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output indent="yes"/>

   <xsl:key name="person-by-id" match="//Person" use="Id"/>

   <xsl:key name="telephone-by-person-id" match="//Telephone" use="PersonId"/>

   

   <xsl:template match="/">

      <xsl:for-each select="//Person">

         <xsl:variable name="current-person-id" select="Id"/>

         <xsl:for-each select="key('telephone-by-person-id', $current-person-id)">

            <Person>

               <Id><xsl:value-of select="$current-person-id"/></Id>

               <Name><xsl:value-of select="Name"/></Name>

               <TelephoneNumber><xsl:value-of select="TelephoneNumber"/></TelephoneNumber>

            </Person>

         </xsl:for-each>

      </xsl:for-each>

   </xsl:template>

</xsl:stylesheet>


corrected code

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

   <xsl:output indent="yes"/>

   

   <xsl:key name="person-key" match="Person" use="Id"/>

   

   <xsl:template match="/">

      <xsl:apply-templates select="//TelephoneNumber"/>

   </xsl:template>

   

   <xsl:template match="//TelephoneNumber">

      <xsl:variable name="person-id" select="../PersonId"/>

      <xsl:for-each select="//Person[Id=$person-id]">

         <Person>

            <Id><xsl:value-of select="Id"/></Id>

            <Name><xsl:value-of select="Name"/></Name>

            <TelephoneNumber><xsl:value-of select="."/></TelephoneNumber>

         </Person>

      </xsl:for-each>

   </xsl:template>

</xsl:stylesheet>


again corrected as 

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

  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

  

  <xsl:key name="person-by-id" match="//Person" use="Id"/>

  

  <xsl:template match="/">

    <xsl:for-each select="//Telephone">

      <xsl:variable name="person-id" select="PersonId"/>

      <xsl:variable name="person" select="key('person-by-id', $person-id)"/>

      <Person>

        <Id><xsl:value-of select="$person/Id"/></Id>

        <Name><xsl:value-of select="$person/Name"/></Name>

        <TelephoneNumber><xsl:value-of select="TelephoneNumber"/></TelephoneNumber>

      </Person>

    </xsl:for-each>

  </xsl:template>

  

</xsl:stylesheet>



still mistaked of sorting of records

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

  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

  

  <xsl:key name="person-by-id" match="//Person" use="Id"/>

  

  <xsl:template match="/">

    <xsl:for-each select="//Telephone">

    

      <xsl:variable name="person-id" select="PersonId"/>

      <xsl:variable name="person" select="key('person-by-id', $person-id)"/>

        

      <Person>

        <Id><xsl:value-of select="$person/Id"/></Id>

        <Name><xsl:value-of select="$person/Name"/></Name>

        <TelephoneNumber><xsl:value-of select="TelephoneNumber"/></TelephoneNumber>

      </Person>

    </xsl:for-each>

  </xsl:template>

  

</xsl:stylesheet>

corrected below



still error

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

  <xsl:output method="xml" indent="yes"/>

  

  <xsl:key name="persons-by-id" match="//Person" use="Id" />

  

  <xsl:template match="/">

    <xsl:apply-templates select="//Telephone" />

  </xsl:template>

  

  <xsl:template match="Telephone">

    <xsl:variable name="person" select="key('persons-by-id', PersonId)" />

    <xsl:for-each select="$person">

      <xsl:sort select="Id" data-type="number" />

      <Person>

        <Id><xsl:value-of select="Id"/></Id>

        <Name><xsl:value-of select="Name"/></Name>

        <TelephoneNumber><xsl:value-of select="TelephoneNumber/TelephoneNumber"/></TelephoneNumber>

      </Person>

    </xsl:for-each>

  </xsl:template>

  

</xsl:stylesheet>

-->

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

   <xsl:output method="xml" indent="yes"/>

  

   <xsl:template match="/">

      <xsl:variable name="persons" select="//Person"/>

      <xsl:for-each select="//Telephone">

         <xsl:variable name="personId" select="./PersonId"/>

         <xsl:variable name="telephoneNumber" select="./TelephoneNumber"/>

         <xsl:for-each select="$persons[Id = $personId]">

            <Person>

               <Id><xsl:value-of select="./Id"/></Id>

               <Name><xsl:value-of select="./Name"/></Name>

               <TelephoneNumber><xsl:value-of select="$telephoneNumber"/></TelephoneNumber>

            </Person>

         </xsl:for-each>

      </xsl:for-each>

      <xsl:apply-templates select="//Person[not(Id = preceding-sibling::Person/Id)]">

         <xsl:sort select="./Id"/>

      </xsl:apply-templates>

   </xsl:template>

   

   <xsl:template match="Person">

      <Person>

         <Id><xsl:value-of select="./Id"/></Id>

         <Name><xsl:value-of select="./Name"/></Name>

         <xsl:for-each select="//Telephone[PersonId = current()/Id]">

            <TelephoneNumber><xsl:value-of select="./TelephoneNumber"/></TelephoneNumber>

         </xsl:for-each>

      </Person>

   </xsl:template>

   

</xsl:stylesheet>





Popular posts from this blog

pss book : శ్రీకృష్ణుడు దేవుడా, భగవంతుడా completed , second review needed. 26th April 2024

pss book: గురు ప్రార్థనామంజరి . completed 21st july 2024

pss book: కధల జ్ఞానము read review pending. 25th june 2024