0

snippet from the xml file:

  <record>
    <entry>2020-06-14</entry>
    <entry>Fraser</entry>
    <entry>F</entry>
    <entry>40-49</entry>
    <entry>Lab-diagnosed</entry>
  </record>
  <record>
    <entry>2020-06-14</entry>
    <entry>Fraser</entry>
    <entry>F</entry>
    <entry>20-29</entry>
    <entry>Lab-diagnosed</entry>
  </record>
  <record>
    <entry>2020-06-14</entry>
    <entry>Vancouver Coastal</entry>
    <entry>M</entry>
    <entry>30-39</entry>
    <entry>Lab-diagnosed</entry>
  </record>

An xsd file which validates with xmllint the full xml file (I quoted only a snippet above):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="csv">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="record"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="record">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="entry"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="entry" type="xs:string"/>
</xs:schema>

Output:

 <table style="width:100%">
  <tr>
    <th>Reported Date</th>
    <th>HA</th>
    <th>Sex</th>
    <th>Age_Group</th>
    <th>Classification_Reported</th>
  </tr>
  <tr>
    <td>2020-06-14</td>    
    <td>Fraser</td>
    <td>F</td>
    <td>40-49</td>
    <td>Lab Diagnosed</td>
  </tr>
  <tr>
    <td>2020-06-14</td>    
    <td>Fraser</td>
    <td>F</td>
    <td>20-29</td>
    <td>Lab Diagnosed</td>
  </tr>
  <tr>
    <td>2020-06-14</td>    
    <td>Vancouver Coastal</td>
    <td>M</td>
    <td>30-39</td>
    <td>Lab Diagnosed</td>
  </tr>
</table> 

What xslt would generate html results as above?

2 Answers 2

1

Use below code

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

    <xsl:output indent="yes"/>

    <xsl:template match="csv">
        <table style="width:100%">
            <tr>
                <th>Reported Date</th>
                <th>HA</th>
                <th>Sex</th>
                <th>Age_Group</th>
                <th>Classification_Reported</th>
            </tr>
            <xsl:apply-templates/>
        </table>
    </xsl:template>

    <xsl:template match="record">
        <tr>
            <xsl:apply-templates/>
        </tr>
    </xsl:template>

    <xsl:template match="entry">
        <td>
            <xsl:apply-templates/>
        </td>
    </xsl:template>
</xsl:stylesheet>

See Transformation at https://xsltfiddle.liberty-development.net/naZYrpA

Sign up to request clarification or add additional context in comments.

Comments

0

only sort of works:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:template match="/">
    <html>
      <body>
        <h2>Catalog</h2>
        <table border="1">
          <xsl:for-each select="/csv/record">
            <tr>
              <td>
                <xsl:value-of select="entry"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

of course, there are five "entry" elements, so...not sure how to handle that...

1 Comment

in case of element to element mapping you don't need any for-each iteration

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.