J_Dizzle
J_Dizzle

Reputation: 11

Changing lists in XML/XHTML with XSLT

I have multiple xml/xhtml files that I need to convert the lists in. The lists can come in two different formats. I have shown an examples of each list and it's desired outcome. The XSLT works fine for Example 1 but for Example 2 both apply-templates employeesList and employeesList2 modes are used. Any help obtaining the desired outcome would be appreciated.

Example 1:

<div class="company1">
    <div class="employees-companyLabel">Employees:</div>
    <ol class="company-employees">
        <li class="company-employee">Joe Smith</li>
        <li class="company-employee">Dave Jones</li>
        <li class="company-employee">Bill Williams</li>
        <li class="company-employee">Steve Hobbs</li>
        <li class="company-employee">Jim Brown</li>
    </ol>
</div>

Desired Outcome:

<div class="company2">
    <div class="header">Employees:</div>
    <ul class="employees">
        <li class="employee">Joe Smith</li>
        <li class="employee">Dave Jones</li>
        <li class="employee">Bill Williams</li>
        <li class="employee">Steve Hobbs</li>
        <li class="employee">Jim Brown</li>
    </ul>
</div>

Example 2:

<div class="company1">      
    <div class="employees-companyLabel">Employees:</div>
    <div class="subhead-employees">Foreman:</div>
    <ol class="company-employees">
        <li class="company-employee">Joe Smith</li>
        <li class="company-employee">Dave Jones</li>
    </ol>
    <div class="subhead-employees">Laborers:</div>
    <ol class="company-employees">
        <li class="company-employee">Bill Williams</li>
        <li class="company-employee">Steve Hobbs</li>
        <li class="company-employee">Jim Brown</li>
    </ol>
</div>

Desired Outcome:

<div class="company2">
    <div class="header">Employees:</div>
    <ul class="employees">
        <li class="positionTitle">Foreman</li>
        <li class="employee">Joe Smith</li>
        <li class="employee">Dave Jones</li>
        <li class="positionTitle">Laborers</li>
        <li class="employee">Bill Williams</li>
        <li class="employee">Steve Hobbs</li>
        <li class="employee">Jim Brown</li>
    </ol>
</div>

XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml" exclude-result-prefixes="xhtml">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="xhtml:div[@class='company1']">
        <xsl:copy>
            <xsl:attribute name="class">
                <xsl:value-of select="'company2'"/>
            </xsl:attribute>
            <xsl:apply-templates select="@*[not(name()='class')]"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="xhtml:div[@class='employees-companyLabel']">
        <xsl:copy>
            <xsl:attribute name="class">
                <xsl:value-of select="'header'"/>
            </xsl:attribute>
            <xsl:apply-templates select="@*[not(name()='class')]"/>
            <xsl:value-of select="translate(., ':', '')" />
        </xsl:copy>
        <xsl:element name="ul">
            <xsl:attribute name="class">employees</xsl:attribute>
            <xsl:apply-templates select="./following-sibling::xhtml:ol[@class='company-employees']" mode="employeesList" />
            <xsl:apply-templates select="./following-sibling::xhtml:div[@class='subhead-employees']" mode="employeesList2" />
        </xsl:element>
    </xsl:template>

    <xsl:template match="xhtml:ol[@class='company-employees']" mode="employeesList">
        <xsl:for-each select="xhtml:li[@class='company-employee']">
            <xsl:element name="li">
                <xsl:attribute name="class">employee</xsl:attribute>
                <xsl:apply-templates />
            </xsl:element>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="xhtml:div[@class='subhead-employees']" mode="employeesList2">
        <xsl:element name="li">
            <xsl:attribute name="class">
                <xsl:value-of select="'positionTitle'"/>
            </xsl:attribute>    
            <xsl:apply-templates />
        </xsl:element>
        <xsl:for-each select="./following-sibling::xhtml:ol[@class='company-employees'][1]/xhtml:li[@class='company-employee']">
            <xsl:element name="li">
                <xsl:attribute name="class">employee</xsl:attribute>
                <xsl:apply-templates />
            </xsl:element>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="xhtml:div[@class='subhead-employees']" />
    <xsl:template match="xhtml:ol[@class='company-employees']" />
    <xsl:template match="xhtml:li[@class='company-employee']" />
</xsl:stylesheet>

Upvotes: 1

Views: 74

Answers (1)

Thomas W
Thomas W

Reputation: 15371

Here is one very simple suggestion that is of the explicit "fill out a template" style:

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

  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="xhtml:div[@class='company1']">
    <div class="company2">
      <div class="header">Employees:</div>
      <ul class="employees">
        <xsl:for-each select="xhtml:div[@class='subhead-employees']|*/xhtml:li">
          <li class="{if (@class='subhead-employees') then 'positionTitle' else 'employee'}">
            <xsl:value-of select="replace(string(), ':\s*$', '')"/>
          </li>
        </xsl:for-each>
      </ul>
    </div>
  </xsl:template>
</xsl:stylesheet>

Alternatively, a suggestion that is more based on template matching:

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

  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="xhtml:div[@class='company1']">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:attribute name="class" select="'company2'"/>
      <xsl:apply-templates select="xhtml:div[@class='employees-companyLabel']"/>
      <ul class="employees">
        <xsl:apply-templates select="xhtml:div[@class='subhead-employees']|*/xhtml:li"/>
      </ul>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="xhtml:div[@class='employees-companyLabel']">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:attribute name="class" select="'header'"/>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="xhtml:div[@class='subhead-employees']">
    <li class="positionTitle">
      <xsl:value-of select="translate(., ':', '')"/>
    </li>
  </xsl:template>

  <xsl:template match="xhtml:li">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:attribute name="class" select="'employee'"/>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Related Questions