通过xsl动态更改HTML [英] Change HTML dynamically thru xsl

查看:74
本文介绍了通过xsl动态更改HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下html:

I have the following 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中获取值

I will get the values from an xml

<person>
    <lastName>Zones</lastName>
    <firstName>Adam</firstName>
    <City>Columbus</City>
    <State>OH</State>
    <Zip>44250</Zip>
</person>

我想要替换表数据中的值< td> 元素动态地为:

I want to replace the values in the table data <td> elements dynamically as:

<td>Zones, Adam</td>
<td>columbus</td>
<td>OH</td>
<td>44250</td>

如何实现这一点,需要更改用户条目的名称,城市,州,邮编。 / p>

How to achieve this, Need to change the name,city,state,zip with user entries.

推荐答案

在XSLT 1.0中这样做很繁琐,但当然可行:

This is rather tedious to do in XSLT 1.0 but certainly possible:

<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文档的路径作为参数提供。

This is assuming you are directing your XSLT processor to process the HTML document (which must also be a well-formed XML document!) and supplying the path to the XML document containing the actual values as a parameter.

这是否是最佳的工作流程,是另一个问题。

Whether this is the best workflow to have in place is another question.

这篇关于通过xsl动态更改HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆