将多个XML标签重命名为不同的名称

问题描述

我正在寻找将标签转换为其他格式的方法,但它只能转换父标签而不是子标签。 我想在不更改结构的情况下转换标签。

例如:

<section name="ABC">
    <section name="123">
        <p>Data</p>
        <p>Data</p>
        <p>Data</p>
    </section>
    <section name="456">
        <table>
            <tr>
                <td><p>Data</p></td>
                <td><p>Data</p></td>
                <td><p>Data</p></td>
            </tr>
        </table>
    </section>
    <section name="232">
      <bold><p>Data</p></bold>
    </section>
</section>

<div class="ABC">
    <div class="123">
        <h1>Data</h1>
        <h1>Data</h1>
        <h1>Data</h1>
    </div>
    <div class="456">
        <table>
            <tr>
                    <td><h1>Data</h1></td>
                    <td><h1>Data</h1></td>
                    <td><h1>Data</h1></td>
            </tr>
        </table>
    </div>
    <div class="232">
        <bold><h1>Data</h1></bold>
    </div>
</div>

这是我在XSLT转换中写的。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:strip-space elements="*"/>
        <xsl:template match="section"><div><xsl:apply-templates select="node()"/></div></xsl:template>
        <xsl:template match="p"><p><xsl:apply-templates select="node()"/></p></xsl:template>
        <xsl:template match="table"><table><xsl:apply-templates select="node()"/></table></xsl:template>
        <xsl:template match="row"><tr><xsl:apply-templates select="node()"/></tr></xsl:template>
        <xsl:template match="cell"><td><xsl:apply-templates select="node()"/></td></xsl:template>
        <xsl:template match="image"><img><xsl:apply-templates select="node()"/></img></xsl:template>
        
        <xsl:template match="/">
              <html>
              <body>
              <xsl:apply-templates/>
              </body>
              </html>
        </xsl:template>
</xsl:stylesheet>

一切正常,但所有属性都已计时,我仅获得标签,而没有获得属性值。我想应用所有要保留的属性,并单独更改特定属性的名称。

解决方法

那应该做的工作:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*"/>
    <xsl:template match="section"><div><xsl:apply-templates select="@*|node()"/></div></xsl:template>
    <xsl:template match="p"><h1><xsl:apply-templates select="@*|node()"/></h1></xsl:template>
    <xsl:template match="table"><table><xsl:apply-templates select="@*|node()"/></table></xsl:template>
    <xsl:template match="row"><tr><xsl:apply-templates select="@*|node()"/></tr></xsl:template>
    <xsl:template match="cell"><td><xsl:apply-templates select="@*|node()"/></td></xsl:template>
    <xsl:template match="image"><img><xsl:apply-templates select="@*|node()"/></img></xsl:template>
    <xsl:template match="@name"><xsl:attribute name="class"><xsl:value-of select="."/></xsl:attribute></xsl:template>
    <!-- identity transformation -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...