K
K
krasnovro2019-12-28 01:42:21
XSL & XSLT
krasnovro, 2019-12-28 01:42:21

Reading a multi-level xml file, how to read it correctly (XSLT)?

There is this xml file:

<?xml version="1.0" encoding="utf-8" ?>
<EmployeeData>  
    <Department id = "1">
    <Name>WEB</Name>
    <Employee id = "1">
      <FirstName>Константин</FirstName>
      <LastName>Васильев</LastName>
      <MiddleName>Иванович</MiddleName>
      <Gender>М</Gender>
      <BirthDate>27.09.2000</BirthDate>
      <DateOfEmployeement>22.07.2019</DateOfEmployeement>
      <Post>Джуниор</Post>
      
      <Project id = "1">
        <Name>Соц портал</Name>
        <Time>300</Time>
        <Price>7000$</Price>
      </Project>
      
    </Employee>
    <Employee id = "3">
      <FirstName>Василий</FirstName>
      <LastName>Михайлов</LastName>
      <MiddleName>Юрьевич</MiddleName>
      <Gender>М</Gender>
      <BirthDate>01.09.1990</BirthDate>
      <DateOfEmployeement>21.02.2005</DateOfEmployeement>
      <Post>Синъер</Post>
      
      <Project id = "1">
        <Name>Соц портал</Name>
        <Time>300</Time>
        <Price>7000$</Price>
      </Project>
      
    </Employee>
    </Department>

    <Department id = "2">
    <Name>Тех поддержка</Name>
    <Employee id = "2">
      <FirstName>Ирина</FirstName>
      <LastName>Петрова</LastName>
      <MiddleName>Петровна</MiddleName>
      <Gender>Ж</Gender>
      <BirthDate>03.08.1998</BirthDate>
      <DateOfEmployeement>04.05.2017</DateOfEmployeement>
      <Post>Оператор</Post>
      
      <Project id = "2">
        <Name>WEB</Name>
        <Time>300</Time>
        <Price>7000$</Price>
      </Project>
      
    </Employee>
  </Department>
</EmployeeData>

I need to read it correctly and display it in html
Here is the xslt code that I wrote:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="EmployeeData">
    <html>
      <head>
        <style type="text/css">
              body {
              background-color: rgb(255, 248, 220);
              }
              h1, h2 {
              text-align: center;
              }
              table {
              border:thin solid rgh(128, 0, 0);
              }
              thead {
              font-weight: bold;
              text-align: center;
              }
              td {
              padding: 5 px;
              border: thin solid black;
              }
              ul {
              font-weight: bold;
              }
              ol {
              font-style: italic;
              forecolor;
              }
            </style>
        <title>Список отделов</title>
      </head>
      <body>
        <h1>Данные о проектах</h1>
        <ul>
        <xsl:for-each select="//Department">
          <li>
           Отдел: Код:
          <xsl:value-of select="@id"/>
            Название:
          <xsl:value-of select="Name"/>>
          
          <ol>
            Сотрудники: ФИО:
            <xsl:value-of select="Employee/FirstName"/>
            <xsl:value-of select="Employee/LastName"/>
            <xsl:value-of select="Employee/MiddleName"/>
            Пол:
            <xsl:value-of select="Employee/Gender"/>
            Дата рождения:
            <xsl:value-of select="Employee/BirthDate"/>
          </ol>
            
          </li>
      </xsl:for-each>
        
        </ul>
        <h2>Данные о сотрудниках</h2>
        <table>
          <thead>
            <tr bgcolor="#9acd32">
              <td style="text-align:left">Имя</td>
              <td style="text-align:left">Фамилия</td>
              <td style="text-align:left">Отчество</td>
              <td style="text-align:left">Должность</td>
              <td style="text-align:left">КодПроекта</td>
              <td style="text-align:left">Проект</td>
              <td style="text-align:left">ВремяПроекта</td>
            </tr>
          </thead>
          <tbody>
            <xsl:for-each select="//Department">
            <tr>
              <td>
                <xsl:value-of select="Employee/FirstName"/>
              </td>
              <td>
                <xsl:value-of select="Employee/LastName"/>
              </td>
              <td>
                <xsl:value-of select="Employee/MiddleName"/>
              </td>
              <td>
                <xsl:value-of select="Employee/Post"/>
              </td>
              <td>
                <xsl:value-of select="Employee/Project/@id"/>
              </td>
              <td>
                <xsl:value-of select="Employee/Project/Name"/>
              </td>
              <td>
                <xsl:value-of select="Employee/Project/Time"/>
              </td>
            </tr>
          </xsl:for-each>
          </tbody>
        </table>
      </body>
    </html>
  
   </xsl:template>        
</xsl:stylesheet>

As a result of execution, I get an output that is not quite correct:
5e068869968ed980110050.png
that is, it displays one employee from each department, and does not iterate over all the employees of this department
. What am I doing wrong

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lev Zabudkin, 2019-12-28
@krasnovro

Oh damn
it And this https://www.php.net/manual/ru/book.xsl.php
does not help?
ps: put me a plus or where here so that I can answer more than once.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question