xsltxslt-1.0apply-templates

Improved XSL script with a linked in XML file and applying templates


Here is some XSL script:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msa="http://www.publictalksoftware.co.uk/msa">
  <xsl:output method="html" indent="yes" version="4.01"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    doctype-public="//W3C//DTD XHTML 1.0 Transitional//EN"/>

  <xsl:variable name="DutyHistory" select="document('DutyAssignHistory.XML')"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>Test</title>
      </head>
      <body>
        <xsl:for-each select="MeetingWorkBook/Meeting">
          <p>
            <xsl:value-of select ="Date/@ThisWeek"/>
          </p>
          <xsl:variable name="Week" select="Date/@ThisWeek"/>
          <table>
            <tr>
              <td>Sound</td>
              <td>
                <xsl:value-of select="$DutyHistory/msa:DutyAssignmentHistory/msa:DutyAssignments/msa:DutyAssignmentEntry[@Week=$Week and @Mode='Weekend']/msa:Assignment[@Index='1' and @IndexType='Fixed']"/>
              </td>
            </tr>
            <tr>
              <td>Platform</td>
              <td>
                <xsl:value-of select="$DutyHistory/msa:DutyAssignmentHistory/msa:DutyAssignments/msa:DutyAssignmentEntry[@Week=$Week and @Mode='Weekend']/msa:Assignment[@Index='5' and @IndexType='Fixed']"/>
              </td>
            </tr>
            <tr>
              <td>Left Mike</td>
              <td>
                <xsl:value-of select="$DutyHistory/msa:DutyAssignmentHistory/msa:DutyAssignments/msa:DutyAssignmentEntry[@Week=$Week and @Mode='Weekend']/msa:Assignment[@Index='7' and @IndexType='Fixed']"/>
              </td>
            </tr>
            <tr>
              <td>Right Mike</td>
              <td>
                <xsl:value-of select="$DutyHistory/msa:DutyAssignmentHistory/msa:DutyAssignments/msa:DutyAssignmentEntry[@Week=$Week and @Mode='Weekend']/msa:Assignment[@Index='8' and @IndexType='Fixed']"/>
              </td>
            </tr>
            <tr>
              <td>Public Talk Chairman</td>
              <td>
                <xsl:value-of select="$DutyHistory/msa:DutyAssignmentHistory/msa:DutyAssignments/msa:DutyAssignmentEntry[@Week=$Week and @Mode='Weekend']/msa:Assignment[@Index='4' and @IndexType='Custom']"/>
              </td>
            </tr>
            <tr>
              <td>Watchtower Reader</td>
              <td>
                <xsl:value-of select="$DutyHistory/msa:DutyAssignmentHistory/msa:DutyAssignments/msa:DutyAssignmentEntry[@Week=$Week and @Mode='Weekend']/msa:Assignment[@Index='5' and @IndexType='Custom']"/>
              </td>
            </tr>
          </table>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

As you can see it is linking in another XML document for reference. Here is one example of that linked in file:

<?xml version="1.0" encoding="utf-8"?>
<DutyAssignmentHistory xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.publictalksoftware.co.uk/msa">
  <DutyAssignments>
    <DutyAssignmentEntry Date="2018-01-04" Week="W20180101" Template="0" Mode="Midweek">
      <Assignment Index="2" IndexType="Fixed">Name 1</Assignment>
      <Assignment Index="5" IndexType="Fixed">Name 2</Assignment>
      <Assignment Index="7" IndexType="Fixed">Name 3</Assignment>
      <Assignment Index="8" IndexType="Fixed">Name 4</Assignment>
      <Assignment Index="13" IndexType="Fixed">Name 5</Assignment>
      <Assignment Index="14" IndexType="Fixed">Name 6</Assignment>
    </DutyAssignmentEntry>
  </DutyAssignments>
</DutyAssignmentHistory>

Potentially a user might want to reference the information in the XML and display it however they like but I am wanting to show them the simplest method.

As you can see there are several criteria:

The above will filter to the right week of assignments. Then, to identify the actual assignment:

Can I use templates in any way (perhaps with variables) to simplify the code as it is getting repetative?


Solution

  • You could use a template and pass parameters, or in XSLT 2.0 or higher you could also define a function, which makes it much easier to use and saves some typing. But for what you are currently doing, a variable and some predicate filters seems to be the most simple and easy.

    The most simple and easy way would be to bind a variable with the weekend assignments, and then apply your predicate filter to select the one with the @Index and @IndexType:

    <xsl:variable name="Week" select="Date/@ThisWeek"/>
    <xsl:variable name="weekend-assignments"
                 select="$DutyHistory/msa:DutyAssignmentHistory/msa:DutyAssignments
                    /msa:DutyAssignmentEntry[@Week=$Week and @Mode='Weekend']/msa:Assignment"/>
    <table>
      <tr>
        <td>Sound</td>
        <td>
          <xsl:value-of select="$weekend-assignments[@Index='1' and @IndexType='Fixed']"/>
          </td>
      </tr>
    

    If you make the variable hold an unfiltered set of Assignment elements, you could perform all of the filtering in the predicates:

    <xsl:variable name="Week" select="Date/@ThisWeek"/>
    <xsl:variable name="assignments"
                 select="$DutyHistory/msa:DutyAssignmentHistory/msa:DutyAssignments
                    /msa:DutyAssignmentEntry/msa:Assignment"/>
    <table>
      <tr>
        <td>Sound</td>
        <td>
          <xsl:value-of 
               select="$assignments[@Index='1' and @IndexType='Fixed']
                                   [..[@Week=$Week and @Mode='Weekend' and @Template='0']]"/>
          </td>
      </tr>
    

    If you want to consolidate the logic for generating the columns, you could define a template for msa:Assignment:

    <xsl:template match="msa:Assignment">
      <td>
        <xsl:value-of select="."/>
      </td>
    </xsl:template>
    

    And then use it like this:

    <table>
      <tr>
        <td>Sound</td>
        <xsl:apply-templates select="$weekend-assignments[@Index='1' and @IndexType='Fixed']"/>
    

    If you want to consolidate the logic for generating rows, you could define a template for msa:Assignment and send in a parameter for the first column:

    <xsl:template match="msa:Assignment">
        <xsl:param name="label"/>
        <tr>
            <td><xsl:value-of select="$label"/></td>
            <td>
                <xsl:value-of select="."/>
            </td>
        </tr>
    </xsl:template>
    

    And then use it like this:

    <table>
      <xsl:apply-templates select="$weekend-assignments[@Index='1' and @IndexType='Fixed']">
        <xsl:with-param name="label" select="'Sound'"/>
      </xsl:apply-templates>