简体   繁体   English

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

[英]Rename Multiple XML tags to different name

I am looking to transform tag to other format but it is transform only parent tags not child tags.我希望将标签转换为其他格式,但它只是转换父标签而不是子标签。 I want to transform the tag without change in the structure.我想在不改变结构的情况下转换标签。

Eg:例如:

<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>

to

<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>

This is what i have written in XSLT transformation.这就是我在 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>

Every thing is working fine but all attributes are getting timed i am getting only tags but not attribute values.一切正常,但所有属性都在计时,我只得到标签而不是属性值。 I want to apply all attributes to be preserved and change name of specific attribute alone.我想应用所有要保留的属性并单独更改特定属性的名称。

That should do the Job:那应该做的工作:

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 XMLMapper 重命名 XML 标签 - rename XML tags using XMLMapper 使用SimpleFramework反序列化多个不同的标签(具有相同的名称) - Deserializing multiple different tags (with the same name) with SimpleFramework 通过Java为soapUI使用不同名称的方法生成相同的XML请求标签 - Generate same XML request tags for methods with different name for soapUI by Java 使用DOM中不同级别上具有相同名称的标签解析XML - Parsing XML with tags with same name on different levels in DOM 如何重命名 XML 节点名称 - How to rename XML node name 如何使用不同标签解组XML - How to unmarshalling xml with different tags 将类似的 xml 文件与具有无序标签的 XmlUnit 进行比较(相同的标签名称具有不同的属性) - Comparing similar xml files with XmlUnit with unordered tags (same tag name with different attributes) 使用 SAX 解析器,您如何解析具有相同名称标签但具有不同元素的 xml 文件? - using SAX parser, how do you parse an xml file which has same name tags but in different elements? 使用多个相同的标签解析XML - Parsing XML with multiple, identical tags 如何将具有相同名称的标签合并,然后在Java中重命名标签? - how to combine the tags with the same name and then rename the tag inside it in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM