从第四个父节点填充XML文件的所有父节点的属性和值



我是XSLT的新手,希望将相同的Attribute和Value添加到从第二个父节点开始的所有父节点。这里的逻辑应该是,如果有Main节点,则属性(Mainattribute(应该是一次,对于Main节点下的所有父节点的其余部分,应该具有不同的属性(childattribute。

我们有一个输入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吗。我尝试了很多案例,但都没能做到。谢谢

以下是XSLT尝试用于第一个Main节点,但不知何故出现错误,无法继续。

<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的答案,您可以求助于模式,而不是依赖于标签的名称:

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

您已经非常接近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>

最新更新