[xsl] RE: unwanted and non prefix namespaces in output

Subject: [xsl] RE: unwanted and non prefix namespaces in output
From: "Joel Friedman" <jfriedman@xxxxxxxxxxxx>
Date: Sat, 31 Jul 2004 02:08:31 -0400
I am very new to xslt (approx 4 hours) but I have managed to (mostly) do
the simple task I am trying to perform.

I am attempting to take an xml file in this format (greatly shortened
for simplicity):

<?xml version="1.0" encoding="utf-16"?>
<sequence version="1" description="foo" command="bar"
xmlns="http://schemas.microsoft.com/ads/2003/sequence";>

  <sequence version="1" description="Image" command="WIN2000"
xmlns="http://schemas.microsoft.com/ads/2003/sequence";>
    <task>
      <command>foo</command>
      <parameters>
        <parameter>bar</parameter>
      </parameters>
    </task>
   <task>
    ...
   </task
  </sequence>

  <sequence version="1" description="Patch" command="WIN2000"
xmlns="http://schemas.microsoft.com/ads/2003/sequence";>
    <task>
      <command>foo</command>
      <parameters>
        <parameter>bar</parameter>
      </parameters>
    </task>
    <task>
     ...
    </task
  </sequence>

  <sequence version="1" description="Patch" command="WIN2K3"
xmlns="http://schemas.microsoft.com/ads/2003/sequence";>
    <task>
      <command>foo</command>
      <parameters>
        <parameter>bar</parameter>
      </parameters>
    </task>
   <task>
    ...
   </task
  </sequence>

 <sequence version="1" description="foo" command="bar"
xmlns="http://schemas.microsoft.com/ads/2003/sequence";>
   ...
 </sequence>

  ...

</sequence>


And apply this XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";

xmlns:ads="http://schemas.microsoft.com/ads/2003/sequence";
                version="1.0">

  <xsl:output method="xml" indent="yes"/>
  <xsl:variable name="osName"
select="ads:sequence/ads:sequence/@command"/>

  <xsl:template match="/">
    <xsl:apply-templates select="ads:sequence"/>
  </xsl:template>

  <xsl:template match="ads:sequence">
    <xsl:choose>
      <xsl:when test="(@description='Patch')">
        <xsl:if test="@command=$osName">
          <sequence>
            <xsl:apply-templates select="@*|node()"/>
          </sequence>
          </xsl:if>
      </xsl:when>
      <xsl:otherwise>
        <sequence>
          <xsl:apply-templates select="@*|node()"/>
        </sequence>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

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

</xsl:stylesheet>


Basically I'm trying to fetch the OS Name from the command attribute of
the second sequence (fixed location)  Then the if OS matches the command
attribute for the sequence with a description of 'Patch', copy it over.
Also copy over everything else that isn't a Patch sequence.  So
basically my output should be identical to my input minus the Patch
sequence where the OS does not match.  My problem enlies that every
'task ' element has the namespace added to it but the sequence element
does not.

..sample output:

<?xml version="1.0" encoding="UTF-16"?>
<sequence version="1" description="foo" command="bar">

 <sequence version="1" description="Image" command="WIN2000">
  <task  xmlns="http://schemas.microsoft.com/ads/2003/sequence";>
   <command>foo</command>
   <parameters>
    <parameter>bar</parameter>
   </parameters>
  </task>


The exclude-result-prefixes="ads" squashed my namespace on the root
sequence element (which I don't want).  I need the namespace to only be
defined on all sequence blocks.  Just like the input.  The logic part of
not copying over the block I do not need does work.  It's probably not
the best way, but it is functional.  Forgive me if I did not use the
correct XSL grammar while trying to explain my problem.

Anybody have an idea of a direction I should look in for a solution?

Thanks in advance,

--Joel

________________________________

From: Joel Friedman
Sent: Saturday, July 31, 2004 2:07 AM
To: 'xsl-list@xxxxxxxxxxxxxxxxxxxxxx'
Subject: unwanted and non prefix namespaces in output


I am very new to xslt (approx 4 hours) but I have managed to (mostly) do
the simple task I am trying to perform.

I am attempting to take an xml file in this format (greatly shortened
for simplicity):

<?xml version="1.0" encoding="utf-16"?>
<sequence version="1" description="foo" command="bar"
xmlns="http://schemas.microsoft.com/ads/2003/sequence";>

  <sequence version="1" description="Image" command="WIN2000"
xmlns="http://schemas.microsoft.com/ads/2003/sequence";>
    <task>
      <command>foo</command>
      <parameters>
        <parameter>bar</parameter>
      </parameters>
    </task>
   <task>
    ...
   </task
  </sequence>

  <sequence version="1" description="Patch" command="WIN2000"
xmlns="http://schemas.microsoft.com/ads/2003/sequence";>
    <task>
      <command>foo</command>
      <parameters>
        <parameter>bar</parameter>
      </parameters>
    </task>
    <task>
     ...
    </task
  </sequence>

  <sequence version="1" description="Patch" command="WIN2K3"
xmlns="http://schemas.microsoft.com/ads/2003/sequence";>
    <task>
      <command>foo</command>
      <parameters>
        <parameter>bar</parameter>
      </parameters>
    </task>
   <task>
    ...
   </task
  </sequence>

 <sequence version="1" description="foo" command="bar"
xmlns="http://schemas.microsoft.com/ads/2003/sequence";>
   ...
 </sequence>

  ...

</sequence>


And apply this XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";

xmlns:ads="http://schemas.microsoft.com/ads/2003/sequence";
                version="1.0">

  <xsl:output method="xml" indent="yes"/>
  <xsl:variable name="osName"
select="ads:sequence/ads:sequence/@command"/>

  <xsl:template match="/">
    <xsl:apply-templates select="ads:sequence"/>
  </xsl:template>

  <xsl:template match="ads:sequence">
    <xsl:choose>
      <xsl:when test="(@description='Patch')">
        <xsl:if test="@command=$osName">
          <sequence>
            <xsl:apply-templates select="@*|node()"/>
          </sequence>
          </xsl:if>
      </xsl:when>
      <xsl:otherwise>
        <sequence>
          <xsl:apply-templates select="@*|node()"/>
        </sequence>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

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

</xsl:stylesheet>


Basically I'm trying to fetch the OS Name from the command attribute of
the second sequence (fixed location)  Then the if OS matches the command
attribute for the sequence with a description of 'Patch', copy it over.
Also copy over everything else that isn't a Patch sequence.  So
basically my output should be identical to my input minus the Patch
sequence where the OS does not match.  My problem enlies that every
'task ' element has the namespace added to it but the sequence element
does not.

..sample output:

<?xml version="1.0" encoding="UTF-16"?>
<sequence version="1" description="foo" command="bar">

 <sequence version="1" description="Image" command="WIN2000">
  <task  xmlns="http://schemas.microsoft.com/ads/2003/sequence";>
   <command>foo</command>
   <parameters>
    <parameter>bar</parameter>
   </parameters>
  </task>


The exclude-result-prefixes="ads" squashed my namespace on the root
sequence element (which I don't want).  I need the namespace to only be
defined on all sequence blocks.  Just like the input.  The logic part of
not copying over the block I do not need does work.  It's probably not
the best way, but it is functional.  Forgive me if I did not use the
correct XSL grammar while trying to explain my problem.

Anybody have an idea of a direction I should look in for a solution?

Thanks in advance,

--Joel

Current Thread