【问题标题】:XSLT Convert generic XMLXSLT 转换通用 XML
【发布时间】:2011-11-15 21:17:05
【问题描述】:

我有一个使用javax.sql.rowset.WebRowSet.writeXml 生成的 XML 文件,它看起来像:

<metadata>
This section has column properties like name / label etc
</metadata>
<data>
<currentRow>
      <columnValue>Ken</columnValue>
      <columnValue>12</columnValue>
      <columnValue>USA</columnValue>
    </currentRow>
</data>

我想将其转换为:

<Class>
     <Student>
      <name>Ken</name>
      <ID>12</ID>
      <location>USA</location>
    </Student>
</Class>

如何进行转换?我需要它来将 XML 转换为 HTML 表。

【问题讨论】:

  • 所以currentRow元素只有一个,columnValue元素有多个?

标签: xslt


【解决方案1】:

以下样式表会产生所需的结果:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <Class>
            <xsl:apply-templates select="/*/data/currentRow" />
        </Class>
    </xsl:template>
    <xsl:template match="currentRow">
        <Student>
            <xsl:apply-templates select="columnValue" />
        </Student>
    </xsl:template>
    <xsl:template match="columnValue[1]">
        <name><xsl:apply-templates/></name>
    </xsl:template>
    <xsl:template match="columnValue[2]">
        <ID><xsl:apply-templates/></ID>
    </xsl:template>
    <xsl:template match="columnValue[3]">
        <location><xsl:apply-templates/></location>
    </xsl:template>
</xsl:stylesheet>

注意:向给定的源添加了一个根节点以使其格式正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 2011-08-29
    • 1970-01-01
    相关资源
    最近更新 更多