0
votes

Here my XSL for the transformation.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> 
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template> 
<xsl:template match="EL3">
    <te>ABC</te>
</xsl:template> 

This is the Source-XML what I want to transform.

<EL1 xmlns="http://anyurl.com" language="2">
<EL2>
    <set1>
        <value1>12</value1>
        <value2>34</value2>
        <value3>45</value3>         
    </set1>
</EL2>
<EL2>
    <set1>
        <value1>AB</value1>
        <value2>CD</value2>
        <value3>EF</value3>
        <EL3>
            <value1>AB</value1>
            <value2>CD</value2>
            <value3>EF</value3>
        </EL3>
    </set1>
</EL2>

This is the Target-XML after transforming.

<EL1 xmlns="http://anyurl.com" language="2">
<EL2>
    <set1>
        <value1>12</value1>
        <value2>34</value2>
        <value3>45</value3>         
    </set1>
</EL2>
<EL2>
    <set1>
        <value1>AB</value1>
        <value2>CD</value2>
        <value3>EF</value3>
        <EL3>
            <value1>AB</value1>
            <value2>CD</value2>
            <value3>EF</value3>
        </EL3>
    </set1>
</EL2>

The matching do not work in relation to the namespace. If I erase the xmlns="http://anyurl.com" from the Source-XML I get the result what I want. The problem is that I get the Source-XML from a external System and I can't change the Source-XML before. How I can edit the XSL for my awaiting result like this?

<EL1 language="2">
   <EL2>
      <set1>
         <value1>12</value1>
         <value2>34</value2>
         <value3>45</value3>
      </set1>
   </EL2>
   <EL2>
      <set1>
         <value1>AB</value1>
         <value2>CD</value2>
         <value3>EF</value3>
         <te>ABC</te>
      </set1>
   </EL2>
</EL1>
1

1 Answers

0
votes

Assuming an XSLT 2.0 processor like Saxon 9 or XmlPrime you only need

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://anyurl.com">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> 
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template> 
<xsl:template match="EL3">
    <te>ABC</te>
</xsl:template>

If you use an XSLT 1.0 processor you need

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:df="http://anyurl.com" exclude-result-prefixes="df">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> 
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template> 
<xsl:template match="df:EL3">
    <te>ABC</te>
</xsl:template>

That should do for the matching, but Carlos solved another problem, namely creating the new element in the right namespace, so you need

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://anyurl.com" xmlns="http://anyurl.com">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> 
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template> 
<xsl:template match="EL3">
    <te>ABC</te>
</xsl:template>

respectively

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:df="http://anyurl.com" exclude-result-prefixes="df" xmlns="http://anyurl.com">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> 
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template> 
<xsl:template match="df:EL3">
    <te>ABC</te>
</xsl:template>