0

When running the below XSLT

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xquery="http://www.w3.org/2005/xpath-functions"
    xmlns:upd="http://www.w3.org/2007/xquery-update-10">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <xsl:for-each select="document('f1.xml')">
            <xsl:variable name="doc" select="."/>
            <xsl:variable name="newFileName" select="concat(substring-before(document-uri($doc), '.xml'), '_chg.xml')" />
            <xsl:result-document href="{$newFileName}">
                <xsl:apply-templates select="$doc/*"/>
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>

<!-- Template to match the root element -->
<xsl:template match="/">
    <xsl:apply-templates select="*"/>
</xsl:template>

<!-- Identity template to copy all nodes and attributes -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <!-- Copy attributes first -->
        <xsl:apply-templates select="@*"/>
        <!-- Then copy child nodes -->
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

The following error is produced.

XTDE0410: Cannot add attribute node to an element node that already has non attribute or namespace children

Why, the attributes are clearly copied before the nodes in the "copy" node.

Tried removing the node in the XML with the attributes listed in the error message, no change, just errors on the next node. Tried removing the iterations through the list of files, no change. Tried combining the "@*" and "node()" on the apply-templates

Sample XML

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="file:///F:/temp%20(F)/2025-06-24a_debug/DataPower_Control_File_cleanup_actions_V3.xslt"?>
<registeredServices>
    <domain name="entsvcs" description="Enterprise Services">
        <service name="commonlogging" version="1.0" type="SOAP" endpoint="http://localhost:3016" sldpolicy="default_sld_policy">
            <registeredConsumers>
                <!--
                <consumer><consumerId></consumerId><consumerName></consumerName><clientName></clientName><slaPolicy>bcbst_default_sla_policy</slaPolicy><logLevel>0</logLevel>
                -->
                <!-- 0 = ALL, 1=DEBUG, 2=INFO, 3=WARN, 4=ERROR, 5=FATAL, 6=OFF -->
                <!--
                </consumer>
                -->
                <consumer>
                    <consumerId>bcbstesvcs</consumerId>
                    <consumerName>BCBST</consumerName>
                    <clientName>Enterprise Services</clientName>
                    <slaPolicy>bcbst_default_sla_policy</slaPolicy>
                    <logLevel>1</logLevel>
                </consumer>
            </registeredConsumers>
        </service>
    </domain>
</registeredServices>
4
  • 1
    Do you realise you have <xsl:template match="/"> twice? Which template do you expect to be used? Also o post a minimal input that allows us to reproduce the error with the code shown. Commented Jun 24 at 16:49
  • Sample xml file added to question. Commented Jun 24 at 18:29
  • Removal of the node with the comment<!-- Template to match the root element --> had not effect. Commented Jun 24 at 18:36
  • I found the issue. Removed all the spaces between tags and let XMLSPY reformat the XSLT and XML. My guess is that there were some white space hex values. Commented Jun 24 at 19:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.