将属性和值填充到第4个父节点的XML文件的所有父节点

问题描述 投票:2回答:2

我是XSLT的新手,并希望从第二个父节点开始向所有父节点添加相同的Attribute和Value。这里的逻辑应该是如果有Main节点,属性(Mainattribute)应该是一次,而Main节点下的所有父节点的其余部分应该具有不同的属性(子属性),除了Main节点之外,它们应该是相同的。

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

            <?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。我已经尝试过这么多案例但却无法实现。谢谢

下面是为第一个Main节点尝试的XSLT,但不知何故得到错误并且无法继续进行。

        <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>
xml xslt xml-parsing xslt-1.0 nodes
2个回答
1
投票

增强@Aniket V的答案,您可以使用模式而不是取决于标签的名称:

<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元素,则此转换是您的:

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

0
投票

您对XSLT的更改非常接近。请修改模板如下。

<Main>的模板

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

名称为Parent的节点的模板。

<xsl:template match="*[contains(name(), 'Parent')]">
    <xsl:copy>
        <xsl:attribute name="childattribute">
            <xsl:value-of select="1" />
        </xsl:attribute>
        <xsl:apply-templates />
    </xsl:copy>
</xsl:template>
© www.soinside.com 2019 - 2024. All rights reserved.