T
T
tj572019-03-25 16:19:17
XSL & XSLT
tj57, 2019-03-25 16:19:17

How to do xslt transformation for long xml files?

I recently got acquainted with xslt-transformations. I have a task to make a transformation for an xml file so that the data is displayed in the form of tables. Small snippet example:
test.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>

<root>
  <APPLICATION ID="88deeba4-69b1-454a-992a-fae3cebfbca1" TYPENAME="MTZMetaModel" NAME="Спец:Метамодель" IsLocked="LockPermanent" WorkOffline="False" STATUS="00000000-0000-0000-0000-000000000000" SecureStyleID="00000000-0000-0000-0000-000000000000">
    <MTZAPP_COL IsLocked="0">
      <MTZAPP ID="ff0e4214-a70d-484c-8b80-0ff708b1837c" Deleted="False" IsLocked="0" RetriveTime="636886968601287765" ChangeTime="599264352000000000" AccessTime="599264352000000000" SecureStyleID="00000000-0000-0000-0000-000000000000" Name="Администрирование" DBName="" TheComment="">
        <ParentPackage_COL IsLocked="0" />
      </MTZAPP>
      <MTZAPP ID="a719b764-60a2-4090-bf9b-0ca9501b8da8" Deleted="False" IsLocked="0" RetriveTime="636886968601287765" ChangeTime="599264352000000000" AccessTime="599264352000000000" SecureStyleID="00000000-0000-0000-0000-000000000000" Name="Ядро" DBName="" TheComment="">
        <ParentPackage_COL IsLocked="0" />
      </MTZAPP>
      <MTZAPP ID="0290e99f-62ec-4012-9fed-3589d8fd1590" Deleted="False" IsLocked="0" RetriveTime="636886968601287765" ChangeTime="599264352000000000" AccessTime="599264352000000000" SecureStyleID="00000000-0000-0000-0000-000000000000" Name="BP3" DBName="" TheComment="Среда быстрого прототипирования V3">
        <ParentPackage_COL IsLocked="0" />
      </MTZAPP>
      <MTZAPP ID="46ed2fa7-4f35-4527-91a6-a61cfe1c3905" Deleted="False" IsLocked="0" RetriveTime="636886968601287765" ChangeTime="599264352000000000" AccessTime="599264352000000000" SecureStyleID="00000000-0000-0000-0000-000000000000" Name="iUROK" DBName="" TheComment="">
        <ParentPackage_COL IsLocked="0" />
      </MTZAPP>
      <MTZAPP ID="946f9ece-7281-488f-99d0-3a4f64da5c1d" Deleted="False" IsLocked="0" RetriveTime="636886968601287765" ChangeTime="599264352000000000" AccessTime="599264352000000000" SecureStyleID="00000000-0000-0000-0000-000000000000" Name="СТД" DBName="" TheComment="">
        <ParentPackage_COL IsLocked="0" />
      </MTZAPP>
      <MTZAPP ID="2eb473eb-a886-47ad-abab-4e4b3642dcf2" Deleted="False" IsLocked="0" RetriveTime="636886968601287765" ChangeTime="599264352000000000" AccessTime="599264352000000000" SecureStyleID="00000000-0000-0000-0000-000000000000" Name="Ядро2" DBName="" TheComment="">
        <ParentPackage_COL IsLocked="0" />
      </MTZAPP>
    </MTZAPP_COL>
...
</APPLICATION>
</root>

style.xsl
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="2.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

  <html>
  <body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">
  <h1><xsl:value-of select="root/APPLICATION/@NAME"/> </h1>
    <table border="1">
      <tr>
        <td>ID</td>
        <td>Deleted</td>
        <td>IsLocked</td>
        <td>RetriveTime</td>
        <td>ChangeTime</td>
        <td>AccessTime</td>
        <td>SecureStyleID</td>
        <td>Name</td>
        <td>DBName</td>
        <td>TheComment</td>
      </tr>
      <xsl:for-each select="root/APPLICATION/MTZAPP_COL/MTZAPP">
      <tr>
        <td><xsl:value-of select="@ID"/></td>
        <td><xsl:value-of select="@Deleted"/></td>
        <td><xsl:value-of select="@IsLocked"/></td>
        <td><xsl:value-of select="@RetriveTime"/></td>
        <td><xsl:value-of select="@ChangeTime"/></td>
        <td><xsl:value-of select="@AccessTime"/></td>
        <td><xsl:value-of select="@SecureStyleID"/></td>
        <td><xsl:value-of select="@Name"/></td>
        <td><xsl:value-of select="@DBName"/></td>
        <td><xsl:value-of select="@TheComment"/></td>
      </tr>
      </xsl:for-each>
    </table>
</body>
</html>

</xsl:template>
</xsl:stylesheet>

It turned out like this:
5c98d44985846575982481.png
There are about 7500 lines in the test.xml file, the rest of the data differs from those shown above and look, for example, like this:
<OBJECTMODE_COL IsLocked="0">
          <OBJECTMODE ID="67a2da64-1444-4e9f-9c60-89923b4bcc6d" Deleted="False" IsLocked="0" RetriveTime="636886970732723806" ChangeTime="599264352000000000" AccessTime="599264352000000000" SecureStyleID="00000000-0000-0000-0000-000000000000" Name="edit" DefaultMode="0" TheComment="редактирование">
            <STRUCTRESTRICTION_COL IsLocked="0" />
            <METHODRESTRICTION_COL IsLocked="0" />
            <FIELDRESTRICTION_COL IsLocked="0" />
          </OBJECTMODE>
          <OBJECTMODE ID="96c54a86-ef5e-4168-b26d-b9858dacf7b6" Deleted="False" IsLocked="0" RetriveTime="636886970732723806" ChangeTime="599264352000000000" AccessTime="599264352000000000" SecureStyleID="00000000-0000-0000-0000-000000000000" Name="read" DefaultMode="-1" TheComment="только чтение,  основной">
            <STRUCTRESTRICTION_COL IsLocked="0">
              <STRUCTRESTRICTION ID="07195bc3-703a-4e10-9d6c-bbcd5c031834" Deleted="False" IsLocked="0" RetriveTime="636886970732983101" ChangeTime="599264352000000000" AccessTime="599264352000000000" SecureStyleID="00000000-0000-0000-0000-000000000000" Struct="4a5cf015-c18d-4149-b965-806ff5beb13a" AllowRead="-1" AllowAdd="0" AllowEdit="-1" AllowDelete="0" />
            </STRUCTRESTRICTION_COL>
            <METHODRESTRICTION_COL IsLocked="0" />
            <FIELDRESTRICTION_COL IsLocked="0">
              <FIELDRESTRICTION ID="1d371bad-9e08-405e-94f1-30d22f0a20e3" Deleted="False" IsLocked="0" RetriveTime="636886970733032974" ChangeTime="599264352000000000" AccessTime="599264352000000000" SecureStyleID="00000000-0000-0000-0000-000000000000" ThePart="4a5cf015-c18d-4149-b965-806ff5beb13a" TheField="91fb40df-4f94-4ede-b63d-040ab311b8b2" AllowRead="-1" AllowModify="0" MandatoryField="0" />
              <FIELDRESTRICTION ID="aa91eac0-fa3a-413b-9a91-47d5cc72d68f" Deleted="False" IsLocked="0" RetriveTime="636886970733032974" ChangeTime="599264352000000000" AccessTime="599264352000000000" SecureStyleID="00000000-0000-0000-0000-000000000000" ThePart="4a5cf015-c18d-4149-b965-806ff5beb13a" TheField="0dcf3edc-c76e-4e6f-842a-59093fa7c48e" AllowRead="-1" AllowModify="0" MandatoryField="0" />
              <FIELDRESTRICTION ID="b5d3b2e6-dc7d-40ec-9428-d727a56c541e" Deleted="False" IsLocked="0" RetriveTime="636886970733032974" ChangeTime="599264352000000000" AccessTime="599264352000000000" SecureStyleID="00000000-0000-0000-0000-000000000000" ThePart="4a5cf015-c18d-4149-b965-806ff5beb13a" TheField="f98c660f-53f3-42f2-af1b-0f70e0ec5a70" AllowRead="-1" AllowModify="0" MandatoryField="0" />
            </FIELDRESTRICTION_COL>
          </OBJECTMODE>

Do I need to create separate tables for all elements in style.xsl or is there some simpler and more convenient way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Fov, 2019-03-25
@Roman-Fov

If the element differs in structure, then it will have to be processed separately.
If there are common features, then it is possible together. List them in apply-templates select and template match.
XML
https://pastebin.com/DeN4rRnY
XSLt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/root/APPLICATION">
    <html>
    <body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">
    <h1><xsl:value-of select="root/APPLICATION/@NAME"/> </h1>
      <table border="1">
        <tr>
          <td>ID</td>
          <td>Deleted</td>
          <td>IsLocked</td>
          <td>RetriveTime</td>
          <td>ChangeTime</td>
          <td>AccessTime</td>
          <td>SecureStyleID</td>
          <td>Name</td>
          <td>DBName</td>
          <td>TheComment</td>
        </tr>
        <xsl:apply-templates select="MTZAPP_COL/MTZAPP | OBJECTMODE_COL/OBJECTMODE"/>
      </table>
  </body>
  </html>
  </xsl:template>

  <xsl:template match="MTZAPP_COL/MTZAPP | OBJECTMODE_COL/OBJECTMODE">
    <tr>
      <td><xsl:value-of select="@ID"/></td>
      <td><xsl:value-of select="@Deleted"/></td>
      <td><xsl:value-of select="@IsLocked"/></td>
      <td><xsl:value-of select="@RetriveTime"/></td>
      <td><xsl:value-of select="@ChangeTime"/></td>
      <td><xsl:value-of select="@AccessTime"/></td>
      <td><xsl:value-of select="@SecureStyleID"/></td>
      <td><xsl:value-of select="@Name"/></td>
      <td><xsl:value-of select="@DBName"/></td>
      <td><xsl:value-of select="@TheComment"/></td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

Result
https://pastebin.com/PizDCL0M
PS Loops in XSLt are not good. What I mean to say is to avoid loops in favor of apply templates wherever possible. They complicate tables, make it impossible to reuse and generally contradict the ideology of the tool.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question