通过 xsl 动态更改 HTML
Change HTML dynamically thru xsl
我有以下 html:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
}
</style>
</head>
<body>
<table style="width:100%">
<tr>
<th>Name</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
</tr>
<tr>
<td> [lastName],[firstName] </td>
<td>[City]</td>
<td>[State]</td>
<td>[Zip]</td>
</tr>
</table>
</body>
</html>
我将从 xml
中获取值
<person>
<lastName>Zones</lastName>
<firstName>Adam</firstName>
<City>Columbus</City>
<State>OH</State>
<Zip>44250</Zip>
</person>
我想将 table 数据 <td>
元素中的值动态替换为:
<td>Zones, Adam</td>
<td>columbus</td>
<td>OH</td>
<td>44250</td>
如何实现,需要用用户条目更改名称、城市、州、zip。
这在 XSLT 1.0 中相当乏味,但肯定可行:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="lookup-doc-path" select="'person.xml'"/>
<xsl:key name="elem-by-name" match="*" use="name()" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="td">
<xsl:copy>
<xsl:call-template name="merge">
<xsl:with-param name="string" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="merge">
<xsl:param name="string"/>
<xsl:choose>
<xsl:when test="contains($string, '[') and contains(substring-after($string, '['), ']')">
<xsl:value-of select="substring-before($string, '[')" />
<!-- lookup -->
<xsl:variable name="placeholder" select="substring-before(substring-after($string, '['), ']')" />
<xsl:for-each select="document($lookup-doc-path)">
<xsl:value-of select="key('elem-by-name', $placeholder)" />
</xsl:for-each>
<!-- recursive call -->
<xsl:call-template name="merge">
<xsl:with-param name="string" select="substring-after(substring-after($string, '['), ']')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
这是假设您要指示 XSLT 处理器处理 HTML 文档(它也必须是格式正确的 XML 文档!)并提供 XML 包含实际值作为参数的文档。
这是否是最好的工作流程是另一个问题。
我有以下 html:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
}
</style>
</head>
<body>
<table style="width:100%">
<tr>
<th>Name</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
</tr>
<tr>
<td> [lastName],[firstName] </td>
<td>[City]</td>
<td>[State]</td>
<td>[Zip]</td>
</tr>
</table>
</body>
</html>
我将从 xml
中获取值<person>
<lastName>Zones</lastName>
<firstName>Adam</firstName>
<City>Columbus</City>
<State>OH</State>
<Zip>44250</Zip>
</person>
我想将 table 数据 <td>
元素中的值动态替换为:
<td>Zones, Adam</td>
<td>columbus</td>
<td>OH</td>
<td>44250</td>
如何实现,需要用用户条目更改名称、城市、州、zip。
这在 XSLT 1.0 中相当乏味,但肯定可行:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="lookup-doc-path" select="'person.xml'"/>
<xsl:key name="elem-by-name" match="*" use="name()" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="td">
<xsl:copy>
<xsl:call-template name="merge">
<xsl:with-param name="string" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="merge">
<xsl:param name="string"/>
<xsl:choose>
<xsl:when test="contains($string, '[') and contains(substring-after($string, '['), ']')">
<xsl:value-of select="substring-before($string, '[')" />
<!-- lookup -->
<xsl:variable name="placeholder" select="substring-before(substring-after($string, '['), ']')" />
<xsl:for-each select="document($lookup-doc-path)">
<xsl:value-of select="key('elem-by-name', $placeholder)" />
</xsl:for-each>
<!-- recursive call -->
<xsl:call-template name="merge">
<xsl:with-param name="string" select="substring-after(substring-after($string, '['), ']')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
这是假设您要指示 XSLT 处理器处理 HTML 文档(它也必须是格式正确的 XML 文档!)并提供 XML 包含实际值作为参数的文档。
这是否是最好的工作流程是另一个问题。