How to use XSLT to convert a XML to Table [CODE UPDATED 11/6]

前端 未结 2 1482
隐瞒了意图╮
隐瞒了意图╮ 2021-01-28 10:41

I have a XML file want to convert to a Table OR CSV by XSLT. I tried by Altova MapForce 2014to map the columns but still,not able to gen out the outcomes. thanks a lot.

相关标签:
2条回答
  • 2021-01-28 11:04

    You need two nested loops here:

    ...
    <xsl:for-each select="Records/Person/NameDetails/Name">
        <xsl:variable name="NameType" select="@NameType"/>
        <xsl:variable name="FirstName" select="NameValue/FirstName"/>
        <xsl:variable name="Surname" select="NameValue/Surname"/>
            <xsl:for-each select="../../Descriptions/Description">
                <tr>
                    <td><xsl:value-of select="../../@id"/></td>
                    <td><xsl:value-of select="../../@date"/></td>
                    <td><xsl:value-of select="../../Gender"/></td>
                    <td><xsl:value-of select="$NameType"/></td>
                    <td><xsl:value-of select="$FirstName"/></td>
                    <td><xsl:value-of select="$Surname"/></td>
                    <td><xsl:value-of select="@Description1"/></td>
                    <td><xsl:value-of select="@Description2"/></td>
                    <td><xsl:value-of select="@Description3"/></td>
                </tr>
            </xsl:for-each>
    </xsl:for-each>
    ...
    

    Edit:

    To avoid storing all name details in individual variables, you can do:

    <xsl:for-each select="Records/Person/NameDetails/Name">
        <xsl:variable name="name" select="."/>
        <xsl:for-each select="../../Descriptions/Description">
            <tr>
                <td><xsl:value-of select="../../@id"/></td>
                <td><xsl:value-of select="../../@date"/></td>
                <td><xsl:value-of select="../../Gender"/></td>
                <td><xsl:value-of select="$name/@NameType"/></td>
                <td><xsl:value-of select="$name/NameValue/FirstName"/></td>
                <td><xsl:value-of select="$name/NameValue/Surname"/></td>
                <td><xsl:value-of select="@Description1"/></td>
                <td><xsl:value-of select="@Description2"/></td>
                <td><xsl:value-of select="@Description3"/></td>
            </tr>
        </xsl:for-each>
    </xsl:for-each>
    

    Edit 2:

    Here's an example of 3 nested loops, adding the dates you have added in your latest edit.

    <xsl:for-each select="Records/Person/NameDetails/Name">
        <xsl:variable name="name" select="."/>
        <xsl:for-each select="../../DateDetails/Date/DateValue">
            <xsl:variable name="dateval" select="."/>
            <xsl:for-each select="../../../Descriptions/Description">
                <tr>
                    <td><xsl:value-of select="../../@id"/></td>
                    <td><xsl:value-of select="../../@date"/></td>
                    <td><xsl:value-of select="../../Gender"/></td>
                    <td><xsl:value-of select="$name/@NameType"/></td>
                    <td><xsl:value-of select="$name/NameValue/FirstName"/></td>
                    <td><xsl:value-of select="$name/NameValue/Surname"/></td>
                    <td><xsl:value-of select="@Description1"/></td>
                    <td><xsl:value-of select="@Description2"/></td>
                    <td><xsl:value-of select="@Description3"/></td>
                    <td><xsl:value-of select="$dateval/../@DateType"/></td>
                    <td><xsl:value-of select="$dateval/@Year"/></td>
                </tr>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:for-each>
    
    0 讨论(0)
  • 2021-01-28 11:10

    Here's how you can accomplish this using templates. To increase the number of levels of permutations, you'll just need to increase the number of templates, and pass more and more parameters from one to the next:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="html" indent="yes"/>
    
      <xsl:template match="/*">
        <html>
          <head>
            <title>Records</title>
          </head>
          <body>
            <table border="1">
              <tr>
                <th>ID</th>
                <th>date</th>
                <th>Gender</th>
                <th>NameType</th>
                <th>FirstName</th>
                <th>SurName</th>
                <th>Description1</th>
                <th>Description2</th>
                <th>Description3</th>
                <th>DateType</th>
                <th>Year</th>
              </tr>
              <xsl:apply-templates select="Person" />
            </table>
          </body>
        </html>
      </xsl:template>
    
      <xsl:template match="Person">
        <xsl:apply-templates select=".//Date/DateValue" />
      </xsl:template>
    
      <xsl:template match="DateValue">
        <xsl:apply-templates select="ancestor::Person/NameDetails/Name">
          <xsl:with-param name="date" select="." />
        </xsl:apply-templates>
      </xsl:template>
    
      <xsl:template match="Name">
        <xsl:param name="date" select="/.." />
    
        <xsl:apply-templates select="ancestor::Person/Descriptions/Description">
          <xsl:with-param name="date" select="$date" />
          <xsl:with-param name="name" select="." />
        </xsl:apply-templates>
      </xsl:template>
    
      <xsl:template match="Description">
        <xsl:param name="date" select="/.." />
        <xsl:param name="name" select="/.." />
    
        <tr>
          <td>
            <xsl:value-of select="../../@id"/>
          </td>
          <td>
            <xsl:value-of select="../../@date"/>
          </td>
          <td>
            <xsl:value-of select="../../Gender"/>
          </td>
          <td>
            <xsl:value-of select="$name/@NameType"/>
          </td>
          <td>
            <xsl:value-of select="$name/NameValue/FirstName"/>
          </td>
          <td>
            <xsl:value-of select="$name/NameValue/Surname"/>
          </td>
          <td>
            <xsl:value-of select="@Description1"/>
          </td>
          <td>
            <xsl:value-of select="@Description2"/>
          </td>
          <td>
            <xsl:value-of select="@Description3"/>
          </td>
          <td>
            <xsl:value-of select="$date/../@DateType"/>
          </td>
          <td>
            <xsl:value-of select="$date/@Year"/>
          </td>
        </tr>
      </xsl:template>
    </xsl:stylesheet>
    
    0 讨论(0)
提交回复
热议问题