tags:

views:

34

answers:

2

Hallo, first of all, i am not very familiar when it comes to xml and similiar, so please dont punish me with my beginner question :D

I have a xml file looking like this :

<?xml version="1.0" encoding="utf-8" ?>
<mainstuff>
    <category_major>
     <project_name>Dream</project_name>
     <project_attribute>Version 1.0</project_attribute>
     <category_A></category_A>
     <category_B></category_B>
     <category_C></category_C>
    </category_major>
</mainstuff>

Then i got a XSLT File looking like this :

<?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" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="/">
    <xsl:element name="mainstuff">
      <xsl:attribute name="version">1.0</xsl:attribute>
      <xsl:apply-templates/>
    </xsl:element>    
  </xsl:template>

  <xsl:template match="category_major">
    <xsl:element name="category_major">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

    <xsl:template match="category_A">
    <xsl:element name="category_A">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

    <xsl:template match="category_B">
    <xsl:element name="category_B">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

    <xsl:template match="category_C">
    <xsl:element name="category_C">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

I want to avoid the two parameters "project_name" and "project_attribute". I want a result like this :

<?xml version="1.0" encoding="utf-8" ?>
<mainstuff>
    <category_major>
     <category_A></category_A>
     <category_B></category_B>
     <category_C></category_C>
    </category_major>
</mainstuff>

But what i get is this after tranformation :

<?xml version="1.0" encoding="utf-8"?>
<mainstuff version="1.0">
    <category_major>
     **Dream
     Version 1.0**
     <category_A />
     <category_B />
     <category_C />
    </category_major>
</mainstuff>

The text is still in it. How can i fix it. What am I doing wrong ? And how can I realise to get Parameters but without the text within it ? In my example an output like this :

<?xml version="1.0" encoding="utf-8" ?>
<mainstuff>
    <category_major>
     **<project_name></project_name>
     <project_attribute></project_attribute>**
     <category_A></category_A>
     <category_B></category_B>
     <category_C></category_C>
    </category_major>
</mainstuff>

Thanks for your help :D

+2  A: 

If there is no template matching an element, a default template gets used. The effect of the default template is effectively to output the string value of the node - for an element, it looks like a concatenation of all descendant text nodes.

If you want to override this behavior, you need to provide your own no-op templates for the elements you wish to skip:

<xsl:template match="project_name | project_attribute" />

For your second request, if you want to output the element, but strip all contents, you can use xsl:copy:

<xsl:template match="project_name | project_attribute">
  <xsl:copy />
</xsl:template>

Note that xsl:copy only copies the element; it does not copy its attributes, nor its children.

Pavel Minaev
Thanks works great :D
Camal
A: 

Try invert it, match what you want to exclude, not what you want to include:

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

<xsl:template match="*" priority="0">
     <xsl:element name="{local-name()}">
         <xsl:apply-templates />
     </xsl:element>
</xsl:template>

<xsl:template match="project_name" priority="1"></xsl:template>
<xsl:template match="project_attribute" priority="1"></xsl:template>

Sorry if that is a little cryptic, but I hope it helps

LorenVS
The identity transform (which is what you seem to be trying to use for `match="*"`) is better written using `xsl:copy`. Also, there's no need for explicit priorities - the priority of a template matching `*` will always be lower than the priority of a template matching a named element.
Pavel Minaev
priorities are for readability of what gets override, I always include priority="0" on templates meant to be overriden, personal styleand no, xsl:copy won't apply templates on an elements children, which is required for this solution to work
LorenVS
You can use `<xsl:apply-templates/>` (or any other constructor) within `<xsl:copy/>`.
Pavel Minaev