XSLT-преобразование XML-документа для отображения двух столбцов справа налево, только если они имеют значение - PullRequest
0 голосов
/ 05 февраля 2011

Я пытаюсь создать вывод XSLT, который будет стилизовать XML по следующим критериям.

  1. Отображать поле и строку таблицы, только если значение присутствует в XML
  2. Отображение только двух столбцов таблицы
  3. Отображение данных слева направо только при наличии значения

См. Пример XML, XSLT, текущий вывод и желаемый вывод. Статья XSLT создание таблицы с различным количеством столбцов может быть тем, что я ищу, но это для 3 столбцов, и я не уверен, как применить этот образец к описанному ниже примеру.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet  [
  <!ENTITY nbsp   "&#160;">
  <!ENTITY copy   "&#169;">
  <!ENTITY reg    "&#174;">
  <!ENTITY trade  "&#8482;">
  <!ENTITY mdash  "&#8212;">
  <!ENTITY ldquo  "&#8220;">
  <!ENTITY rdquo  "&#8221;">
  <!ENTITY pound  "&#163;">
  <!ENTITY yen    "&#165;">
  <!ENTITY euro   "&#8364;">
]>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <xsl:output method="html" encoding="utf-16"/>
  <xsl:template match="/">


    <xsl:for-each select="Employees/Person">

      <table border="1" style="font-size:12pt; width:50%;">
        <td>
          <p style="text-align:center; font-size:14pt; font-weight:bold; background-color:#404060; color:white;">
            (<xsl:value-of select="position()"/>&nbsp;of&nbsp;<xsl:value-of select="count(//Person)"/>&nbsp;records found)
          </p>
        </td>
      </table>
      <table border="1" style="font-size:12pt; width:50%;">
        <tr>
          <td width="50%">
            <span style="font-weight:bold;">First Name:</span>
            <xsl:value-of select="FirstName"/>
          </td>
          <td width="50%">
            <span style="font-weight:bold;">Last Name:</span>
            <xsl:value-of select="LastName"/>
          </td>
        </tr>
        <tr>
          <td width="50%">
            <span style="font-weight:bold;">Middle Name:</span>
            <xsl:value-of select="MiddleName"/>
          </td>
          <td width="50%">
            <span style="font-weight:bold;">Generation:</span>
            <xsl:value-of select="Generation"/>
          </td>
        </tr>
        <tr>
          <td width="50%">
            <span style="font-weight:bold;">Address:</span>
            <xsl:value-of select="Address"/>
          </td>
          <td width="50%">
            <span style="font-weight:bold;">City:</span>
            <xsl:value-of select="City"/>
          </td>
        </tr>
        <tr>
          <td width="50%">
            <span style="font-weight:bold;">State:</span>
            <xsl:value-of select="State"/>
          </td>
          <td width="50%">
            <span style="font-weight:bold;">Zip:</span>
            <xsl:value-of select="Zip"/>
          </td>
        </tr>
      </table>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Обновление

Текущий XML - Employees.xml

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="Employees.xslt"?>
<Employees>
  <Person>
    <LastName>Smith</LastName>
    <FirstName>John</FirstName>
    <MiddleName>David</MiddleName>
    <Generation>Jr.</Generation>
    <Address>101 Oak</Address>
    <City>Peoria </City>
    <State>AZ </State>
    <Zip>85345 </Zip>
  </Person>
  <Person>
    <LastName>Johnson</LastName>
    <FirstName>Mark</FirstName>
    <MiddleName/>
    <Generation>Sr.</Generation>
    <Address>105 Blue</Address>
    <City>New Albany </City>
    <State>IN </State>
    <Zip>70005 </Zip>
  </Person>
  <Person>
    <LastName>Walker</LastName>
    <FirstName>Jimmy</FirstName>
    <MiddleName>Frank </MiddleName>
    <Generation/>
    <Address/>
    <City/>
    <State>CA </State>
    <Zip>90052 </Zip>
  </Person>
</Employees>

Текущий вывод таблицы

<table border="1" style="font-size: 12pt; width: 50%;">
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">First Name: </span>John
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Last Name: </span>Smith
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Middle Name: </span>Middle Name:David
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Generation: </span>Generation:Jr.
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Address: </span>Address:101 Oak
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">City: </span>Peoria
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">State: </span>AZ
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Zip: </span>85345
        </td>
    </tr>
</table>
<br />
<table border="1" style="font-size: 12pt; width: 50%;">
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">First Name: </span>Mark
        </td>
        <td>
            <span style="font-weight: bold;">Last Name: </span>Johnson
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Middle Name: </span>
        </td>
        <td>
            <span style="font-weight: bold;">Generation: </span>Sr.
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Address: </span>105 Blue
        </td>
        <td>
            <span style="font-weight: bold;">City: </span>New Albany
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">State: </span>IN
        </td>
        <td>
            <span style="font-weight: bold;">Zip: </span>70005
        </td>
    </tr>
</table>
<br />
<table border="1" style="font-size: 12pt; width: 50%;">
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">First Name: </span>Jimmy
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Last Name: </span>Walker
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Middle Name: </span>Frank
        </td>
        <td>
            <span style="font-weight: bold;">Generation: </span>
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Address: </span>
        </td>
        <td>
            <span style="font-weight: bold;">City: </span>
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">State: </span>CA
        </td>
        <td>
            <span style="font-weight: bold;">Zip: </span>90052
        </td>
    </tr>
</table>

Требуемый вывод таблицы

<table border="1" style="font-size: 12pt; width: 50%;">
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">First Name: </span>John
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Last Name: </span>Smith
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Middle Name: </span>David
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Generation: </span>Jr.
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Address: </span>101 Oak
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">City: </span>Peoria
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">State: </span>AZ
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Zip: </span>85345
        </td>
    </tr>
</table>
<br />
<table border="1" style="font-size: 12pt; width: 50%;">
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">First Name: </span>Mark
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Last Name: </span>Johnson
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Generation: </span>Sr.
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Address: </span>105 Blue
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">City: </span>New Albany
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">State: </span>IN
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Zip: </span>70005
        </td>
        <td valign="top" width="50%">
        </td>
    </tr>
</table>
<br />
<table border="1" style="font-size: 12pt; width: 50%;">
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">First Name: </span>Jimmy
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Last Name: </span>Walker
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Middle Name: </span>Frank
        </td>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">State: </span>CA
        </td>
    </tr>
    <tr>
        <td valign="top" width="50%">
            <span style="font-weight: bold;">Zip: </span>90052
        </td>
        <td valign="top" width="50%">
        </td>
    </tr>
</table>

1 Ответ

1 голос
/ 09 февраля 2011

Вот таблица стилей XSLT 2.0:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="xs"
  version="2.0">

  <xsl:param name="n" as="xs:integer" select="2"/>

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

  <xsl:strip-space elements="*"/>

  <xsl:template match="Employees">
    <div>
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="Person">
    <table border="1" style="font-size: 12pt; width: 50%;">
      <xsl:apply-templates select="*[normalize-space()][position() mod $n eq 1]" mode="row"/>
    </table>
    <xsl:if test="position() ne last()"><br/></xsl:if>
  </xsl:template>

  <xsl:template match="Person/*" mode="row">
    <tr>
      <xsl:variable name="cells" as="element()*" select=". | following-sibling::*[normalize-space()][position() lt $n]"/>
      <xsl:apply-templates select="$cells"/>
      <xsl:for-each select="1 to ($n - count($cells))">
        <td valign="top" width="50%">&#160;</td>
      </xsl:for-each>
    </tr>
  </xsl:template>

  <xsl:template match="Person/*">
    <td valign="top" width="50%">
      <span style="font-weight: bold;">
        <xsl:value-of select="local-name()"/>
        <xsl:text>: </xsl:text>
      </span>
      <xsl:value-of select="."/>
    </td>
  </xsl:template>

</xsl:stylesheet>

При применении с Saxon 9.3 к входу XML

<Employees>
  <Person>
    <LastName>Smith</LastName>
    <FirstName>John</FirstName>
    <MiddleName>David</MiddleName>
    <Generation>Jr.</Generation>
    <Address>101 Oak</Address>
    <City>Peoria </City>
    <State>AZ </State>
    <Zip>85345 </Zip>
  </Person>
  <Person>
    <LastName>Johnson</LastName>
    <FirstName>Mark</FirstName>
    <MiddleName/>
    <Generation>Sr.</Generation>
    <Address>105 Blue</Address>
    <City>New Albany </City>
    <State>IN </State>
    <Zip>70005 </Zip>
  </Person>
  <Person>
    <LastName>Walker</LastName>
    <FirstName>Jimmy</FirstName>
    <MiddleName>Frank </MiddleName>
    <Generation/>
    <Address/>
    <City/>
    <State>CA </State>
    <Zip>90052 </Zip>
  </Person>
</Employees>

вывод выглядит следующим образом:

<div xmlns="http://www.w3.org/1999/xhtml">
   <table border="1" style="font-size: 12pt; width: 50%;">
      <tr>
         <td valign="top" width="50%"><span style="font-weight: bold;">LastName: </span>Smith
         </td>
         <td valign="top" width="50%"><span style="font-weight: bold;">FirstName: </span>John
         </td>
      </tr>
      <tr>

         <td valign="top" width="50%"><span style="font-weight: bold;">MiddleName: </span>David
         </td>
         <td valign="top" width="50%"><span style="font-weight: bold;">Generation: </span>Jr.
         </td>
      </tr>
      <tr>
         <td valign="top" width="50%"><span style="font-weight: bold;">Address: </span>101 Oak
         </td>
         <td valign="top" width="50%"><span style="font-weight: bold;">City: </span>Peoria 
         </td>

      </tr>
      <tr>
         <td valign="top" width="50%"><span style="font-weight: bold;">State: </span>AZ 
         </td>
         <td valign="top" width="50%"><span style="font-weight: bold;">Zip: </span>85345 
         </td>
      </tr>
   </table><br /><table border="1" style="font-size: 12pt; width: 50%;">
      <tr>

         <td valign="top" width="50%"><span style="font-weight: bold;">LastName: </span>Johnson
         </td>
         <td valign="top" width="50%"><span style="font-weight: bold;">FirstName: </span>Mark
         </td>
      </tr>
      <tr>
         <td valign="top" width="50%"><span style="font-weight: bold;">Generation: </span>Sr.
         </td>
         <td valign="top" width="50%"><span style="font-weight: bold;">Address: </span>105 Blue
         </td>

      </tr>
      <tr>
         <td valign="top" width="50%"><span style="font-weight: bold;">City: </span>New Albany 
         </td>
         <td valign="top" width="50%"><span style="font-weight: bold;">State: </span>IN 
         </td>
      </tr>
      <tr>
         <td valign="top" width="50%"><span style="font-weight: bold;">Zip: </span>70005 
         </td>

         <td valign="top" width="50%"> </td>
      </tr>
   </table><br /><table border="1" style="font-size: 12pt; width: 50%;">
      <tr>
         <td valign="top" width="50%"><span style="font-weight: bold;">LastName: </span>Walker
         </td>
         <td valign="top" width="50%"><span style="font-weight: bold;">FirstName: </span>Jimmy
         </td>

      </tr>
      <tr>
         <td valign="top" width="50%"><span style="font-weight: bold;">MiddleName: </span>Frank 
         </td>
         <td valign="top" width="50%"><span style="font-weight: bold;">State: </span>CA 
         </td>
      </tr>
      <tr>
         <td valign="top" width="50%"><span style="font-weight: bold;">Zip: </span>90052 
         </td>

         <td valign="top" width="50%"> </td>
      </tr>
   </table>
</div>
...