#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    XSLT Code for complex XML document


    I have an xml document and I want to transform it to add an extra node element depending on some condition. Example given below. I want to write an .xsl program for the requirement "If a does not have , use the of the immediate one level up". Basically I want to add node if does not have one, and that value should be of the person one above it in my case. The values for

    Tommy Trojans - Engineering
    Ronald Jackson - Engineering
    Justin Jones - Marketing

    Can anyone please advice on how to do this??

    Thanks,
    Ace




    <?xml version="1.0" encoding="UTF-8"?>
    -<Data> -<People> -<Person id="1"> <name>John Taylor</name> <title>Manager</title> <department>Engineering</department> -<reports> -<report id="2"> <name>Tommy Trojans</name> <title>Engineer</title> </report> -<report id="3"> <name>Mike Lee</name> <title>Designer</title> <department>UI/UX</department> </report> -<report id="4"> <name>Ronald Jackson</name> <title>Engineer</title> </report> </reports> </Person> -<Person id="16"> <name>Maria Sanchez</name> <title>CEO</title> </Person> -<Person id="10"> <name>Matt Jones</name> <title>Director</title> <department>Marketing</department> -<reports> -<report id="9"> <name>Justin Jones</name> <title>Manager</title> </report> </reports> </Person> </People> -<Departments> -<Department> <name>Engineering</name> <rate max="100" min="10" type="hourly"/> <rate max="12000" min="1000" type="monthly"/> </Department> -<Department> <name>UI/UX</name> <rate max="50" min="12" type="hourly"/> <rate max="9000" min="1500" type="monthly"/> <rate max="2000" min="300" type="weekly"/> <rate max="100000" min="25000" type="yearly"/> </Department> -<Department> <name>Marketing</name> <rate max="90000" min="30000" type="yearly"/> <rate max="5000" min="500" type="weekly"/> </Department> </Departments> </Data>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    191
    Rep Power
    49
    controlled by department

    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output indent="yes" method="text"/>
    	<xsl:template match="/">
    		<xsl:text>output &#xA;</xsl:text>
    		<xsl:apply-templates select="Data/Departments"/>
    	</xsl:template>
    	<xsl:template match="Departments">
    		<xsl:apply-templates select="Department"/>
    	</xsl:template>
    	<xsl:template match="Department">
    		<xsl:value-of select="concat('work in ',name,'&#xA;')"/>
    		<xsl:apply-templates select="name"/>
    	</xsl:template>
    	<xsl:template match="name">
    		<xsl:variable name="test" select="."/>
    			<xsl:apply-templates select="//Data/People/Person/department[.=$test]/../reports"/>
    	</xsl:template>
    	<xsl:template match="reports">
    		<xsl:apply-templates select="report"/>
    	</xsl:template>
    	<xsl:template match="report">
    		<xsl:value-of select="concat(name,' ')"/>
    		<xsl:value-of select="concat(title,'&#xA;')"/>
    	</xsl:template>
    </xsl:stylesheet>
    output
    work in Engineering
    Tommy Trojans Engineer
    Mike Lee Designer
    Ronald Jackson Engineer
    work in UI/UX
    work in CEO
    work in Marketing
    Justin Jones Manager
    Helmut Hagemann Germany

    fallen to the bottom of the facts?
    I reach my hand and we go together


    wer lesen und google kann ist klar im Vorteil
    who read and google is able is clear in the advantage
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2006
    Posts
    24
    Rep Power
    0
    An alternative for your consideration that gives same/similar result

    input.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <Data>
      <People>
        <Person id="1">
          <name>John Taylor</name>
          <title>Manager</title>
          <department>Engineering</department>
          <reports>
            <report id="2">
              <name>Tommy Trojans</name>
              <title>Engineer</title>
            </report>
            <report id="3">
              <name>Mike Lee</name>
              <title>Designer</title>
              <department>UI/UX</department>
            </report>
            <report id="4">
              <name>Ronald Jackson</name>
              <title>Engineer</title>
            </report>
          </reports>
        </Person>
        <Person id="16">
          <name>Maria Sanchez</name>
          <title>CEO</title>
        </Person>
        <Person id="10">
          <name>Matt Jones</name>
          <title>Director</title>
          <department>Marketing</department>
          <reports>
            <report id="9">
              <name>Justin Jones</name>
              <title>Manager</title>
            </report>
          </reports>
        </Person>
      </People>
      <Departments>
        <Department>
          <name>Engineering</name>
          <rate max="100" min="10" type="hourly"/>
          <rate max="12000" min="1000" type="monthly"/>
        </Department>
        <Department>
          <name>UI/UX</name>
          <rate max="50" min="12" type="hourly"/>
          <rate max="9000" min="1500" type="monthly"/>
          <rate max="2000" min="300" type="weekly"/>
          <rate max="100000" min="25000" type="yearly"/>
        </Department>
        <Department>
          <name>Marketing</name>
          <rate max="90000" min="30000" type="yearly"/>
          <rate max="5000" min="500" type="weekly"/>
        </Department>
      </Departments>
    </Data>
    using xsl
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <xsl:output method="text" indent="yes"/>
    
      <xsl:template match="/">
        <xsl:apply-templates select="node()|@*"/>
      </xsl:template>
    
      <xsl:template match="node()">
        <xsl:apply-templates select="node()|@*"/>
      </xsl:template>
    
      <xsl:template match="@*">
    	</xsl:template>
    
      <xsl:template match="name[parent::report][following-sibling::department]">
        <xsl:value-of select="text()"/> - <xsl:value-of select="./following-sibling::department[1]"/>
        <xsl:text>
    </xsl:text>
      </xsl:template>
    
      <xsl:template match="name[parent::report][not(following-sibling::department)]">
        <xsl:value-of select="text()"/> - <xsl:value-of select="ancestor::Person/department[1]"/>
        <xsl:text>
    </xsl:text>
      </xsl:template>
    </xsl:stylesheet>
    result
    Code:
    Tommy Trojans - Engineering
    Mike Lee - UI/UX
    Ronald Jackson - Engineering
    Justin Jones - Marketing

IMN logo majestic logo threadwatch logo seochat tools logo