Html XSLT将XML文档转换为仅在有值时从右向左显示两列
我试图创建XSLT输出,将XML样式设置为以下标准Html XSLT将XML文档转换为仅在有值时从右向左显示两列,html,xslt,xslt-2.0,Html,Xslt,Xslt 2.0,我试图创建XSLT输出,将XML样式设置为以下标准 仅当XML中存在值时才显示字段和表行 仅显示两列表格 仅当存在值时才从左到右显示数据 请参阅示例XML、XSLT、当前输出和所需输出。 这篇文章可能是我想要的,但它有3列,不确定如何将此示例应用到下面描述的示例中 <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> &
<?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>
史密斯
约翰
大卫
年少者。
101橡树
皮奥里亚
阿兹
85345
约翰逊
做记号
老。
105蓝色
新奥尔巴尼
在里面
70005
散步的人
吉米
直率的
加利福尼亚州
90052
当前表输出
<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>
名字:约翰
姓:史密斯
中间名:中间名:大卫
世代:世代:小。
地址:101橡树
城市:皮奥里亚
州:亚利桑那州
邮政编码:85345
名字:马克
姓:约翰逊
中名:
世代:Sr。
地址:105蓝色
城市:新奥尔巴尼
州:在
邮政编码:70005
名字:吉米
姓:沃克
中间名:弗兰克
生成:
地址:
城市:
州:加利福尼亚州
邮政编码:90052
所需表格输出
<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>
名字:约翰
姓:史密斯
中间名:大卫
世代:小。
地址:101橡树
城市:皮奥里亚
州:亚利桑那州
邮政编码:85345
名字:马克
姓:约翰逊
世代:Sr。
地址:105蓝色
城市:新奥尔巴尼
州:在
邮政编码:70005
名字:吉米
姓:沃克
中间名:弗兰克
州:加利福尼亚州
邮政编码:90052
以下是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%"> </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>
史密斯
约翰
大卫
年少者。
101橡树
皮奥里亚
阿兹
85345
约翰逊
做记号
老。
105蓝色
新奥尔巴尼
在里面
70005
散步的人
吉米
直率的
加利福尼亚州
90052
结果如下:
<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>
姓氏:史密斯
名字:约翰
姓名:大卫
世代:小。
地址:101橡树
城市:皮奥里亚
州:亚利桑那州
邮政编码:85345
姓氏:约翰逊
名字:马克
世代:Sr。
地址:105蓝色
城市:新奥尔巴尼
州:在
邮政编码:70005
姓氏:沃克
名字:吉米
姓名:弗兰克
州:加利福尼亚州
邮政编码:90052
USE604130,考虑将XML输入和相应的输出用XSLT创建的最小但完整的样本。并解释XML示例中“如果存在值”的含义;这是否意味着元素不是“数据”中的空元素,还是意味着元素需要存在,但可以是空的?@user604130:我不清楚。。。