Я пытаюсь создать вывод XSLT, который будет стилизовать XML по следующим критериям.
- Отображать поле и строку таблицы, только если значение присутствует в XML
- Отображение только двух столбцов таблицы
- Отображение данных слева направо только при наличии значения
См. Пример XML, XSLT, текущий вывод и желаемый вывод.
Статья XSLT создание таблицы с различным количеством столбцов может быть тем, что я ищу, но это для 3 столбцов, и я не уверен, как применить этот образец к описанному ниже примеру.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<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()"/> of <xsl:value-of select="count(//Person)"/> 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>