草庐IT

xml - XSLT - 剪切和粘贴

coder 2024-06-27 原文

如何在xslt中进行剪切和粘贴?假设我的 xml 是这样的,

  <root>
    <header>
      some nodes
    </header>
    <body>
    <p>
      some text
    <link>1</link>
      <note xml:id="c5-note-0001" numbered="no">text</note>
    </p>
    <p>
      some text
      <link>2</link>
      <figure>
        <note xml:id="c5-note-0003">text</note>
      </figure>
      <note xml:id="c5-note-0002">text</note>
    </p>
    <tabular>
      <note xml:id="c5-note-0004" numbered="no">text</note>
    </tabular>
    <p>
      some text
      <link>3</link>
      <notegroup>
        <note xml:id="c5-note-0006">text</note>
      </notegroup>
      <note xml:id="c5-note-0005">text</note>
    </p>
    <p>
      some text
      <link>4</link>
      <note xml:id="c5-note-0007">text</note>
    </p>
    </body>
  </root>

我的预期输出是,

  <root>
    <header>
      some nodes
    </header>
    <body>
      <p>
        some text
        <link>1</link>
        <note xml:id="c5-note-0001" numbered="no">text</note>
      </p>
      <p>
        some text
        <link>2</link>
        <figure>
            <note xml:id="c5-note-0003">text</note>
        </figure>
        <notenum>1</notenum>
      </p>
      <tabular>
        <note xml:id="c5-note-0004" numbered="no">text</note>
      </tabular>
      <p>
        some text
        <link>3</link>
        <notegroup>
          <note xml:id="c5-note-0006">text</note>
        </notegroup>
        <notenum>2</notenum>
      </p>
      <p>
        some text
        <link>4</link>
        <notenum>3</notenum>
      </p>
      <newnote>
        <note xml:id="c5-note-0002">text</note>
        <note xml:id="c5-note-0005">text</note>
        <note xml:id="c5-note-0007">text</note>
      </newnote>
    </body>
  </root>

我需要在 body 标记结束之前创建一个新节点 newnote 并将 note 节点剪切并粘贴到其中,并且需要生成一个 notenum 节点而不是那个 note

我只需要在 p 节点中执行此操作。如果 note 位于 tabularfigurenotegroup 下,则无需执行任何操作。

如果 note 包含类似 numbered="no" 的属性,则无需执行任何操作。

我正在使用以下 xslt(只是为了显示我正在使用的模板匹配),

  <?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 ="figure">
      some operation
    </xsl:template>
    <xsl:template match ="tabular">
      some operation
    </xsl:template>
    <xsl:template match ="notegroup">
      some operation
    </xsl:template>
  </xsl:stylesheet>

提前致谢。

最佳答案

一般来说,在 XSLT 中将“剪切和粘贴”想象成实际的样子是有益的,即“复制和粘贴并删除原始内容”。

i need to create a new node newnote before the end of body tag and cut and paste the note node into that

您要做的是调整 <body>元素,因此它在其原始内容(可能以其他方式修改)之后包含一个新的 <newnote>。元素。因此,为 <body> 添加一个新模板元素:

<xsl:template match="body">
</xsl:template>

如前所述,您想基本上接管原始内容。由于原始内容可能仍由其他模板处理,我们将使用 <xsl:apply-tepmlates>这里:

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

在原始内容之后,您想插入新元素:

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

最后,在这个元素中,您需要所有描述的<note> 的副本元素,可以通过 <xsl:for-each> 实现循环:

<xsl:template match="body">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    <newnote>
        <xsl:for-each select="p/note[not(@numbered = 'no')]">
            <xsl:copy-of select="."/>
        </xsl:for-each>
    </newnote>
</xsl:template>

and need to generate a notenum node instead of that note.

这可以通过替换相应的 <note> 的模板来完成。元素:

<xsl:template match="p/note[not(@numbered = 'no')]">
    <notenum><xsl:value-of select="count(preceding::note[parent::p][not(@numbered = 'no')]) + 1"/></notenum>
</xsl:template>

插入新的 <notenum>元素并使用 <xsl:value-of>命令输出计算值。该值为前面<note>的个数符合您的限制条件的元素加一。

关于xml - XSLT - 剪切和粘贴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11807763/

有关xml - XSLT - 剪切和粘贴的更多相关文章

  1. 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代码修改为

  2. ruby-on-rails - 如何在 Rails 3 中禁用 XML 解析 - 2

    我想禁用HTTP参数的自动XML解析。但我发现命令仅适用于Rails2.x,它们都不适用于3.0:config.action_controller.param_parsers.deleteMime::XML(application.rb)ActionController::Base.param_parsers.deleteMime::XMLRails3.0中的等价物是什么? 最佳答案 根据CVE-2013-0156的最新安全公告你可以将它用于Rails3.0。3.1和3.2ActionDispatch::ParamsParser::

  3. ruby - 如何使用 Nokogiri::XML::Builder 生成动态标签? - 2

    我正在遍历数组中的一组标签名称,我想使用构建器打印每个标签名称,而不是求助于“我认为:builder=Nokogiri::XML::Builder.newdo|xml|fortagintagsxml.tag!tag,somevalendend会这样做,但它只是创建名称为“tag”的标签,并将标签变量作为元素的文本值。有人可以帮忙吗?这个看起来应该比较简单,我刚刚在搜索引擎上找不到答案。我可能没有以正确的方式提问。 最佳答案 尝试以下操作。如果我没记错的话,我添加了一个根节点,因为Nokogiri需要一个。builder=Nokogi

  4. ruby - 如何让 Nokogiri 解析并返回 XML 文档? - 2

    这是一些奇怪的例子:#!/usr/bin/rubyrequire'rubygems'require'open-uri'require'nokogiri'print"withoutread:",Nokogiri(open('http://weblog.rubyonrails.org/')).class,"\n"print"withread:",Nokogiri(open('http://weblog.rubyonrails.org/').read).class,"\n"运行此返回:withoutread:Nokogiri::XML::Documentwithread:Nokogiri::

  5. ruby - 模式加载时出现 Nokogiri::XML::Schema SyntaxError - 2

    我正在尝试加载SAML协议(protocol)架构(具体来说:https://www.oasis-open.org/committees/download.php/3407/oasis-sstc-saml-schema-protocol-1.1.xsd),但在执行此操作之后:schema=Nokogiri::XML::Schema(File.read('saml11_schema.xsd'))我得到这个输出:Nokogiri::XML::SyntaxErrorException:Element'{http://www.w3.org/2001/XMLSchema}element',att

  6. ruby-on-rails - 来自 cucumber 的 HTTP POST XML 内容 - 2

    我正在尝试通过POST将XML内容发送到一个简单的Rails项目中的Controller(“解析”)方法(“索引”)。它不是RESTful,因为我的模型名称不同,比如“汽车”。我在有效的功能测试中有以下内容:deftest_index...data_file_path=File.dirname(__FILE__)+'/../../app/views/layouts/index.xml.erb'message=ERB.new(File.read(data_file_path))xml_result=message.result(binding)doc=REXML::Document.ne

  7. ruby - 为什么我的带有 ANSI 颜色代码的 IRB 提示通过复制/粘贴弄乱了翻页/翻页行为? - 2

    我添加到我的.irbrc:IRB.conf[:PROMPT].reverse_merge!(:RAILS_ENV=>{:PROMPT_I=>"#{current_app}#{rails_env}#{prompt}",:PROMPT_N=>"#{current_app}#{rails_env}#{prompt}",:PROMPT_S=>nil,:PROMPT_C=>"?>",:RETURN=>"=>%s\n"})IRB.conf[:PROMPT_MODE]=:RAILS_ENV如果我这样做:current_app="\e[31mfoo_bar_app\e[0m"rails_env="\e

  8. ruby - 如何使用 XPath 和 Nokogiri 获取 XML 节点的内容 - 2

    我有这样的代码:@doc=Nokogiri::HTML(open(url)@doc.xpath(query).eachdo|html|putshtml#howgetcontentofanodeend我如何获取节点的内容而不是像这样: 最佳答案 这是READMEfile中的概要示例为Nokogiri展示了一种使用CSS、XPath或混合的方法:require'nokogiri'require'open-uri'#GetaNokogiri::HTML:Documentforthepagewe’reinterestedin...doc=N

  9. ruby - 使用 vim,是否有命令自动换行粘贴文本? - 2

    上下文:我正在做的部分工作涉及将文本段落从word文档粘贴到ruby​​文件中。问题:这些段落被粘贴为一行很长的文本,我必须手动插入换行符以使这些行的长度合理。解决方案:有没有办法让粘贴功能“意识到”合理的边距限制并在我粘贴时将文本换行? 最佳答案 先做一个settextwidth:settw=80然后执行gqq-对于单行对于整个文件ggVGgqq 关于ruby-使用vim,是否有命令自动换行粘贴文本?,我们在StackOverflow上找到一个类似的问题:

  10. ruby - 使用 Ruby 向网络服务器发送 XML 请求 - 2

    恐怕我没有太多通过网络服务器发布文档(例如XML)的经验,所以如果我对HTTP的理解不足,我深表歉意。我在127.0.0.1上的ruby​​应用程序中设置了一个基本的MongrelWeb服务器端口2000.(服务器)。我在同一台计算机上运行一个单独的Ruby应用程序。(客户)。我需要客户端向服务器发送XML文档。我曾尝试使用Net::HTTP来执行此操作,但我找不到一个明确的示例来告诉我应该做什么。我试过了,但遇到了错误。我已将请求分解,使其尽可能基本:http=Net::HTTP.new("127.0.0.1",2000)http.post('file','query=foo')#x

随机推荐