Hello,
I'm attempting to "populate" an template XML file with some data using
identity transform (approach suggested by Joe K.; Thanks! :-) and have
come across some strange behaviour using XMLSpy, AltovaXML, and
xsltproc. The actual stylesheets are to be auto-generated by another
stylesheet (that process I've worked out! :-) and now troubleshooting
these generated stylesheets in XMLSpy's XSL debugger. So I'm wondering
if my approach is basically wrong to elicit the unexpected behaviour,
rather than that the apps are buggy. So here goes:
The transformation stylesheet is:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/message/Control/Trace/Id">
<xsl:attribute name="extension">bbb-999</xsl:attribute>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
nothing fancy except that I'm trying to put/add value to the
"/message/Control/Trace/Id/@extension" attribute and the xml to be
transformed is:
<?xml version="1.0" encoding="ISO-8859-1"?>
<message>
<id>Text</id>
<creationTime>Text</creationTime>
<versionCode/>
<Id>Text</Id>
<processingCode/>
<processingModeCode/>
<acceptCode/>
<commRcv>
<device>
<id extension="Old_Ext">Text</id>
</device>
</commRcv>
<commSnd>
<device>
<id>Text</id>
</device>
</commSnd>
<Control>
<author>
<Entity>
<id>Text</id>
<code/>
<Organization>
<id>Text</id>
</Organization>
</Entity>
</author>
<Trace>
<Id extension="OLD">Text</Id>
<person>
<value/>
<Text>String</Text>
</person>
<birthTime>
<value>Text</value>
<Text>String</Text>
</birthTime>
<name>
<value>Text</value>
<Text>String</Text>
</name>
</Trace>
</Control>
</message>
I'm unsure if I am doing anything illegal as far as XSL/T etc. is
concerned but here are the results:
XMLSpy + AltovaXML add the attribute to /message/Control/Trace instead,
and the /message/Control/Trace/Id element isn't created at all.
xsltproc:
Gives the error "xsl:attribute: Cannot add attributes to an element if
children have been already added to the element." That is very odd since
the /message/Control/Trace/Id/ element doesn't have any children!
I've tried various permutations of the stylesheet as follows:
Trial #1: Populate attribute & element
<xsl:attribute name="extension">bbb-999</xsl:attribute>
<xsl:elementname="Id">xsl-123456789</xsl:element>
XMLSpy/AltovaXML add the extension to the parent /message/Control/Trace
element and the "Id" element isn't created. xsltproc errors as before.
Trial #2: Populate element & attribute
<xsl:elementname="Id">xsl-123456789</xsl:element>
<xsl:attribute name="extension">bbb-999</xsl:attribute>
XMLSpy/AltovaXML add/populate the "Id" element correctly but the
"extension" attribute is missing. xsltproc errors as before.
Trial #3: Populate element
<xsl:elementname="Id">xsl-123456789</xsl:element>
Value for "Id" element is add without problem by all.
Trial #4: Two seperate templates
This was my original approach but didn't work (see below). Created two
seperate template, one for the element and one for the attribute:
<xsl:template match="/message/Control/Trace/Id/@extension">
<xsl:attribute
name="extension">bbb-999</xsl:attribute> </xsl:template>
<xsl:template match="/message/Control/Trace/Id">
<xsl:element name="queryId">xsl-123456789</xsl:element>
</xsl:template>
The template for the "Id" element is actioned while the one for the
"Id/@extension" attribute never is; unsure why the xslt processor never
'runs' the template for the
So what am I doing wrong??? Is there a better/proper way to populate
both the element and its attribute?
Would aoppreciate any suggestions! :-)
Regards,
Bilal B.
*** Sent via Developersdex http://www.developersdex.com ***