必须有一种通用的方法来转换一些层次化的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的所有内容,所以我会及时解决的,但任何线索都会很有用。
发布于 2008-09-18 00:19:32
我不是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>发布于 2012-08-02 23:09:59
我需要一个类似的XSLT,但深度未知,下面是我是如何做到的。
首先,为生成的HTML table/def列表添加一个包装器,并将模板mode=命名为“puke”,以扁平化我们的XML树:
<xsl:element name="div">
<xsl:attribute name="class" select="puke" />
<xsl:apply-templates select="$notice" mode="puke" />
</xsl:element> 在这里,我们匹配每个节点以显示其内容(例如text())及其属性。我们递归地做这件事。我使用dl/dt/dd是因为我的源树是一个复杂的树,不能被扁平化为。
<!-- @description:
Display all field from the notice so the customer can tell what he want
-->
<xsl:template match="node()" mode="puke">
<xsl:message>
puking : <xsl:value-of select="local-name( . )" />
</xsl:message>
<xsl:element name="dl">
<xsl:element name="dt">
<xsl:attribute name="class">tagName</xsl:attribute>
<xsl:value-of select="local-name( . )" />
</xsl:element>
<xsl:element name="dd">
<xsl:attribute name="class">tagText</xsl:attribute>
<xsl:value-of select="text()" /></xsl:element>
<xsl:element name="dl">
<xsl:attribute name="class">attr</xsl:attribute>
<!-- display attribute -->
<xsl:apply-templates select="@*" />
</xsl:element>
</xsl:element>
<!-- recursive call on node() -->
<xsl:apply-templates select="./*" mode="puke" />
</xsl:template>匹配给定节点的属性并显示它们。
CSS在中使用来格式化生成的HTML:
<style>
.puke {
background-color: #BDD6DE;
clear: both;
}
.tagName, .attrName {
float: left;
}
.tagText, .attrText {
clear: right;
}
</style>发布于 2008-09-18 00:17:37
我们已经有了一个从Oracle数据库读取的Pro*C程序,它调用一个perl脚本,该脚本反过来执行一些Java从上述数据库中提取XML格式的数据,用于调用批处理文件来执行一些vbscript FTPing文件到其他服务器。我真的很希望用Fortran写点什么。
https://stackoverflow.com/questions/88773
复制相似问题