0

I am trying to transform an xml file that contain a list of words and I am trying to exclude some elements from the resulting document, more concretely and

My List is as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="merge.xsl"?>

    <dic:englishWords xmlns:dic = "dictionary">
        <dic:words>
                <dic:englishWords xmlns:dic = "dictionary">
    <dic:title>
    English Dictionary
    </dic:title>
    <dic:author>
        <dic:authorsName>
            Author:
        <dic:name>
            User
        </dic:name>
        <dic:lastName>
            Name
        </dic:lastName> 
        </dic:authorsName>
    </dic:author>
    <dic:words>
            <dic:name>Water</dic:name><br/>
            <dic:name>Room</dic:name><br/>
            <dic:name>Computer</dic:name><br/>
            <dic:name>Book</dic:name><br/>
            <dic:name>Garage</dic:name><br/>
            <dic:name>Car</dic:name><br/>
            <dic:name>Ship</dic:name><br/>
            <dic:name>Food</dic:name><br/>
            <dic:name>Coffee</dic:name><br/>
            <dic:name>Program</dic:name><br/>
    </dic:words>
</dic:englishWords>

The path to the list of words is contained in an xml file as follows:

<dic:dictionary xmlns:dic = "dictionary">
    <dic:Logo>Logo</dic:Logo>
    <dic:Author>User Name</dic:Author>
    <dic:EnglishWords>english</dic:EnglishWords>
    <dic:SwedishTranslation>swedish</dic:SwedishTranslation>
    <dic:SwedishWords>swedish</dic:SwedishWords>
    <dic:EnglishTranslation>english</dic:EnglishTranslation>
</dic:dictionary>

my transformation is as follows

    <!--Declare a parameter with the nodes to be removed-->
   <xsl:param name="removeElementsNamed" select="'|dic:author|dic:title'"/>

    <!--create a template and call it remove node-->
    <xsl:template match="node()|@*" name="removeNode">
          <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
          </xsl:copy>
    </xsl:template>

    <!--remove the actual nodes-->
    <xsl:template match="*">
          <xsl:if test="not(contains($removeElementsNamed, concat('|',name(),'|')))">
            <xsl:call-template name="removeNode"/>
          </xsl:if>
    </xsl:template>

I am trying to follow an example which I have found here:

how to exclude elements

...but in my case it does not work.

Any help will be appreciated...

Bluetxxth

Community
  • 1
  • 1
bluetxxth
  • 121
  • 2
  • 16

2 Answers2

0

You could match the element you don't want but not output anything.

    <xsl:template match="nodeToMatch" />
Chris Bentley
  • 1,945
  • 18
  • 23
  • Do you mean to remove all the code in the transformation above and instead have that for every node I which I want to delete? – bluetxxth Jan 22 '13 at 00:47
  • I have also tried this ` ` but still does now work... I am not sure what am I doing wrong.... – bluetxxth Jan 22 '13 at 01:07
  • @user1179105 I mean add a new empty template that matches the node you want to exclude but outputs nothing. – Chris Bentley Jan 22 '13 at 01:19
  • I see that is exactly what you wrote... if I create an empty template like this `` the node author does not go away... can you tell me what is it that I do wrong? Thank you – bluetxxth Jan 22 '13 at 01:47
  • @user1179105 Are you correctly declaring the `dic` namespace in your XSLT? Could you show us the whole thing? – JLRishe Jan 22 '13 at 02:30
0

So it looks like you're trying to exclude elements based on whether |ELEMENTNAME| is present in $removeElementsNamed, but dic:author is the only item in that list that has pipes on both sides. It might almost work if you did this:

<xsl:param name="removeElementsNamed" select="'|dic:author|dic:title|'"/>

However this is a bit of a hack.

A better approach would to just do something like this:

<xsl:template match="dic:author | dic:title" />

This should exclude dic:author and dic:title from the output.

Another issue is that this template is misnamed:

<xsl:template match="node()|@*" name="removeNode"> 

What this template would actually do, if it worked, would be to include nodes that were sent its way, but a template can't both have a match attribute and a name attribute. I would suggest rewriting your XSLT to be like this and starting from here:

<!--Declare a parameter with the nodes to be removed-->
<xsl:template match="dic:author | dic:title" />

<!--create a template and call it remove node-->
<xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
</xsl:template>
JLRishe
  • 99,490
  • 19
  • 131
  • 169