XSL -- I\'m sure this is simple but I can\'t getting to do what I need to. Will
ID: 3804477 • Letter: X
Question
XSL -- I'm sure this is simple but I can't getting to do what I need to. Will appreciate some help. What it is supposed to happen is in the comments and below is what I have done. Below the stye sheet, I copied 3 of xml files as examples for debuging. Appreciate it as I can't figure it out.
<!-- Have MULTIPLE xml documents. Each file name is formatted with the word dept plus the department number and .xml(depta00.xml). Need to create a horizons.xml file with the data from all of the xml files that lists all the employees from each deaprtment.
Each document is formated the same way...
I need to do the following within the style sheet:
1. Create a template named getEmployees
2. Within getPeople template, create a variable named depts containing a sequence of the following text strings representing the deaprtment codes: 'a00','c01','d11','d21','e11', and 'e21'
3. After the line to create the depts variable, create the departments element.
4. within the departments element, insert a for-each loop that loops through each entry in the depts sequence.
6. For each entry in the depts sequence do the following:
a. Create a variable name currentDept equal to the current item in the depts sequence.
b. Create an element department with an attribute named deptID whose value is equal to the value of the currentDept variable.
c. Use the doc() function to reference the "deptcurrent.xml" file, where current is the value of the currentDept variable. Use concat() function to combine the text strings for "dept", the currentDept variable, and teh text string ".xml".
d. Use the copy-of element to copy the contents of the employees element and its descendants to the department element.
7. Save all changes and use XSLT 2.0 processor to generate the result document horizons.xml by applying
the getEmployees template within the alldepartments.xsl style sheet.
-->
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template name ="getEmployees">
<xsl:variable name="depts" select="('a00','c01','d11','d21','e11', 'e21')" as="xs:string*" />
<xsl:element name="departments">
<xsl:for-each select="$depts"> <xsl:value-of select="." />
<xsl:variable name="currentDept"> <xsl:value-of select="$depts" /> </xsl:variable>
<xsl:element name="department">
<xsl:attribute name="deptID"> <xsl:value-of select="$currentDept" /> </xsl:attribute>
<xsl:value-of select="doc(concat('dept',$currentDept, '.xml'))" />
<xsl:copy-of select="employees" />
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Employee Information from Department A00
Filename: depta00.xml
-->
<?xml-stylesheet type="text/xsl" href="alldepartments.xsl"?>
<employees>
<employee empID="10">
<firstName>Marylin</firstName>
<middleInt>A</middleInt>
<lastName>Johnson</lastName>
<department>A00</department>
<phone>3978</phone>
<email>Johnson.60@example.com/horizons</email>
<dateHired>2000-01-01</dateHired>
<title>President</title>
<edLevel>18</edLevel>
<gender>F</gender>
<birthDate>1968-08-24</birthDate>
<salary>121300</salary>
<bonus>2300</bonus>
<commission>9700</commission>
</employee>
<employee empID="40">
<firstName>Heather</firstName>
<middleInt>D</middleInt>
<lastName>Gordon</lastName>
<department>A00</department>
<phone>3915</phone>
<email>Gordon.59@example.com/horizons</email>
<dateHired>2009-03-01</dateHired>
<title>Manager</title>
<edLevel>18</edLevel>
<gender>F</gender>
<birthDate>1986-06-03</birthDate>
<salary>85400</salary>
<bonus>1700</bonus>
<commission>6500</commission>
</employee>
</employees>
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Employee Information from Department E11
Filename: depte11.xml
-->
<?xml-stylesheet type="text/xsl" href="alldepartments.xsl"?>
<employees>
<employee empID="50">
<firstName>Alicia</firstName>
<middleInt>J</middleInt>
<lastName>Silva</lastName>
<department>E11</department>
<phone>6789</phone>
<email>Silva.50@example.com/horizons</email>
<dateHired>2013-08-17</dateHired>
<title>Manager</title>
<edLevel>16</edLevel>
<gender>M</gender>
<birthDate>1960-09-15</birthDate>
<salary>92400</salary>
<bonus>1800</bonus>
<commission>7400</commission>
</employee>
<employee empID="90">
<firstName>Ila</firstName>
<middleInt>R</middleInt>
<lastName>Leclerc</lastName>
<department>E11</department>
<phone>5498</phone>
<email>Leclerc.51@example.com/horizons</email>
<dateHired>2005-08-15</dateHired>
<title>Manager</title>
<edLevel>16</edLevel>
<gender>F</gender>
<birthDate>1976-05-15</birthDate>
<salary>68400</salary>
<bonus>1400</bonus>
<commission>5500</commission>
</employee>
</employees>
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Employee Information from Department D21
Filename: deptd21.xml
-->
<?xml-stylesheet type="text/xsl" href="alldepartments.xsl"?>
<employees>
<employee empID="70">
<firstName>William</firstName>
<middleInt>P</middleInt>
<lastName>Kimble</lastName>
<department>D21</department>
<phone>7831</phone>
<email>Kimble.87@example.com/horizons</email>
<dateHired>2015-09-30</dateHired>
<title>Manager</title>
<edLevel>16</edLevel>
<gender>F</gender>
<birthDate>1988-05-26</birthDate>
<salary>83200</salary>
<bonus>1600</bonus>
<commission>6700</commission>
</employee>
<employee empID="230">
<firstName>Kevin</firstName>
<middleInt>J</middleInt>
<lastName>Smith</lastName>
<department>D21</department>
<phone>2094</phone>
<email>Smith.77@example.com/horizons</email>
<dateHired>2001-11-21</dateHired>
<title>Clerk</title>
<edLevel>14</edLevel>
<gender>M</gender>
<birthDate>1970-05-30</birthDate>
<salary>51000</salary>
<bonus>900</bonus>
<commission>4100</commission>
</employee>
<employee empID="240">
<firstName>Patrick</firstName>
<middleInt>M</middleInt>
<lastName>Trexler</lastName>
<department>D21</department>
<phone>3780</phone>
<email>Trexler.63@example.com/horizons</email>
<dateHired>2014-12-05</dateHired>
<title>Clerk</title>
<edLevel>17</edLevel>
<gender>M</gender>
<birthDate>1989-03-31</birthDate>
<salary>66100</salary>
<bonus>1400</bonus>
<commission>5300</commission>
</employee>
</employees>
Explanation / Answer
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template name ="getEmployees">
<xsl:variable name="depts" select="('a00','c01','d11','d21','e11', 'e21')" as="xs:string*" />
<xsl:element name="departments">
<xsl:for-each select="$depts"> <xsl:value-of select="." />
<xsl:variable name="currentDept"> <xsl:value-of select="$depts" /> </xsl:variable>
<xsl:element name="department">
<xsl:attribute name="deptID"> <xsl:value-of select="$currentDept" /> </xsl:attribute>
<xsl:value-of select="doc(concat('dept',$currentDept, '.xml'))" />
<xsl:copy-of select="employees" />
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Employee Information from Department A00
Filename: depta00.xml
-->
<?xml-stylesheet type="text/xsl" href="alldepartments.xsl"?>
<employees>
<employee empID="10">
<firstName>Marylin</firstName>
<middleInt>A</middleInt>
<lastName>Johnson</lastName>
<department>A00</department>
<phone>3978</phone>
<email>Johnson.60@example.com/horizons</email>
<dateHired>2000-01-01</dateHired>
<title>President</title>
<edLevel>18</edLevel>
<gender>F</gender>
<birthDate>1968-08-24</birthDate>
<salary>121300</salary>
<bonus>2300</bonus>
<commission>9700</commission>
</employee>
<employee empID="40">
<firstName>Heather</firstName>
<middleInt>D</middleInt>
<lastName>Gordon</lastName>
<department>A00</department>
<phone>3915</phone>
<email>Gordon.59@example.com/horizons</email>
<dateHired>2009-03-01</dateHired>
<title>Manager</title>
<edLevel>18</edLevel>
<gender>F</gender>
<birthDate>1986-06-03</birthDate>
<salary>85400</salary>
<bonus>1700</bonus>
<commission>6500</commission>
</employee>
</employees>
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Employee Information from Department E11
Filename: depte11.xml
-->
<?xml-stylesheet type="text/xsl" href="alldepartments.xsl"?>
<employees>
<employee empID="50">
<firstName>Alicia</firstName>
<middleInt>J</middleInt>
<lastName>Silva</lastName>
<department>E11</department>
<phone>6789</phone>
<email>Silva.50@example.com/horizons</email>
<dateHired>2013-08-17</dateHired>
<title>Manager</title>
<edLevel>16</edLevel>
<gender>M</gender>
<birthDate>1960-09-15</birthDate>
<salary>92400</salary>
<bonus>1800</bonus>
<commission>7400</commission>
</employee>
<employee empID="90">
<firstName>Ila</firstName>
<middleInt>R</middleInt>
<lastName>Leclerc</lastName>
<department>E11</department>
<phone>5498</phone>
<email>Leclerc.51@example.com/horizons</email>
<dateHired>2005-08-15</dateHired>
<title>Manager</title>
<edLevel>16</edLevel>
<gender>F</gender>
<birthDate>1976-05-15</birthDate>
<salary>68400</salary>
<bonus>1400</bonus>
<commission>5500</commission>
</employee>
</employees>
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Employee Information from Department D21
Filename: deptd21.xml
-->
<?xml-stylesheet type="text/xsl" href="alldepartments.xsl"?>
<employees>
<employee empID="70">
<firstName>William</firstName>
<middleInt>P</middleInt>
<lastName>Kimble</lastName>
<department>D21</department>
<phone>7831</phone>
<email>Kimble.87@example.com/horizons</email>
<dateHired>2015-09-30</dateHired>
<title>Manager</title>
<edLevel>16</edLevel>
<gender>F</gender>
<birthDate>1988-05-26</birthDate>
<salary>83200</salary>
<bonus>1600</bonus>
<commission>6700</commission>
</employee>
<employee empID="230">
<firstName>Kevin</firstName>
<middleInt>J</middleInt>
<lastName>Smith</lastName>
<department>D21</department>
<phone>2094</phone>
<email>Smith.77@example.com/horizons</email>
<dateHired>2001-11-21</dateHired>
<title>Clerk</title>
<edLevel>14</edLevel>
<gender>M</gender>
<birthDate>1970-05-30</birthDate>
<salary>51000</salary>
<bonus>900</bonus>
<commission>4100</commission>
</employee>
<employee empID="240">
<firstName>Patrick</firstName>
<middleInt>M</middleInt>
<lastName>Trexler</lastName>
<department>D21</department>
<phone>3780</phone>
<email>Trexler.63@example.com/horizons</email>
<dateHired>2014-12-05</dateHired>
<title>Clerk</title>
<edLevel>17</edLevel>
<gender>M</gender>
<birthDate>1989-03-31</birthDate>
<salary>66100</salary>
<bonus>1400</bonus>
<commission>5300</commission>
</employee>
</employees>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.