Re: [xsl] Adding element to xml snippet using xslt

Subject: Re: [xsl] Adding element to xml snippet using xslt
From: Nishi Bhonsle <nishi.bhonsle@xxxxxxxxxx>
Date: Mon, 21 Feb 2005 14:01:31 -0800
Joris Gillis wrote:

Tempore 22:05:27, die 02/21/2005 AD, hinc in xsl-list@xxxxxxxxxxxxxxxxxxxxxx scripsit Nishi Bhonsle <nishi.bhonsle@xxxxxxxxxx>:

make that:

<xsl:template match="server">
    <xsl:element name="drillOutDir">
        <xsl:value-of select="abc:configuration/drillOutDir"/
    </xsl:element>
    <xsl:element name="started">
        <xsl:copy-of select="@status[. = 'true' ]"/>
        <xsl:value-of select="../abc:configuration/started"/>
    </xsl:element>
</xsl:template>



Hi: Apparently, this results in the following output -- <drillOutDir></drillOutDir> <started></started> <server> </server>

The status = true attribute of the started element does not show up?
The output for the started element should be --
<started status="true">This application was stopped.</started>

What could be wrong/missing in the above xslt?

Thanks.


Hi:
Any updates on this?


The original part of the XML document is actually too limited too solve your problems. Could you include a more complete and well-formed snippet?

<abc:configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="http://oracle.com/
xmlns/abcst/configurationconfiguration.xsd" locale="en" webcache="false" rmi="true">
<started status="true"></started>
<server></server>
..
....
</abc:configuration>



The 'status' attribute of the 'start' element is constructed by copying it from the 'server' node. Because this element has no 'status' attribute in the above XML, the 'started' element will not receive this attribute.


I don't understand were the text node 'This application was stopped.' should come from.

Hi:
The started element is at the same level as the server element and not a part of it.


I am providing a more complete snippet below of the orginal xml file say FileA --
<abc:configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="http://oracle.com/ xmlns/abcst/configurationconfiguration.xsd" locale="en" webcache="false" rmi="true">
<started status="true"></started>
<server></server>
<servlet autoRetryAfter="5" charting="true" chartingTimeout="60" queryRefreshPeriod="6"</servlet>>
....
</abc:configuration>


This FileA needs to be merged with the FileB using xslt to get a final resultant file FileC.
FileB contains --
<abc:configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="http://oracle.com/ xmlns/abcst/configurationconfiguration.xsd" locale="en" webcache="false" rmi="true" metadata="true" useMgmt="true">
<drillOutDir></drillOutDir>
<started status="true">This application was stopped.</started>
<server></server>
<servlet autoRetryAfter="5" charting="true" chartingTimeout="60" queryRefreshPeriod="6"</servlet>>
....
</abc:configuration>


FileC should contain should contain all elements of A and the new elements of B with the common ones being merged--
FileC should contain --
<abc:configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="http://oracle.com/ xmlns/abcst/configurationconfiguration.xsd" locale="en" webcache="false" rmi="true" metadata="true" useMgmt="true">
<drillOutDir></drillOutDir>
<started status="true">This application was stopped.</started>
<server></server>
<servlet autoRetryAfter="5" charting="true" chartingTimeout="60" queryRefreshPeriod="6"</servlet>>
....
</abc:configuration>


Currently, everything gets copied correctly to FileC except the fact that the "started" element is copied/merged as an empty element such as <started></started>. If I take out the "started" element from the "server" node and place it outside, it does not even showup in the final merged output.

<xsl:template match="abc:configuration">
<xsl:copy>
<xsl:copy-of select="@*[local-name() != 'rmi' and local-name() != 'locale']"/>
<xsl:attribute name="useMgmt">
<xsl:text>true</xsl:text></xsl:attribute>
<xsl:attribute name="metadata"><xsl:text>true</xsl:text></xsl:attribute>
<xsl:apply-templates>
<xsl:sort select="boolean(self::server)" order="descending"/>
</xsl:apply-templates>
</xsl:copy>
<xsl:text></xsl:text>
</xsl:template>


<xsl:template match="server">
   <xsl:element name="drillOutDir">
       <xsl:value-of select="abc:configuration/drillOutDir"/
   </xsl:element>
   <xsl:element name="started">
       <xsl:copy-of select="@status[. = 'true' ]"/>
       <xsl:value-of select="../abc:configuration/started"/>
   </xsl:element>
</xsl:template>

<xsl:template match="abc:configuration/started">
<xsl:apply-templates/>
</xsl:template>


Please help.

Thanks.



regards,

Current Thread