草庐IT

c# - 删除 Xslt 中的属性

coder 2024-07-04 原文

我正在研究 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 属性。 有人知道怎么做吗。

杂项问题,我想简化 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

例如:

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

最佳答案

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

关于c# - 删除 Xslt 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7090014/

有关c# - 删除 Xslt 中的属性的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  3. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  4. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  5. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  6. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  7. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  8. ruby - 我可以使用 Ruby 从 CSV 中删除列吗? - 2

    查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html

  9. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  10. ruby-on-rails - 在混合/模块中覆盖模型的属性访问器 - 2

    我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah

随机推荐