views:

52

answers:

1

Could someone tell me why this is happening, please?

My XML is:

<?xml version="1.0" encoding="utf-8" ?>
<example xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="XMLCompositeQuoteswithSelect.xsd">
    <title>some text goes here</title>
    <atopelem>a top elem</atopelem>
    <date>today</date>
    <anode anodeattr="an attribute">
        <asubnode>a subnode</asubnode>
        <somemorecontent>more content</somemorecontent>
    </anode>
    <anode anodeattr="another attribute">
        <asubnode>another subnode</asubnode>
        <somemorecontent>even more content</somemorecontent>
    </anode>
</example>

My XSL is:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="XMLCompositeQuoteswithSelect.xsd"
exclude-result-prefixes="msxsl xsl xsi xmlns">

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

<xsl:template match="/">
    <exampleelems>
        <xsl:copy-of select="*/title | */atopelem | */date" />
        <xsl:for-each select="*/anode">
            <xsl:element name="node">
                <xsl:element name="anodeattr">
                    <xsl:value-of select="@anodeattr"/>
                </xsl:element>
                <xsl:apply-templates />
            </xsl:element>
        </xsl:for-each>
    </exampleelems>
</xsl:template>

<xsl:template match="/anode/*" >
    <xsl:copy >
        <xsl:apply-templates />
    </xsl:copy>
</xsl:template>

<xsl:template match="node()|@*" >
    <xsl:copy />
</xsl:template>
</xsl:stylesheet>

My output XML is:

<?xml version="1.0" encoding="utf-16"?>
<exampleelems>
    <title xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;some text goes here</title>
    <atopelem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;a top elem</atopelem>
    <date xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;today&lt;/date&gt;
    <node>
        <anodeattr>an attribute</anodeattr>
        <asubnode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
        <somemorecontent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
    </node>
    <node>
        <anodeattr>another attribute</anodeattr>
        <asubnode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
        <somemorecontent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
    </node>
</exampleelems>

And the code which executes this (slightly trimmed) is:

    // create the xsl transformer
    XslCompiledTransform t = new XslCompiledTransform(true);
    t.Load(reader);

    // create the writer which will output the transformed xml
    StringBuilder sb = new StringBuilder();
    //XmlWriterSettings tt = new XmlWriterSettings();
    //tt.Encoding = new UTF8Encoding(false);
    XmlWriter results = XmlWriter.Create(new StringWriter(sb));//, tt);

    // write the transformed xml out to a stringbuilder
    t.Transform(input, null, results);

As you can probably guess, I really don't want the xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributes being copied to every child element, but I don't know how to stop it.

Thanks for any and all help and info,

Matt.

+1  A: 

You seem to want to transform the name of the root element but copy its attribute so all you want is e.g.

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

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

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

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

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

  <xsl:template match="anode/@anodeattr">
    <xsl:element name="{name()}">
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>
Martin Honnen
Thank you, that's great and I'm working with it now on my real XML. One extension question: How do you say "at this level, this element but no others". I think I may not understand templates as well as I thought.
Matt W
Alejandro
Matt, I think it is better you explain in a new question in more detail what you want to achieve with "at this level, this element but no others".If you want to select only a certain child element (named "foo") for processing the simply do e.g. `<xsl:apply-templates select="foo"/>` instead of `<xsl:apply-templates/>`.
Martin Honnen