Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过xsl动态更改HTML_Html_Xml_Xslt_Xslt 1.0 - Fatal编程技术网

通过xsl动态更改HTML

通过xsl动态更改HTML,html,xml,xslt,xslt-1.0,Html,Xml,Xslt,Xslt 1.0,我有以下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%"

我有以下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>

表,th,td{
边框:1px纯黑;
边界塌陷:塌陷;
}
th,td{
填充物:5px;
}
名称
城市
状态
拉链
[姓氏],[姓氏]
[城市]
[国家]
[邮编]
我将从xml中获取值

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

地带
亚当
哥伦布
哦
44250
我想将表数据
元素中的值动态替换为:

<td>Zones, Adam</td>
<td>columbus</td>
<td>OH</td>
<td>44250</td>
分区,亚当 哥伦布 哦 44250
如何实现这一点,需要使用用户条目更改名称、城市、州、邮政编码。

在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文档路径作为参数


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

XSL不是这样工作的,它有一个特定的语法,与HTML无关。通常的方法是让XSLT处理XML输入并使用创建HTML的模板,然后在模板中使用XSLT语法,如
,…
。如果您使用自己的组合模板语法,那么您可能需要实现自己的编程语言来处理模板。