gpt4 book ai didi

xml - 将 XML 扁平化为 HTML 表

转载 作者:数据小太阳 更新时间:2023-10-29 01:51:17 24 4
gpt4 key购买 nike

必须有一种通用的方法来转换一些分层的 XML,例如:

<element1 A="AValue" B="BValue">
<element2 C="DValue" D="CValue">
<element3 E="EValue1" F="FValue1"/>
<element3 E="EValue2" F="FValue2"/>
</element2>
...
</element1>

在扁平化的 XML (html) 中沿途选取选定的属性,并为成为列标题的属性提供不同的标签。

<table>
<tr>
<th>A_Label</th>
<th>D_Label</th>
<th>E_Label</th>
<th>F_Label</th>
</tr>
<tr>
<td>AValue</td>
<td>DValue</td>
<td>EValue1</td>
<td>FValue1</td>
</tr>
<tr>
<td>AValue</td>
<td>DValue</td>
<td>EValue2</td>
<td>FValue2</td>
</tr>
<table>

好吧,由于属性重新标记,所以没有通用的解决方案,但希望您明白我的意思。我刚刚开始研究所有 XSLT/XPATH 的东西,所以我会适时解决它,但任何线索都会有用。

最佳答案

我不是 100% 确定您要尝试做什么,但如果您的 element1、element2 和 element3 嵌套一致,则此解决方案可能有效。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
<table>
<xsl:apply-templates select="//element3"></xsl:apply-templates>
</table>
</xsl:template>

<xsl:template match="element3">
<tr>
<td><xsl:value-of select="../../@A"/></td>
<td><xsl:value-of select="../../@B"/></td>
<td><xsl:value-of select="../@C"/></td>
<td><xsl:value-of select="../@D"/></td>
<td><xsl:value-of select="@E"/></td>
<td><xsl:value-of select="@F"/></td>
</tr>
<xsl:apply-templates select="*"></xsl:apply-templates>
</xsl:template>

</xsl:stylesheet>

关于xml - 将 XML 扁平化为 HTML 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/88773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com