Javascript 动态重新填充HTML表
我有一些简单的XML,如下所示:Javascript 动态重新填充HTML表,javascript,xml,json,xslt,xmlhttprequest,Javascript,Xml,Json,Xslt,Xmlhttprequest,我有一些简单的XML,如下所示: <Tag> <Keyphrases> <Keyphrase Phrase = "This is phrase one"/> <Keyphrase Phrase = "This is phrase two"/> <Keyphrase Phrase = "This is phrase three"/> </Keyphrases> <
<Tag>
<Keyphrases>
<Keyphrase Phrase = "This is phrase one"/>
<Keyphrase Phrase = "This is phrase two"/>
<Keyphrase Phrase = "This is phrase three"/>
</Keyphrases>
</Tag>
我宁愿避免在这个项目中使用任何库,比如jQuery
更新:
谢谢你的回复。一些评论和澄清:
我强烈建议在这个任务中使用JSON而不是XML——正如您已经意识到的,您可能不想在客户端解析XML 一旦将XHR响应作为JSON数据,就可以使用客户端模板将响应转换为标记,然后将标记放在页面上。如果您改变了对jQuery的看法,那么jQuery有一个模板插件(),您可以使用它来实现这一点;还有独立的和非常轻量级的。当然,您也可以编写自己的模板代码。所有客户端模板工具都遵循相同的一般原则(尽管语法不同,因此请务必阅读文档),采用如下模板:
<p>Hello, my name is ${firstName} ${lastName}</p>
。。。然后组合它们来创建所需的输出,然后可以将其添加到DOM中
如果要更新现有表,可以为完整表生成HTML,然后删除旧表并插入新表。或者,您可以只追加新行——这取决于您尝试执行的操作的确切性质
最后:正如评论者提到的,我会重新考虑你反对使用任何类型的库,除非有真正令人信服的理由不这样做。重新发明轮子没有什么好处。我强烈建议在这个任务中使用JSON而不是XML——正如您已经意识到的,您可能不想在客户端解析XML 一旦将XHR响应作为JSON数据,就可以使用客户端模板将响应转换为标记,然后将标记放在页面上。如果您改变了对jQuery的看法,那么jQuery有一个模板插件(),您可以使用它来实现这一点;还有独立的和非常轻量级的。当然,您也可以编写自己的模板代码。所有客户端模板工具都遵循相同的一般原则(尽管语法不同,因此请务必阅读文档),采用如下模板:
<p>Hello, my name is ${firstName} ${lastName}</p>
。。。然后组合它们来创建所需的输出,然后可以将其添加到DOM中
如果要更新现有表,可以为完整表生成HTML,然后删除旧表并插入新表。或者,您可以只追加新行——这取决于您尝试执行的操作的确切性质
最后:正如评论者提到的,我会重新考虑你反对使用任何类型的库,除非有真正令人信服的理由不这样做。重新发明轮子没有什么好处。如果您愿意,您可以在我的库中看看我是如何做到这一点的,函数调用是一组处理JSON的函数。函数用于进行AJAX调用,我对跨浏览器兼容性做了大量的研究,所以它是一个很好的例子 如果您使用的是PHP,则可以将数据编码为JSON并将其传回给您的JS,如下所示:
echo json_encode($myData);
我不认为这已经解决了你的问题,但我确实认为这已经帮助你更接近了。如果你愿意,你可以在我的库中看看我是如何做到这一点的,函数调用一组处理JSON的函数。函数用于进行AJAX调用,我对跨浏览器兼容性做了大量的研究,所以它是一个很好的例子 如果您使用的是PHP,则可以将数据编码为JSON并将其传回给您的JS,如下所示:
echo json_encode($myData);
我认为这并没有解决您的问题,但我确实认为这有助于您更进一步。这里是一个XSLT转换,其中更新被接受为外部参数。它作为源XML文档在当前表上运行:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pReps">
<updates>
<replace line="2">This is new phrase two</replace>
<delete line="3"/>
<insert-before line="1">This is phrase zero (new)</insert-before>
<insert-after line="4">This is phrase five (new)</insert-after>
</updates>
</xsl:param>
<xsl:variable name="vReps"
select="ext:node-set($pReps)/*/*"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tr">
<xsl:choose>
<xsl:when test="not(position()=$vReps/@line)">
<xsl:call-template name="identity"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vPos" select="position()"/>
<xsl:variable name="vupdCommand"
select="$vReps[@line=$vPos]"/>
<xsl:choose>
<xsl:when test="$vupdCommand[self::replace]">
<tr><td><xsl:value-of select="$vupdCommand"/></td></tr>
</xsl:when>
<xsl:when test="$vupdCommand[self::insert-before]">
<tr><td><xsl:value-of select="$vupdCommand"/></td></tr>
<xsl:call-template name="identity"/>
</xsl:when>
<xsl:when test="$vupdCommand[self::insert-after]">
<xsl:call-template name="identity"/>
<tr><td><xsl:value-of select="$vupdCommand"/></td></tr>
</xsl:when>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<tabe id="realtimetable">
<tr><td>This is phrase one</td></tr>
<tr><td>This is phrase two</td></tr>
<tr><td>This is phrase three</td></tr>
<tr><td>This is phrase four</td></tr>
</tabe>
<tabe id="realtimetable">
<tr>
<td>This is phrase zero (new)</td>
</tr>
<tr>
<td>This is phrase one</td>
</tr>
<tr>
<td>This is new phrase two</td>
</tr>
<tr>
<td>This is phrase four</td>
</tr>
<tr>
<td>This is phrase five (new)</td>
</tr>
</tabe>
这是新的短语二
这是短语零(新)
这是第五阶段(新)
将此转换应用于当前表时:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pReps">
<updates>
<replace line="2">This is new phrase two</replace>
<delete line="3"/>
<insert-before line="1">This is phrase zero (new)</insert-before>
<insert-after line="4">This is phrase five (new)</insert-after>
</updates>
</xsl:param>
<xsl:variable name="vReps"
select="ext:node-set($pReps)/*/*"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tr">
<xsl:choose>
<xsl:when test="not(position()=$vReps/@line)">
<xsl:call-template name="identity"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vPos" select="position()"/>
<xsl:variable name="vupdCommand"
select="$vReps[@line=$vPos]"/>
<xsl:choose>
<xsl:when test="$vupdCommand[self::replace]">
<tr><td><xsl:value-of select="$vupdCommand"/></td></tr>
</xsl:when>
<xsl:when test="$vupdCommand[self::insert-before]">
<tr><td><xsl:value-of select="$vupdCommand"/></td></tr>
<xsl:call-template name="identity"/>
</xsl:when>
<xsl:when test="$vupdCommand[self::insert-after]">
<xsl:call-template name="identity"/>
<tr><td><xsl:value-of select="$vupdCommand"/></td></tr>
</xsl:when>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<tabe id="realtimetable">
<tr><td>This is phrase one</td></tr>
<tr><td>This is phrase two</td></tr>
<tr><td>This is phrase three</td></tr>
<tr><td>This is phrase four</td></tr>
</tabe>
<tabe id="realtimetable">
<tr>
<td>This is phrase zero (new)</td>
</tr>
<tr>
<td>This is phrase one</td>
</tr>
<tr>
<td>This is new phrase two</td>
</tr>
<tr>
<td>This is phrase four</td>
</tr>
<tr>
<td>This is phrase five (new)</td>
</tr>
</tabe>
这是第一阶段
这是第二阶段
这是第三阶段
这是第四句
生成所需的正确结果:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pReps">
<updates>
<replace line="2">This is new phrase two</replace>
<delete line="3"/>
<insert-before line="1">This is phrase zero (new)</insert-before>
<insert-after line="4">This is phrase five (new)</insert-after>
</updates>
</xsl:param>
<xsl:variable name="vReps"
select="ext:node-set($pReps)/*/*"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tr">
<xsl:choose>
<xsl:when test="not(position()=$vReps/@line)">
<xsl:call-template name="identity"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vPos" select="position()"/>
<xsl:variable name="vupdCommand"
select="$vReps[@line=$vPos]"/>
<xsl:choose>
<xsl:when test="$vupdCommand[self::replace]">
<tr><td><xsl:value-of select="$vupdCommand"/></td></tr>
</xsl:when>
<xsl:when test="$vupdCommand[self::insert-before]">
<tr><td><xsl:value-of select="$vupdCommand"/></td></tr>
<xsl:call-template name="identity"/>
</xsl:when>
<xsl:when test="$vupdCommand[self::insert-after]">
<xsl:call-template name="identity"/>
<tr><td><xsl:value-of select="$vupdCommand"/></td></tr>
</xsl:when>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<tabe id="realtimetable">
<tr><td>This is phrase one</td></tr>
<tr><td>This is phrase two</td></tr>
<tr><td>This is phrase three</td></tr>
<tr><td>This is phrase four</td></tr>
</tabe>
<tabe id="realtimetable">
<tr>
<td>This is phrase zero (new)</td>
</tr>
<tr>
<td>This is phrase one</td>
</tr>
<tr>
<td>This is new phrase two</td>
</tr>
<tr>
<td>This is phrase four</td>
</tr>
<tr>
<td>This is phrase five (new)</td>
</tr>
</tabe>
这是短语零(新)
这是第一阶段
这是新的短语二
这是第四句
这是第五阶段(新)
这里是一个XSLT转换,其更新被接受为外部参数。它作为源XML文档在当前表上运行:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pReps">
<updates>
<replace line="2">This is new phrase two</replace>
<delete line="3"/>
<insert-before line="1">This is phrase zero (new)</insert-before>
<insert-after line="4">This is phrase five (new)</insert-after>
</updates>
</xsl:param>
<xsl:variable name="vReps"
select="ext:node-set($pReps)/*/*"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tr">
<xsl:choose>
<xsl:when test="not(position()=$vReps/@line)">
<xsl:call-template name="identity"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vPos" select="position()"/>
<xsl:variable name="vupdCommand"
select="$vReps[@line=$vPos]"/>
<xsl:choose>
<xsl:when test="$vupdCommand[self::replace]">
<tr><td><xsl:value-of select="$vupdCommand"/></td></tr>
</xsl:when>
<xsl:when test="$vupdCommand[self::insert-before]">
<tr><td><xsl:value-of select="$vupdCommand"/></td></tr>
<xsl:call-template name="identity"/>
</xsl:when>
<xsl:when test="$vupdCommand[self::insert-after]">
<xsl:call-template name="identity"/>
<tr><td><xsl:value-of select="$vupdCommand"/></td></tr>
</xsl:when>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<tabe id="realtimetable">
<tr><td>This is phrase one</td></tr>
<tr><td>This is phrase two</td></tr>
<tr><td>This is phrase three</td></tr>
<tr><td>This is phrase four</td></tr>
</tabe>
<tabe id="realtimetable">
<tr>
<td>This is phrase zero (new)</td>
</tr>
<tr>
<td>This is phrase one</td>
</tr>
<tr>
<td>This is new phrase two</td>
</tr>
<tr>
<td>This is phrase four</td>
</tr>
<tr>
<td>This is phrase five (new)</td>
</tr>
</tabe>
这是新的短语二
这是短语零(新)
这是第五阶段(新)