1
votes

Input XML

<Employer>
     <Employeedetail>Sam SR Engineer 10%</ Employeedetail >
     <Employeedetail>Sam SR 10%</ Employeedetail >
     <Employeedetail>Sam SR</ Employeedetail >
</ Employer >

Below is the Output Input XML

<Employer>
 < Employeedetails >
  < Employeedetail1>
    < Employeedetail >
       <Name>Sam</ Name >
       <Grade> SR Engineer </ Grade >
       <Experience>10</ Experience >
    </ Employeedetail >
  </ Employeedetail1>
 </ Employeedetails>
</ Employer >

 <Employer>
 < Employeedetails >
  < Employeedetail1>
    < Employeedetail >
       <Name>Sam</ Name >
       <Grade>SR</ Grade >
       <Experience>10</ Experience >
    </ Employeedetail >
  </ Employeedetail1>
 </ Employeedetails>
</ Employer >

 <Employer>
 < Employeedetails >
  < Employeedetail1>
    < Employeedetail >
       <Name>Sam</ Name >
       <Grade>SR</ Grade >
       <Experience>10</ Experience >
    </ Employeedetail >
  </ Employeedetail1>
 </ Employeedetails>
</ Employer >

People from stack overflow they have helped if we have three space sperated node we will get the output

But here there is a slight twist again if you look into the grade Sr Engineer this will come into the grade but the code what I have will display SR into the Grade element and Engineer and 10 (Experience) will come in the Experience element.

If there is no value for Experience it has take by default 10.

In the above Input XML there will be number of Employee details it will come.

Here is the link for that xslt

Please help for the above question

thanks in advance

3

3 Answers

1
votes

Look into xsl:analyze-string http://www.w3.org/TR/xslt20/#analyze-string e.g. along the lines of

<xsl:template match="Employeedetail">
 <Employeedetail>
  <xsl:analyze-string="." regex="(\w+)\s+(\w+(\s\w+)?)\s+(\d+)">
    <xsl:matching-substring>
       <Name><xsl:value-of select="regex-group(1)"/></Name>
       <Grade><xsl:value-of select="regex-group(2)"/></Grade>
       <Experience><xsl:value-of select="regex-group(4)"/></Experience>
    </xsl:matching-substring>
  </xsl:analyze-string>
 </Employeedetail>
</xsl:template>

Untested but should point you in the right direction.

0
votes

Maha, I suggest you google on XSLT 1.0 string functions. This sort of problem is really trivial enough that you shouldn't be posting in StackOverflow. Yes, you may be a beginner, but that doesn't stop you from using google.

Here is a couple of starting points for you:

  1. Look at http://www.w3schools.com/xpath/xpath_functions.asp
  2. In relation to your particular question, consider functions string-before() and string-after()
0
votes

This XSLT 1.0 transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="Employeedetail">
  <xsl:variable name="vExp" select=
   "substring(., string-length()-2, 2)"/>
     <Employer>
      <Employeedetails>
        <Employeedetail1>
            <Employeedetail>
             <name><xsl:value-of select="substring-before(., ' ')"/></name>
             <grade>
               <xsl:value-of select=
               "substring-before(substring-after(., ' '), $vExp)"/>
             </grade>
             <experience><xsl:value-of select="$vExp"/></experience>
            </Employeedetail>
        </Employeedetail1>
      </Employeedetails>
     </Employer>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<Employer>
    <Employeedetail>Sam SR Engineer 10%</Employeedetail>
</Employer>

produces the wanted, correct result:

<Employer>
   <Employeedetails>
      <Employeedetail1>
         <Employeedetail>
            <name>Sam</name>
            <grade>SR Engineer </grade>
            <experience>10</experience>
         </Employeedetail>
      </Employeedetail1>
   </Employeedetails>
</Employer>