[英]Checking multiple values of nodes in xml from xslt and displaying in html
[英]XSLT template to match multiple nodes is repeating the html
使用xsl在html表中顯示xml時遇到問題。 1. html視圖重復下面的xsl和2.無法區分具有相同名稱的子元素(例如,下面xml中的name標簽)。 xml在以下不同節點中具有不同種類的信息。
<employee>
<address>
<street>street1</street>
<city>city1</city>
<pincode>123456</pincode>
</address>
<personalinfo>
<name>testname1</name>
<phone>999999999</phone>
<dob>23-09-34</dob>
</personalinfo>
<remarks>
<education>
<name>testname2</name>
<college>college1</college>
<gpa>7.5</gpa>
</education>
</remarks>
</employee>
這是我的xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="html"/>
<xsl:template match="employee">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="address|personalinfo|remarks">
<table width="630">
<tr>
<td>Name</td>
<td>College</td>
<td>City</td>
</tr>
<tr>
<td><xsl:value-of select="//name"/></td>
<td><xsl:value-of select="//college"/></td>
<td><xsl:value-of select="//city"/></td>
</tr>
</table>
<span><br/>
</span>
</xsl:template>
</xsl:stylesheet>
請在這方面幫助我。 謝謝。
從表結構來看,您想將表頭移到與根節點匹配的模板上,然后使用相對 (顯式)路徑獲取每個員工的相應詳細信息:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<html>
<body>
<table>
<tr>
<th>Name</th>
<th>College</th>
<th>City</th>
</tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="employee">
<tr>
<td>
<xsl:value-of select="personalinfo/name"/>
</td>
<td>
<xsl:value-of select="remarks/education/college"/>
</td>
<td>
<xsl:value-of select="address/city"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
表達式//name
選擇文檔中任何位置的所有名稱元素。 這不是你想要的:你希望中的name元素personalInfo
或remarks
,您正在處理:這是select="name"
為直接子,或select=".//name"
在任何深度的后裔。
使用相同的模板規則來處理結構非常不同的三個元素( address|personalinfo|remarks
)似乎很奇怪。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.