我正在使用以下xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Style.xslt"?>
<AdditionalInfo>
<Headers>
<Header Text="Sys. Ref.">SysRef</Header>
<Header Text="Doc. Ref.">DocRef</Header>
<Header Text="Value">Value</Header>
</Headers>
<Lines>
<Line>
<SysRef>1234</SysRef>
<DocRef>IN-1234</DocRef>
<Value>123.45</Value>
</Line>
<Line>
<XYZ>aaa</XYZ>
<SysRef>1234</SysRef>
<DocRef>IN-1234</DocRef>
<Value>123.45</Value>
</Line>
</Lines>
</AdditionalInfo>
我需要将以下XML转换为如下所示
Sys. Ref. Doc. Ref. Value
1234 IN-1234 123.45
1234 IN-1234 123.45
但它正在转变为
Sys. Ref. Doc. Ref. Value
Sysref DocRef Value
这就是为什么我来看看是否有某种善良的人知道我做错了什么。我知道这很可能涉及我所做的每件事,但我一遍又一遍地试过,这是我最接近的。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:template match="Headers">
<xsl:for-each select="*">
<td style="border-top:1px double #D4D4D4;border-bottom:1px double #D4D4D4;border-right:1px double #D4D4D4;">
<xsl:value-of select="@Text"/>
</td>
</xsl:for-each>
</xsl:template>
<xsl:template match="AdditionalInfo">
<html>
<head>
<title>Additional Information</title>
</head>
<body>
<div>
<div style="margin-top:20px;">
<span style="font-weight:bold;"> Details</span>
</div>
<br/>
<table style="width:700px;font:sans-seriff;">
<tbody>
<tr>
<xsl:apply-templates select="Headers"/>
</tr>
<xsl:for-each select="Lines/Line">
<tr>
<xsl:for-each select="../../Headers/Header">
<xsl:variable name="headertext" select="."/>
<td style="border-top:1px double #D4D4D4;border-bottom:1px double #D4D4D4;border-right:1px double #D4D4D4;">
<xsl:for-each select="../../Lines/Line">
<xsl:value-of select="$headertext"/>
</xsl:for-each>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:1)
我建议采用完全不同的方法。以下是它的简单例子:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="headers" select="/AdditionalInfo/Headers/Header"/>
<xsl:variable name="rows" select="/AdditionalInfo/Lines/Line"/>
<xsl:template match="/">
<table>
<thead>
<tr>
<xsl:for-each select="$headers">
<th><xsl:value-of select="@Text"/></th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:for-each select="$rows">
<xsl:variable name="rowNum" select="position()"/>
<tr>
<xsl:for-each select="$headers">
<td>
<xsl:value-of select="$rows[$rowNum]/*[name()=current()]"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
</xsl:stylesheet>
应用于以下输入时:
<?xml version="1.0" encoding="UTF-8"?>
<AdditionalInfo>
<Headers>
<Header Text="Sys. Ref.">SysRef</Header>
<Header Text="Doc. Ref.">DocRef</Header>
<Header Text="Value">Value</Header>
</Headers>
<Lines>
<Line>
<SysRef>1234</SysRef>
<DocRef>IN-1234</DocRef>
<irrelevant>bbb</irrelevant>
<Value>123.45</Value>
</Line>
<Line>
<XYZ>aaa</XYZ>
<SysRef>4567</SysRef>
<DocRef>IN-4567</DocRef>
<Value>45.67</Value>
</Line>
<Line>
<SysRef>8901</SysRef>
<ignore>ccc</ignore>
<Value>8.91</Value>
</Line>
</Lines>
</AdditionalInfo>
结果是:
<?xml version="1.0" encoding="UTF-8"?>
<table>
<thead>
<tr>
<th>Sys. Ref.</th>
<th>Doc. Ref.</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>1234</td>
<td>IN-1234</td>
<td>123.45</td>
</tr>
<tr>
<td>4567</td>
<td>IN-4567</td>
<td>45.67</td>
</tr>
<tr>
<td>8901</td>
<td/>
<td>8.91</td>
</tr>
</tbody>
</table>
呈现为:
答案 1 :(得分:0)
编写单独的模板大大简化了任务。一些评论:
thead
元素<强>样式表强>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"
exclude-result-prefixes="fn xs">
<xsl:output method="html" indent="yes"/>
<xsl:template match="Headers">
<xsl:for-each select="*">
<td style="border-top:1px double #D4D4D4;border-bottom:1px double #D4D4D4;border-right:1px double #D4D4D4;">
<xsl:value-of select="@Text"/>
</td>
</xsl:for-each>
</xsl:template>
<xsl:template match="AdditionalInfo">
<html>
<head>
<title>Additional Information</title>
</head>
<body>
<div>
<div style="margin-top:20px;">
<span style="font-weight:bold;"> Details</span>
</div>
<br/>
<table style="width:700px;font:sans-seriff;">
<tbody>
<tr>
<xsl:apply-templates select="Headers"/>
</tr>
<xsl:apply-templates select="Lines"/>
</tbody>
</table>
</div>
</body>
</html>
</xsl:template>
<xsl:template match="Lines">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Line">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="SysRef|DocRef|Value">
<td style="border-top:1px double #D4D4D4;border-bottom:1px double #D4D4D4;border-right:1px double #D4D4D4;">
<xsl:value-of select="."/>
</td>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
HTML输出
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Additional Information</title>
</head>
<body>
<div>
<div style="margin-top:20px;"><span style="font-weight:bold;"> Details</span></div><br><table style="width:700px;font:sans-seriff;">
<tbody>
<tr>
<td style="border-top:1px double #D4D4D4;border-bottom:1px double #D4D4D4;border-right:1px double #D4D4D4;">Sys. Ref.</td>
<td style="border-top:1px double #D4D4D4;border-bottom:1px double #D4D4D4;border-right:1px double #D4D4D4;">Doc. Ref.</td>
<td style="border-top:1px double #D4D4D4;border-bottom:1px double #D4D4D4;border-right:1px double #D4D4D4;">Value</td>
</tr>
<tr>
<td style="border-top:1px double #D4D4D4;border-bottom:1px double #D4D4D4;border-right:1px double #D4D4D4;">1234</td>
<td style="border-top:1px double #D4D4D4;border-bottom:1px double #D4D4D4;border-right:1px double #D4D4D4;">IN-1234</td>
<td style="border-top:1px double #D4D4D4;border-bottom:1px double #D4D4D4;border-right:1px double #D4D4D4;">123.45</td>
</tr>
<tr>
<td style="border-top:1px double #D4D4D4;border-bottom:1px double #D4D4D4;border-right:1px double #D4D4D4;">1234</td>
<td style="border-top:1px double #D4D4D4;border-bottom:1px double #D4D4D4;border-right:1px double #D4D4D4;">IN-1234</td>
<td style="border-top:1px double #D4D4D4;border-bottom:1px double #D4D4D4;border-right:1px double #D4D4D4;">123.45</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
呈现输出