将属性和值从第 4 个父节点填充到 XML 文件的所有父节点 [英] Populate Attribute and values to all parent nodes of the XML file from 4th parent node

查看:38
本文介绍了将属性和值从第 4 个父节点填充到 XML 文件的所有父节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 XSLT 的新手,想向从第二个父节点开始的所有父节点添加相同的属性和值.这里的逻辑应该是如果有主节点,属性(Mainattribute)应该是一次,对于主节点下的所有父节点的其余部分应该具有不同的属性(子属性),除了主节点之外的所有其他节点都应该相同.

I am new to XSLT and would like to add Attribute and Value of the same to all parent nodes starting from 2nd parent node. here the logic should be the if there is Main node, the attribute(Mainattribute) should be one time and for rest of all parent nodes under the Main node should have different attribute(childattribute) which should be same across all other than Main node.

我们有一个像下面这样的输入xml:它只有一些字段,理想情况下会有更多标签并且可能不同.

we have a input xml like below : It is just have some fields only, ideally there will be more tags and may differ.

            <?xml version="1.0" encoding="UTF-8"?>
            <Header>
                <Main>
                    <Parent2>
                        <status>12</status>
                        <statusmsg>Helo</status_text>
                    </Parent2>
                    <Parent3>
                        <Child1>112</Child1>
                        <Child2>Hai</Child2>
                    </Parent3>
                    <Parent4>
                        <Child3>Valley</Child3>
                        <Parent5>
                            <Child7>Kind</Child1>
                            <Child8>Pls</Child2>
                        </Parent5>
                    </Parent4>
                </Main>
            </Header>

输出应如下所示:

        <?xml version="1.0" encoding="UTF-8"?>
        <Header>
            <Main Mainattribute="1">
                <Parent2 childattribute="1">
                    <status>12</status>
                    <statusmsg>Helo</status_text>
                </Parent2>
                <Parent3 childattribute="1">
                    <Child1>112</Child1>
                    <Child2>Hai</Child2>
                </Parent3>
                <Parent4 childattribute="1">
                    <Child3>Valley</Child3>
                    <Parent5 childattribute="1">
                        <Child7>Kind</Child1>
                        <Child8>Pls</Child2>
                    </Parent5>
                </Parent4>
            </Main>
        </Header>

有人可以共享 XSLT 吗?我尝试了很多案例,但无法实现.谢谢

Can some one please share XSLT for the same. i have tried so many cases but not able to achieve it. thank you

下面是第一个主节点的 XSLT 尝试,但不知何故出错,无法继续.

Below is the XSLT tried for first Main node, but somehow getting error and couldnt proceed further.

        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <xsl:output indent="yes"/>
            <!-- Template to copy all the elements -->
            <xsl:template match="@*|node()">
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
            </xsl:template>

            <xsl:template match="Main">
                <Main>
                    <xsl:attribute name="Mainattribute">
                        <xsl:value-of select="1"/>
                    </xsl:attribute>
                    <xsl:apply-templates select="child::node()"/>
                </Main>
            </xsl:template>



        </xsl:stylesheet>

推荐答案

增强@Aniket V 的回答,你可以求助于模式而不是依赖于标签的名称:

Enhancing @Aniket V's answer, you can resort to modes instead of depending on the name of the tag:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="Main">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:attribute name="Mainattribute"><xsl:value-of select="1"/></xsl:attribute>
            <xsl:apply-templates mode="parent_mode"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*" mode="parent_mode">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:attribute name="childattribute"><xsl:value-of select="1"/></xsl:attribute>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

<小时>

更新

如果您想要更新所有具有子元素(但不是顶级元素)的 XML 元素,那么此转换就是您的:

If you waht to update all XML elements which have children (but not the top level element), then this transformation is yours:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="Main" priority="1">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:attribute name="Mainattribute"><xsl:value-of select="1"/></xsl:attribute>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*[child::* and ancestor::*]">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:attribute name="childattribute"><xsl:value-of select="1"/></xsl:attribute>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

这篇关于将属性和值从第 4 个父节点填充到 XML 文件的所有父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆