开发者

Remove an attribute in Xslt

I'm working on XSLT.

I have this XML source file :

<?xml version="1.0" encoding="UTF-8"?>
<Magasins>
  <Magasin Nom="Name" CodeRouteur="TE">
    <Client IdClient="1" ComplementCodeRouteur="A" Name="YYY"><Elem /></Client>
    <Client IdClient="2" ComplementCodeRouteur="B" Name="XXX"><Elem /></Client>
  </Magasin>
  <Magasin Nom="Name2" CodeRouteur="TE">
    <Client IdClient="3" ComplementCodeRouteur="A" Name="YYY"><Elem /></Client>
  </Magasin>
</Magasins>

This XSL file :

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="Magasins">
    <Magasins xmlns:xi="http://www.w3.org/2001/XInclude" Id="{@Id}">
      <xsl:apply-templates/>
    </Magasins>
  </xsl:template>

  <xsl:key name="kClientGroup" match="Client"
      use="concat(../@CodeRouteur, @ComplementCodeRouteur)"
        />

  <xsl:template match="Magasin">
<xsl:apply-templates select="Client[generate-id() 
        =
        generate-id(key('kClientGroup', 
        concat(../@CodeRouteur, @ComplementCodeRouteur))[1])]"
        />
  </xsl:template>

  <xsl:template match="Client">
    <Magasin
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        CodeRouteur="{concat(../@CodeRouteur,@ComplementCodeRouteur)}"
        Nom="{../@Nom}">

      <xsl:apply-templates select="key('kClientGroup', 
                concat(../@CodeRouteur,@ComplementCodeRouteur))" mode="copy"/>

    </Magasin>
  </xsl:template>

  <xsl:template match="Client" mode="copy">
    <xsl:copy>
      <xsl:copy-of select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

And I have this result :

<?xml version="1.0" encoding="UTF-8"?>
<Magasins>
  <Magasin Nom="Name" CodeRouteur="TEA">
    <Client IdClient="1" ComplementCodeRouteur="A" Name="YYY"><Elem /></Client>
  </Magasin>
  <Magasin Nom="Name" CodeRouteur="TEA">
    <Client IdClient="2" ComplementCodeRouteur="B" Name="XXX"><Elem /></Client>
  </Magasin>
  <Magasin Nom="Name2" CodeRouteur="TEA">
    <Client IdClient="3" ComplementCodeRouteur="A" Name="YYY"><Elem /></Client>
  </Magasin>
</Magasins>

I want to remove the attribute ComplementCodeRouteur in the Client element. Does someone know how to do this.

Miscallenous question, I'd like to simplify the copy of the Magasin element. Now, each attribute is copied. Is it possible to make a "copy all attribute but CodeRouteur is defined manually"

PS : I removed a lot of attribute to improve the readability. There may be some dead attribute.

PS 2 : I can only use the .net implementation in .Net (XSLT 1.0)

Edit :

I have a good solution but I've another need.

  <xsl:template match="Client">
    <Magasin
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        CodeRouteur="{concat(../@CodeRouteur,@ComplementCodeRouteur)}"
        Nom="{../@Nom}">

      <xsl:apply-templates select="key('kClientGroup', 
                concat(../@CodeRouteur,@ComplementCodeRouteur))" mode="copy"/>

    </Magasin>
  </xsl:template>

I would like to copy all Magasin attribute but not the Cod开发者_如何学编程eRouteur attribute which will be defined manually. You can also notice that the Magasin element is created in a template match="Client" (and not in a template match="Magasin") because the CodeRouteur attribute is defined with information contained in the Client element.

Edit 2:

I found another bug

My current XSLT is :

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="Magasins">
    <Magasins xmlns:xi="http://www.w3.org/2001/XInclude" Id="{@Id}">
      <xsl:apply-templates/>
    </Magasins>
  </xsl:template>

  <xsl:key name="kClientGroup" match="Client"
      use="concat(../@CodeRouteur, @ComplementCodeRouteur)"
        />

  <xsl:template match="Magasin">
<xsl:apply-templates select="Client[generate-id() 
        =
        generate-id(key('kClientGroup', 
        concat(../@CodeRouteur, @ComplementCodeRouteur))[1])]"
        />
  </xsl:template>

  <xsl:template match="Client">
    <Magasin
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        CodeRouteur="{concat(../@CodeRouteur,@ComplementCodeRouteur)}">

      <xsl:copy-of select="../@*[name() != 'CodeRouteur']"/>

      <xsl:apply-templates select="key('kClientGroup', 
                concat(../@CodeRouteur,@ComplementCodeRouteur))" mode="copy"/>

    </Magasin>
  </xsl:template>

  <xsl:template match="Client" mode="copy">
    <xsl:copy>
      <xsl:copy-of select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

When I have 2

Ex :

<?xml version="1.0" encoding="UTF-8"?>
<Magasins>
  <Magasin Nom="Name" CodeRouteur="TE">
    <Client IdClient="1" ComplementCodeRouteur="A" Name="YYY"><Elem /></Client>
    <Client IdClient="2" ComplementCodeRouteur="B" Name="XXX"><Elem /></Client>
  </Magasin>
  <Magasin Nom="Name2" CodeRouteur="TE">
    <Client IdClient="3" ComplementCodeRouteur="A" Name="YYY"><Elem /></Client>
  </Magasin>
</Magasins>

will give :

<?xml version="1.0" encoding="UTF-8"?>
<Magasins>
  <Magasin Nom="Name" CodeRouteur="TEA">
    <Client IdClient="1" ComplementCodeRouteur="A" Name="YYY"><Elem /></Client>
    <Client IdClient="3" ComplementCodeRouteur="A" Name="YYY"><Elem /></Client>
  </Magasin>
  <Magasin Nom="Name2" CodeRouteur="TEB">
    <Client IdClient="2" ComplementCodeRouteur="B" Name="XXX"><Elem /></Client>
  </Magasin>
</Magasins>

and I would like :

<?xml version="1.0" encoding="UTF-8"?>
<Magasins>
  <Magasin Nom="Name" CodeRouteur="TEA">
    <Client IdClient="1" ComplementCodeRouteur="A" Name="YYY"><Elem /></Client>
  </Magasin>
  <Magasin Nom="Name" CodeRouteur="TEB">
    <Client IdClient="2" ComplementCodeRouteur="B" Name="XXX"><Elem /></Client>
  </Magasin>
  <Magasin Nom="Name2" CodeRouteur="TEA">
    <Client IdClient="3" ComplementCodeRouteur="A" Name="YYY"><Elem /></Client>
  </Magasin>
</Magasins>

How can I modify the XSLT file. I want to have one Magasin element if one that information change : Magasin, CodeRouteur, ComplementCodeRouteur


<xsl:template match="Client" mode="copy">
    <xsl:copy>
        <xsl:copy-of select="node() | @*[not(name() = 'ComplementCodeRouteur')]"/>
    </xsl:copy>
</xsl:template>

Updated:

<xsl:template match="Client">
        <Magasin
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
         CodeRouteur="{concat(../@CodeRouteur,@ComplementCodeRouteur)}"
            >

            <xsl:copy-of select="../@*[name() != 'CodeRouteur']"/>

            <xsl:apply-templates select="key('kClientGroup', 
                concat(../@CodeRouteur,@ComplementCodeRouteur))" mode="copy"/>

        </Magasin>
    </xsl:template>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜