1

Here is the root of my XML (There is more to it but here is the basis):

<?xml version="1.0" encoding="UTF-8"?><section xmlns="http://quark.com/smartcontent/2.0" id="_63f6a0c9-bac8-4a15-854c-03d80bd46b64" type="clientFactsheet">

Here is my XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:deltaxml="http://www.deltaxml.com/ns/well-formed-delta-v1" version="2.0">
   <xsl:output method="xml" indent="yes"/>
   <xsl:template match="*|@*">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="section[@id]">
      <xsl:copy>
         <xsl:attribute name="deltaxml:key" select="@id"/>
            <xsl:apply-templates select="@*, node()"/>
      </xsl:copy>
  </xsl:template>

Here is the output XML root:

<?xml version="1.0" encoding="UTF-8"?><section xmlns="http://quark.com/smartcontent/2.0" id="_63f6a0c9-bac8-4a15-854c-03d80bd46b64" type="clientFactsheet">

Here is what I get if I take off the default namespace from the input XML (which is desired result, except that I need to keep the default namespace):

<?xml version="1.0" encoding="UTF-8"?><section xmlns:deltaxml="http://www.deltaxml.com/ns/well-formed-delta-v1" deltaxml:key="_63f6a0c9-bac8-4a15-854c-03d80bd46b64" id="_63f6a0c9-bac8-4a15-854c-03d80bd46b64" type="clientFactsheet">

Basically, I need to be able to add the "deltaxml:key" attributes with the "id" value of the section so for this I need to add the namespace "deltaxml" but I also need to keep the default namespace (which can change from document to document which I won't have control over... so I can't just copy it over). How can I accomplish this?

1 Answer 1

1

I also need to keep the default namespace (which can change from document to document

It is a rare situation where the namespace of the incoming XML is unknown in advance. This puts you at a disadvantage, since trying to match/select a node by its local name only may easily result in matching/selecting other nodes with the same local name, but in different namespaces/s.

If you cannot avoid it, try something along these lines:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:deltaxml="http://www.deltaxml.com/ns/well-formed-delta-v1">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="*:section[@id]">
    <xsl:copy>
        <xsl:attribute name="deltaxml:key" select="@id"/>
        <xsl:apply-templates select="@*, node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Applied to the following input example:

XML

<section xmlns="http://quark.com/smartcontent/2.0" 
         id="_63f6a0c9-bac8-4a15-854c-03d80bd46b64" 
         type="clientFactsheet">
<!-- content -->
</section>

the result will be:

<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://quark.com/smartcontent/2.0"
         xmlns:deltaxml="http://www.deltaxml.com/ns/well-formed-delta-v1"
         deltaxml:key="_63f6a0c9-bac8-4a15-854c-03d80bd46b64"
         id="_63f6a0c9-bac8-4a15-854c-03d80bd46b64"
         type="clientFactsheet"><!-- content --></section>
Sign up to request clarification or add additional context in comments.

Comments

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.