删除Xslt中的属性

时间:2011-08-17 08:45:30

标签: c# .net xml xslt xpath

我正在研究XSLT。

我有这个XML源文件:

<?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>

此XSL文件:

<?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>

我有这个结果:

<?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>

我想删除Client元素中的ComplementCodeRouteur属性。 有人知道怎么做。

Miscallenous问题,我想简化Magasin元素的副本。 现在,每个属性都被复制。 是否可以制作“复制所有属性,但手动定义CodeRouteur”

PS:我删除了很多属性以提高可读性。可能有一些死属性。

PS 2:我只能在.Net(XSLT 1.0)

中使用.net实现

修改:

我有一个很好的解决方案,但我还有其他需要。

  <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>

我想复制所有Magasin属性,但不复制将手动定义的CodeRouteur属性。您还可以注意到,Magasin元素是在模板match =“Client”中创建的(而不是在模板match =“Magasin”中),因为CodeRouteur属性是使用Client元素中包含的信息定义的。

编辑2:

我发现了另一个错误

我目前的XSLT是:

<?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>

当我有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>

会给:

<?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>

我希望:

<?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>

如何修改XSLT文件。 如果信息发生变化,我想要一个Magasin元素:Magasin,CodeRouteur,ComplementCodeRouteur

1 个答案:

答案 0 :(得分:5)

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