-1

I saw the post XSLT : Add a namespace declaration to the root element

The @StuartLC answer works. I need help ... in the example @schglurps... ¿How would you add a new namespace to a non-root node?

The input XML document:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="AcquisitionFolder">
            <Directory Id="dir2EE87E668A6861A2C8B6528214144568" Name="bin" />
            <Directory Id="dir99C9EB95694B90A2CD31AD7E2F4BF7F6" Name="Decoders" />
        </DirectoryRef>
    </Fragment>
</Wix>

And I'd want to obtain:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Fragment>
        <DirectoryRef Id="AcquisitionFolder" xmlns:ns1="http://prerk">
            <Directory Id="dir2EE87E668A6861A2C8B6528214144568" Name="bin" />
            <Directory Id="dir99C9EB95694B90A2CD31AD7E2F4BF7F6" Name="Decoders" />
            <ns1:dir>prueba</ns1:dir>
        </DirectoryRef>
    </Fragment>
</Wix>

A new namespace in the node DirectoryRef (eg) xmlns:ns1="http://prerk", it is a non root and copies all the same nodes.

I tried, but I couldn't find any suitable solution.

Could you please advise me?

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
Blas
  • 3
  • 4
  • Are you sure that's the result you want? What you show is not only changing the namespace of `DirectoryRef`. It is also removing the inherited namespace of its `Directory ` children. – michael.hor257k Sep 26 '19 at 13:12
  • @michael.hor257k: `DirectoryRef` remains in the default namespace despite the namespace prefix declaration appearing there, but you're right to question whether OP really has to have that declaration on `DirectoryRef`. It can appear where used on the `dir` element or anywhere in the `dir` element's ancestry. – kjhughes Sep 26 '19 at 13:18
  • 1
    @kjhughes You are right. – michael.hor257k Sep 26 '19 at 13:35
  • @michael.hor257k you're rigth in that declaration site, i was wrong. The was in the imput. I need to add in the DirectoryRef because dinamically i need to add prefix to some nodes, eg and declare in the top node.. I think i have to edit the post – Blas Sep 27 '19 at 09:27
  • @kjhughes thanks i was wrong in the xml imput, i have to add in the DirectoryRef because dinamically i need to add prefix to some nodes, eg and declare in the top node. Im going to edit the post with my imput example. Please if you are help me – Blas Sep 27 '19 at 09:34
  • @BlasAM I have rolled back your question to what it was when I answered it. Please start a new question and post your significantly different problem there. – michael.hor257k Sep 27 '19 at 12:37

1 Answers1

1

You could get the expected result using:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"
exclude-result-prefixes="wix">
<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="wix:DirectoryRef">
     <DirectoryRef xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:ns1="http://prerk">
        <xsl:apply-templates select="@*|node()"/>
         <ns1:dir>prueba</ns1:dir>
    </DirectoryRef>
</xsl:template>

</xsl:stylesheet>

Though it might be simpler to do just:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"
exclude-result-prefixes="wix">
<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="wix:DirectoryRef">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <ns1:dir xmlns:ns1="http://prerk">prueba</ns1:dir>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

to get a semantically identical result:

<?xml version="1.0" encoding="utf-16"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Fragment>
    <DirectoryRef Id="AcquisitionFolder">
      <Directory Id="dir2EE87E668A6861A2C8B6528214144568" Name="bin" />
      <Directory Id="dir99C9EB95694B90A2CD31AD7E2F4BF7F6" Name="Decoders" />
      <ns1:dir xmlns:ns1="http://prerk">prueba</ns1:dir>
    </DirectoryRef>
  </Fragment>
</Wix>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Work it, but the namespace ns1 is added in the node, not in the top – Blas Sep 27 '19 at 09:30
  • 1
    @BlasAM I believe that with the 1st stylesheet the namespace declaration will be added to the `DirectoryRef` start-tag - where you said you wanted it to be. However, this may depend on your processor. In general, such namespace declarations can be anywhere on the `ancestor-or-self` axis, and you shouldn't care where exactly your processor decides to put it. – michael.hor257k Sep 27 '19 at 09:54
  • yes, you're right ... I edited the post with my problem :). Thanks – Blas Sep 27 '19 at 12:23