hi all,

I am new to this forum

Right now I am working on XML & XSLT

Below is my Input XMl file

**<?xml version="1.0" encoding="UTF-8"?>    
<Employer>
    <Employees>
        <EmployeesDetails>van ind 26%</EmployeesDetails>
    </Employees>    
    <Employees>
        <EmployeesDetails>van ind</EmployeesDetails>
    </Employees>    
</Employer>**

and I need output as

<?xml version="1.0" encoding="UTF-8"?>
<Employer>
    <Employees>
        <Names>van</Names>
        <Location>ind</Location>                
        <Weather>26</Weather>
    </Employees>
    <Employees>
        <Names>van</Names>
        <Location>ind</Location>
        <Weather>100</Weather>
    </Employees>
</Employer>

The twist is XSLT Should check for whether element it has value or not first checking for example
if i have the value <weather>26%</weather> the output should be <weather>26</weather> else if there is no weather element in the given xml file it has crate <weather>100</weather>.

Can any help me here how to do it in XSLT 1.0

Thanks in advance
M

Input XML

<?xml version="1.0" encoding="UTF-8"?>
<Employer>
  <Employees>
    <EmployeesDetails>van ind 26%</EmployeesDetails>
  </Employees>
  <Employees>
    <EmployeesDetails>van ind</EmployeesDetails>
  </Employees>
</Employer>

XSLT Sheet

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

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="EmployeesDetails/text()" name="tokenize">
    <xsl:param name="text" select="."/>
    <xsl:param name="separator" select="' '"/>
    <xsl:param name="nodename">Names</xsl:param>
    <xsl:choose>
      <xsl:when test="not(contains($text, $separator))">
        <xsl:choose>
          <xsl:when test="(contains($text, '%'))">
            <xsl:element name ="Weather">
              <xsl:value-of select="normalize-space(substring-before($text, '%'))"/>
            </xsl:element>
          </xsl:when>
          <xsl:otherwise>
            <xsl:element name ="{$nodename}">
              <xsl:value-of select="normalize-space($text)"/>
            </xsl:element>
            <xsl:element name="Weather">100</xsl:element>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:element name ="{$nodename}">
          <xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
        </xsl:element>
        <xsl:call-template name="tokenize">
          <xsl:with-param name="text" select="substring-after($text, $separator)"/>
          <xsl:with-param name ="nodename">Location</xsl:with-param>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

Output

<?xml version="1.0" encoding="utf-8"?>
<Employer>
  <Employees>
    <EmployeesDetails>
      <Names>van</Names>
      <Location>ind</Location>
      <Weather>26</Weather>
    </EmployeesDetails>
  </Employees>
  <Employees>
    <EmployeesDetails>
      <Names>van</Names>
      <Location>ind</Location>
      <Weather>100</Weather>
    </EmployeesDetails>
  </Employees>
</Employer>

Splitting a string into new nodes is not simple in XSLT 1.0, it requires a recursive function, it is further complicated by the element naming requirements and so this function is tailor written for those three names alone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.