Transforming XML to group fields into columns with XSLT

北战南征 提交于 2019-12-12 04:03:44

问题


I currently have an XML file which looks like the following:

<Plan>
  <People>
    <Person>
      <name>Fred Bloggs</name>
      <position>CEO</position>
      <responsibility>Everything</responsibility>
    </Person>
    <Person>
      <name>Joe Bloggs</name>
      <position>Cleaner</position>
      <responsibility>Cleaning</responsibility>
    </Person>
    <Person>
      <name>Wilma Bloggs</name>
      <position>CTO</position>
      <responsibility>Tech stuff</responsibility>
    </Person>
    <Person>
      <name>Betty Bloggs</name>
      <position>MD</position>
      <responsibility>Management</responsibility>
    </Person>
  </People>
</Plan>

Effectively it is a list of People, each of whom have a list of fields. The list of fields will expand in future, but each Person will have the same fields.

I want to transform and format this, using XSLT and XSL-FO, to produce a PDF which has four columns, with each row listing the field name and then the field values for three Person objects. For example, as there are four people listed above I want the table to look like this:

Name            Fred Bloggs   Joe Bloggs  Wilma Bloggs
Position        CEO           Cleaner     CTO
Responsibility  Everything    Cleaning    Tech stuff
Name            Betty Bloggs
Position        MD
Responsibility  Management

Effectively, in the XML file I group the fields by person (so each person has three fields), whereas in the final output I want to group the people by fields (so each row has the same field for three different people).

If I write the XSL-FO code manually, I can achieve this by producing markup along these lines for each row:

<fo:table-row>
  <fo:table-cell>
    <fo:block>Name</fo:block>
  </fo:table-cell>
  <fo:table-cell>
    <fo:block>Fred Bloggs</fo:block>
  </fo:table-cell>
  <fo:table-cell>
    <fo:block>Joe Bloggs</fo:block>
  </fo:table-cell>
  <fo:table-cell>
    <fo:block>Wilma Bloggs</fo:block>
  </fo:table-cell>
</fo:table-row>

This gives me the result I want, so I know the XSL-FO to PDF part is working fine, but I need to automate the process. Can I achieve this directly via XSLT?

The other option I can think of is to produce a separate XML file which explicitly matches the layout structure and then transform that, e.g.:

<PersonTable>
  <PersonRow>
    <name1>Fred Bloggs</name1>
    <name2>Joe Bloggs</name2>
    <name3>Wilma Bloggs</name3>
  </PersonRow>
</PersonTable>

The final, least preferred option, would be to produce the complete XSL-FO data automatically, completely side-stepping the transformation step (i.e. I effectively go from XSL-FO to PDF). I don't want to do this as in the long run I would like to be able to ship different XSL template files without modifying the software, but I can fall back to this option if the previous ones aren't possible.


回答1:


To simplify the matter (for me), the following stylesheet creates a simple HTML table in the requested format:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="cols" select="3" />

<xsl:template match="People">
    <table border="1">
        <xsl:apply-templates select="Person[position() mod $cols = 1]"/>
    </table>
</xsl:template>

<xsl:template match="Person">
    <xsl:variable name="group" select=". | following-sibling::Person[position() &lt; $cols]" />
    <xsl:for-each select="*">
        <xsl:variable name="i" select="position()" />
        <tr>
            <td>
                <xsl:value-of select="name()"/>
            </td>
            <xsl:for-each select="$group">
                <td>
                    <xsl:value-of select="*[$i]"/>
                </td>
            </xsl:for-each>
        </tr>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Applied to your example, the result is:

<?xml version="1.0" encoding="UTF-8"?>
<table border="1">
   <tr>
      <td>name</td>
      <td>Fred Bloggs</td>
      <td>Joe Bloggs</td>
      <td>Wilma Bloggs</td>
   </tr>
   <tr>
      <td>position</td>
      <td>CEO</td>
      <td>Cleaner</td>
      <td>CTO</td>
   </tr>
   <tr>
      <td>responsibility</td>
      <td>Everything</td>
      <td>Cleaning</td>
      <td>Tech stuff</td>
   </tr>
   <tr>
      <td>name</td>
      <td>Betty Bloggs</td>
   </tr>
   <tr>
      <td>position</td>
      <td>MD</td>
   </tr>
   <tr>
      <td>responsibility</td>
      <td>Management</td>
   </tr>
</table>

Rendered as:




回答2:


Adapting my solution from two adjacent tables in body-region each with two columns(xsl-fo) and still using XSLT 1.0:

  <xsl:param name="cols" select="3" />

  <xsl:template match="People">
    <fo:table>
      <fo:table-body>
        <xsl:call-template name="rows" />
      </fo:table-body>
    </fo:table>
  </xsl:template>

  <xsl:template name="rows">
    <xsl:param name="persons" select="*" />

    <xsl:call-template name="row-group">
      <xsl:with-param name="persons"
                      select="$persons[position() &lt;= $cols]" />
    </xsl:call-template>
    <xsl:if test="count($persons) > $cols">
      <xsl:call-template name="rows">
        <xsl:with-param name="persons" select="$persons[position() > $cols]" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template name="row-group">
    <xsl:param name="persons" />

    <xsl:for-each select="$persons[1]/*">
      <xsl:variable name="position" select="position()" />
      <fo:table-row>
        <fo:table-cell>
          <fo:block><xsl:value-of select="local-name()" /></fo:block>
        </fo:table-cell>
        <xsl:for-each select="$persons">
          <fo:table-cell>
            <fo:block><xsl:apply-templates select="./*[position()= $position]" /></fo:block>
          </fo:table-cell>
        </xsl:for-each>
      </fo:table-row>
    </xsl:for-each>
  </xsl:template>


来源:https://stackoverflow.com/questions/34898601/transforming-xml-to-group-fields-into-columns-with-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!