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

huangapple go评论63阅读模式
英文:

Rename Multiple XML tags to different name

问题

我正在寻求将标签转换为其他格式,但只转换父标签,而不转换子标签。
我希望在不改变结构的情况下转换标签。

例如:

<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 class="{@name}">
            <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="tr">
        <tr>
            <xsl:apply-templates select="node()"/>
        </tr>
    </xsl:template>
    <xsl:template match="td">
        <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>

一切都运行正常,但所有属性都被删除,我只得到标签而不是属性值。我想要保留所有属性并仅更改特定属性的名称。

英文:

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:

&lt;section name=&quot;ABC&quot;&gt;
    &lt;section name=&quot;123&quot;&gt;
	    &lt;p&gt;Data&lt;/p&gt;
	    &lt;p&gt;Data&lt;/p&gt;
	    &lt;p&gt;Data&lt;/p&gt;
    &lt;/section&gt;
    &lt;section name=&quot;456&quot;&gt;
	    &lt;table&gt;
		    &lt;tr&gt;
				&lt;td&gt;&lt;p&gt;Data&lt;/p&gt;&lt;/td&gt;
				&lt;td&gt;&lt;p&gt;Data&lt;/p&gt;&lt;/td&gt;
				&lt;td&gt;&lt;p&gt;Data&lt;/p&gt;&lt;/td&gt;
		    &lt;/tr&gt;
	    &lt;/table&gt;
    &lt;/section&gt;
    &lt;section name=&quot;232&quot;&gt;
	  &lt;bold&gt;&lt;p&gt;Data&lt;/p&gt;&lt;/bold&gt;
    &lt;/section&gt;
&lt;/section&gt;

to

&lt;div class=&quot;ABC&quot;&gt;
	&lt;div class=&quot;123&quot;&gt;
		&lt;h1&gt;Data&lt;/h1&gt;
		&lt;h1&gt;Data&lt;/h1&gt;
		&lt;h1&gt;Data&lt;/h1&gt;
	&lt;/div&gt;
	&lt;div class=&quot;456&quot;&gt;
		&lt;table&gt;
			&lt;tr&gt;
					&lt;td&gt;&lt;h1&gt;Data&lt;/h1&gt;&lt;/td&gt;
					&lt;td&gt;&lt;h1&gt;Data&lt;/h1&gt;&lt;/td&gt;
					&lt;td&gt;&lt;h1&gt;Data&lt;/h1&gt;&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/table&gt;
	&lt;/div&gt;
	&lt;div class=&quot;232&quot;&gt;
		&lt;bold&gt;&lt;h1&gt;Data&lt;/h1&gt;&lt;/bold&gt;
	&lt;/div&gt;
&lt;/div&gt;

This is what i have written in XSLT transformation.

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

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.

答案1

得分: 1

这应该可以完成任务:

<?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>
英文:

That should do the Job:

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

huangapple
  • 本文由 发表于 2020年8月11日 20:41:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63358375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定