Reputation: 101
i have the following input xml :
<?xml version="1.0" encoding="UTF-8"?>
<class xmlns:xfa="soommmeee-tteexxxxtttttt">
<students>
------
------
------
------
</students>
<students>
<id>B</id>
<name>jzvxcbhjnba</name>
<description>mjZSVxcj</description>
<student>
<id>3</id>
<refObjectId>m8</refObjectId>
<subject>
<id>91</id>
<name>humanities</name>
<friend>hercules</friend>
<firstname>
<textContent>
<Id>m954</Id>
<title xfa:contentType="text/html">
<body>Title</body>
</title>
<text xfa:contentType="text/html">
<body>Current</body>
</text>
<Key>Title</Key>
</textContent>
<textContent>
<Id>m9542</Id>
<title xfa:contentType="text/html">
<body>SubTitle</body>
</title>
<text xfa:contentType="text/html">
<body>Sr</body>
</text>
<Key>SubTitle</Key>
</textContent>
<isDisplay>false</isDisplay>
</firstname>
<lastname>
<Detail>
<lastNameDetail>
<fragId>aksbgdk</fragId>
<znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc>
<zmdcb>slzdhfksjfml</zmdcb>
</lastNameDetail>
</Detail>
</lastname>
</subject>
</student>
<student>
<id>3</id>
<refObjectId>m8</refObjectId>
<subject>
<id>91</id>
<name>humanities</name>
<friend>aladin</friend>
<firstname>
<textContent>
<Id>m954</Id>
<title xfa:contentType="text/html">
<body>Title</body>
</title>
<text xfa:contentType="text/html">
<body>Current</body>
</text>
<Key>Title</Key>
</textContent>
<textContent>
<Id>m9542</Id>
<title xfa:contentType="text/html">
<body>SubTitle</body>
</title>
<text xfa:contentType="text/html">
<body>Sr</body>
</text>
<Key>SubTitle</Key>
</textContent>
<isDisplay>false</isDisplay>
</firstname>
<lastname>
<Detail>
<lastNameDetail>
<fragId>aksbgdk</fragId>
<znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc>
<zmdcb>slzdhfksjfml</zmdcb>
</lastNameDetail>
</Detail>
</lastname>
</subject>
</student>
<student>
<id>3</id>
<refObjectId>m8</refObjectId>
<subject>
<id>91</id>
<name>humanities</name>
<friend>aladin</friend>
<firstname>
<textContent>
<Id>m954</Id>
<title xfa:contentType="text/html">
<body>Title</body>
</title>
<text xfa:contentType="text/html">
<body>Current</body>
</text>
<Key>Title</Key>
</textContent>
<textContent>
<Id>m9542</Id>
<title xfa:contentType="text/html">
<body>SubTitle</body>
</title>
<text xfa:contentType="text/html">
<body>Sr</body>
</text>
<Key>SubTitle</Key>
</textContent>
<isDisplay>false</isDisplay>
</firstname>
<lastname>
<Detail>
<lastNameDetail>
<fragId>aksbgdk</fragId>
<znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc>
<zmdcb>slzdhfksjfml</zmdcb>
</lastNameDetail>
</Detail>
</lastname>
</subject>
</student>
<student>
<id>4</id>
<refObjectId>m3</refObjectId>
<subject>
<id>75</id>
<name>PCM</name>
<friend>hercules</friend>
<firstname>
<textContent>
<Id>j4423</Id>
<title xfa:contentType="text/html">
<body>Title</body>
</title>
<text xfa:contentType="text/html">
<body>dfzxsdbjchA</body>
</text>
<Key>zdbgcfjkbna</Key>
</textContent>
<textContent>
<Id>m95</Id>
<title xfa:contentType="text/html">
<body>zdjbhfjkcda</body>
</title>
<text xfa:contentType="text/html">
<body>sdjkhzjk</body>
</text>
<Key>mzsdjkbjk</Key>
</textContent>
<isDisplay>true</isDisplay>
</firstname>
<lastname>
<Detail>
<lastNameDetail>
<fragId>sdbnfn</fragId>
<znjdvgsc>masdjkljk</znjdvgsc>
<zmdcb>msjkldbhfjkn</zmdcb>
</lastNameDetail>
</Detail>
</lastname>
</subject>
</student>
</students>
<students>
--------
-------
---------
--------
</students>
<faculty>
<configuration>
<Type>Output</Type>
<Key>Font</Key>
<Value>10pt</Value>
</configuration>
<configuration>
<Type>Out</Type>
<Key>Text</Key>
<Value>127,127,127</Value>
</configuration>
<configuration>
<Type>put</Type>
<Key>Slide</Key>
<Value>18pt</Value>
</configuration>
<configuration>
<Type>utp</Type>
<Key>Slide</Key>
<Value>127</Value>
</configuration>
</faculty>
<Info />
<Dean>
<name>zcdfjkaqbkd</name>
</Dean>
</class>
what i want to do is that the child node 'id' of every parent node, which may or may not be present in every tree should become attribute of its parent node and the id node should be removed from the xml permanently.
i am using xslt 1.0, below is my xsl file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
version="1.0">
<xsl:output indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates
select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="id" />
<xsl:template match="subject">
<subject id="{id}">
<xsl:copy>
<xsl:apply-templates select="child::node()" />
</xsl:copy>
</subject>
</xsl:template>
<xsl:template match="students">
<students id="{id}">
<xsl:copy>
<xsl:apply-templates select="child::node()" />
</xsl:copy>
</students>
</xsl:template>
<xsl:template match="student">
<student id="{id}">
<xsl:copy>
<xsl:apply-templates select="child::node()" />
</xsl:copy>
</student>
</xsl:template>
</xsl:stylesheet>
what my xslt is doing that it is making the attribute in the parent node as required, it is removing the id node that too as desired but it is creating a new copy of the parent node my current output is attached herewith:
<?xml version="1.0" encoding="UTF-8"?><class xmlns:xfa="soommmeee-tteexxxxtttttt">
<students id="A">
<students>
-----
-----
------
</students>
</students>
<students id="B">
<students>
<name>jzvxcbhjnba</name>
<description>mjZSVxcj</description>
<student id="3">
<student>
<refObjectId>m8</refObjectId>
<subject id="91">
<subject>
<name>humanities</name>
<friend>hercules</friend>
<firstname>
<textContent>
<Id>m954</Id>
<title xfa:contentType="text/html">
<body>Title</body>
</title>
<text xfa:contentType="text/html">
<body>Current</body>
</text>
<Key>Title</Key>
</textContent>
<textContent>
<Id>m9542</Id>
<title xfa:contentType="text/html">
<body>SubTitle</body>
</title>
<text xfa:contentType="text/html">
<body>Sr</body>
</text>
<Key>SubTitle</Key>
</textContent>
<isDisplay>false</isDisplay>
</firstname>
<lastname>
<Detail>
<lastNameDetail>
<fragId>aksbgdk</fragId>
<znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc>
<zmdcb>slzdhfksjfml</zmdcb>
</lastNameDetail>
</Detail>
</lastname>
</subject>
</subject>
</student>
</student>
<student id="3">
<student>
<refObjectId>m8</refObjectId>
<subject id="91">
<subject>
<name>humanities</name>
<friend>aladin</friend>
<firstname>
<textContent>
<Id>m954</Id>
<title xfa:contentType="text/html">
<body>Title</body>
</title>
<text xfa:contentType="text/html">
<body>Current</body>
</text>
<Key>Title</Key>
</textContent>
<textContent>
<Id>m9542</Id>
<title xfa:contentType="text/html">
<body>SubTitle</body>
</title>
<text xfa:contentType="text/html">
<body>Sr</body>
</text>
<Key>SubTitle</Key>
</textContent>
<isDisplay>false</isDisplay>
</firstname>
<lastname>
<Detail>
<lastNameDetail>
<fragId>aksbgdk</fragId>
<znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc>
<zmdcb>slzdhfksjfml</zmdcb>
</lastNameDetail>
</Detail>
</lastname>
</subject>
</subject>
</student>
</student>
<student id="3">
<student>
<refObjectId>m8</refObjectId>
<subject id="91">
<subject>
<name>humanities</name>
<friend>aladin</friend>
<firstname>
<textContent>
<Id>m954</Id>
<title xfa:contentType="text/html">
<body>Title</body>
</title>
<text xfa:contentType="text/html">
<body>Current</body>
</text>
<Key>Title</Key>
</textContent>
<textContent>
<Id>m9542</Id>
<title xfa:contentType="text/html">
<body>SubTitle</body>
</title>
<text xfa:contentType="text/html">
<body>Sr</body>
</text>
<Key>SubTitle</Key>
</textContent>
<isDisplay>false</isDisplay>
</firstname>
<lastname>
<Detail>
<lastNameDetail>
<fragId>aksbgdk</fragId>
<znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc>
<zmdcb>slzdhfksjfml</zmdcb>
</lastNameDetail>
</Detail>
</lastname>
</subject>
</subject>
</student>
</student>
<student id="4">
<student>
<refObjectId>m3</refObjectId>
<subject id="75">
<subject>
<name>PCM</name>
<friend>hercules</friend>
<firstname>
<textContent>
<Id>j4423</Id>
<title xfa:contentType="text/html">
<body>Title</body>
</title>
<text xfa:contentType="text/html">
<body>dfzxsdbjchA</body>
</text>
<Key>zdbgcfjkbna</Key>
</textContent>
<textContent>
<Id>m95</Id>
<title xfa:contentType="text/html">
<body>zdjbhfjkcda</body>
</title>
<text xfa:contentType="text/html">
<body>sdjkhzjk</body>
</text>
<Key>mzsdjkbjk</Key>
</textContent>
<isDisplay>true</isDisplay>
</firstname>
<lastname>
<Detail>
<lastNameDetail>
<fragId>sdbnfn</fragId>
<znjdvgsc>masdjkljk</znjdvgsc>
<zmdcb>msjkldbhfjkn</zmdcb>
</lastNameDetail>
</Detail>
</lastname>
</subject>
</subject>
</student>
</student>
</students>
</students>
<students id="B">
<students>
----
-----
-----
----
</students>
</students>
<faculty>
<configuration>
<Type>Output</Type>
<Key>Font</Key>
<Value>10pt</Value>
</configuration>
<configuration>
<Type>Out</Type>
<Key>Text</Key>
<Value>127,127,127</Value>
</configuration>
<configuration>
<Type>put</Type>
<Key>Slide</Key>
<Value>18pt</Value>
</configuration>
<configuration>
<Type>utp</Type>
<Key>Slide</Key>
<Value>127</Value>
</configuration>
</faculty>
<Info/>
<Dean>
<name>zcdfjkaqbkd</name>
</Dean>
</class>
Please help...i am stuck here!!!
Upvotes: 0
Views: 132
Reputation: 70648
It is creating a copy of the "parent" node because you are using xsl:copy
as well as creating a new node manually. xsl:copy
will create a copy of the current node, so you end up with two. The one you create manually with <student id="{id}">
and the one you copy with xsl:copy
.
So, your template should really look like this:
<xsl:template match="subject">
<subject id="{id}">
<xsl:apply-templates />
</subject>
</xsl:template>
However, you can make your XSLT more generic, to match any element with a child id
node.
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
version="1.0">
<xsl:output indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates
select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="id" />
<xsl:template match="*[id]">
<xsl:copy>
<xsl:attribute name="id">
<xsl:value-of select="id" />
</xsl:attribute>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2