如何使用 ant xmltask 通过将标签从源文件复制到目标文件的特定位置来扩展 XML 文件

发布于 2024-10-18 04:46:33 字数 3886 浏览 1 评论 0原文

我正在尝试为本地开发创建一个特殊的 web.xml 。

我有一些标签存储在一个单独的 xml 文件中,需要选择这些标签并将其粘贴到最终 web.xml 中的特定位置。

为了实现此目的,我按以下方式使用复制和粘贴操作:

<xmltask source="templates.xml">
  <copy path="//data/init-param" buffer="initParamBuffer"/>
</xmltask>

<xmltask source="web.xml" dest="web.xml">
  <paste path="//web-app/filter[contains(./filter-name,'MyFilter')]
                           /init-param[contains(./param-name,
                                                'MyInitParameter')]"
         position="after" buffer="initParamBuffer"/>
</xmltask>

我的目的是从源文件中收集所有 init-param 标签,并将它们粘贴到粘贴操作中选择的标签之后。

此外,我使用 contains() 函数选择包含具有指定内容的标签的标签的部分也运行不顺利。

也许有更好的方法来形成这个 xpath 表达式...


更新:

正如我之前所写的,我不知道解决这个问题的最佳方法。我已经阅读过有关使用样式表进行转换的可能性,但由于 ant-xmltask 承诺是一个更巧妙的解决方案,我首先尝试了这一点。

据我采用这种方法,可以使用这种方法将标签插入/写入到 web.xml 中。我已经成功地在稍微偏离的位置插入了单个 init-param 标签,并且表达式不太复杂:

<paste path="//web-app/filter[1]/init-param"
             position="after" buffer="initParamBuffer"/>

所以我的问题是: A:我想选择多个标签到缓冲区中 B:我想将该缓冲区的内容插入到由名称(而不是索引)指定的标记之后。

以下是插入到 web.xml 中的源 (templates.xml) 的示例:

<data>
  <init-param>
    <param-name>newparam1</param-name>
    <param-value>1</param-value>
  </init-param>
  <init-param>
    <param-name>newparam2</param-name>
    <param-value>2</param-value>
  </init-param>
</data>

这是 web.xml 的一部分,其中将粘贴上述部分:

<web-app>
  <filter-mapping>
    <filter-name>SomeFilter</filter-name>
    <url-pattern>/somePath/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>MyFilter</filter-name>
    <url-pattern>/myPath/*</url-pattern>
  </filter-mapping>

  <filter>
    <filter-name>SomeFilter</filter-name>
    <filter-class>foo.bar.SomeFilter</filter-class>
    <init-param>
      <param-name>SomeInitParameter</param-name>
      <param-value>4711</param-value>
    </init-param>
  </filter>
  <filter>
    <filter-name>MyFilter</filter-name>
    <filter-class>foo.bar.MyFilter</filter-class>
    <init-param>
      <param-name>MyInitParameter</param-name>
      <param-value>0815</param-value>
    </init-param>
  </filter>
</web-app>

这是我希望实现的结果:

<web-app>
  <filter-mapping>
    <filter-name>SomeFilter</filter-name>
    <url-pattern>/somePath/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>MyFilter</filter-name>
    <url-pattern>/myPath/*</url-pattern>
  </filter-mapping>

  <filter>
    <filter-name>SomeFilter</filter-name>
    <filter-class>foo.bar.SomeFilter</filter-class>
    <init-param>
      <param-name>SomeInitParameter</param-name>
      <param-value>4711</param-value>
    </init-param>
  </filter>
  <filter>
    <filter-name>MyFilter</filter-name>
    <filter-class>foo.bar.MyFilter</filter-class>
    <init-param>
      <param-name>MyInitParameter</param-name>
      <param-value>0815</param-value>
    </init-param>
    <init-param>
      <param-name>newparam1</param-name>
      <param-value>1</param-value>
    </init-param>
    <init-param>
      <param-name>newparam2</param-name>
      <param-value>2</param-value>
    </init-param>
  </filter>
</web-app>

Im trying to create a special web.xml for local development.

I have some tags stored in a separate xml-file which need to be selected and pasted to specific positions inside the final web.xml.

To achieve this I am using the copy and the paste action in the following manner:

<xmltask source="templates.xml">
  <copy path="//data/init-param" buffer="initParamBuffer"/>
</xmltask>

<xmltask source="web.xml" dest="web.xml">
  <paste path="//web-app/filter[contains(./filter-name,'MyFilter')]
                           /init-param[contains(./param-name,
                                                'MyInitParameter')]"
         position="after" buffer="initParamBuffer"/>
</xmltask>

My intention is to gather ALL init-param Tags from the source File and paste them after the Tag selected in the paste operation.

Also the Part where Im selecting a Tag which contains a tag with a specified content using the contains() function is not working smoothly either.

Maybe there is a better way to form this xpath expression...


update:

As I have written before, I do not know the best approach to this problem. I have read about the possibility to transform using stylesheets, but since the ant-xmltask promised to be a more sleak sollution I have tried this first.

As far as I have come with this approach, it is possible to insert/write tags into the web.xml using this approach. I have succeeded in inserting single init-param tags at locations that where sligtly off, with a less complex expression:

<paste path="//web-app/filter[1]/init-param"
             position="after" buffer="initParamBuffer"/>

So my Problem was:
A: I want to select more than one tag into the buffer
B: I want to insert the content of that buffer after a tag specified by a name (not index).

Here is an example for the sources (templates.xml) to insert into the web.xml:

<data>
  <init-param>
    <param-name>newparam1</param-name>
    <param-value>1</param-value>
  </init-param>
  <init-param>
    <param-name>newparam2</param-name>
    <param-value>2</param-value>
  </init-param>
</data>

Here is part of an web.xml where the above section is to be pasted:

<web-app>
  <filter-mapping>
    <filter-name>SomeFilter</filter-name>
    <url-pattern>/somePath/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>MyFilter</filter-name>
    <url-pattern>/myPath/*</url-pattern>
  </filter-mapping>

  <filter>
    <filter-name>SomeFilter</filter-name>
    <filter-class>foo.bar.SomeFilter</filter-class>
    <init-param>
      <param-name>SomeInitParameter</param-name>
      <param-value>4711</param-value>
    </init-param>
  </filter>
  <filter>
    <filter-name>MyFilter</filter-name>
    <filter-class>foo.bar.MyFilter</filter-class>
    <init-param>
      <param-name>MyInitParameter</param-name>
      <param-value>0815</param-value>
    </init-param>
  </filter>
</web-app>

And here is the result I would hope to achieve:

<web-app>
  <filter-mapping>
    <filter-name>SomeFilter</filter-name>
    <url-pattern>/somePath/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>MyFilter</filter-name>
    <url-pattern>/myPath/*</url-pattern>
  </filter-mapping>

  <filter>
    <filter-name>SomeFilter</filter-name>
    <filter-class>foo.bar.SomeFilter</filter-class>
    <init-param>
      <param-name>SomeInitParameter</param-name>
      <param-value>4711</param-value>
    </init-param>
  </filter>
  <filter>
    <filter-name>MyFilter</filter-name>
    <filter-class>foo.bar.MyFilter</filter-class>
    <init-param>
      <param-name>MyInitParameter</param-name>
      <param-value>0815</param-value>
    </init-param>
    <init-param>
      <param-name>newparam1</param-name>
      <param-value>1</param-value>
    </init-param>
    <init-param>
      <param-name>newparam2</param-name>
      <param-value>2</param-value>
    </init-param>
  </filter>
</web-app>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮倦 2024-10-25 04:46:33

使用 XPath 或:

路径|路径

从:

<paste path="//web-app/filter[1]/init-param" position="after" buffer="initParamBuffer"/>

到:

<paste path="//web-app/filter[1]/init-param|//web-app/filter[2]/init-param" position="after" buffer="initParamBuffer"/>

Use XPath Or:

Path|Path

From:

<paste path="//web-app/filter[1]/init-param" position="after" buffer="initParamBuffer"/>

To:

<paste path="//web-app/filter[1]/init-param|//web-app/filter[2]/init-param" position="after" buffer="initParamBuffer"/>
深陷 2024-10-25 04:46:33

XPath 是一种在 XML 文档的 XML 信息集上运行的查询语言。它只能返回结果或选择节点,但不能更改源 XML 文档的结构或以任何方式对其进行修改

XSLT 是专门为处理 XML 文档和生成新 XML 文档而设计的合适语言。

您的问题中指定的任务可以使用 XSLT 以非常简单的方式完成。

请提供完整(但最少)的源 XML 文档、完整的所需结果并指定转换应遵循的任何规则。然后很多人就能给你正确且有用的解决方案。

XPath is a Query language operating on the XML Infoset of XML documents. It can only return results or select nodes, but it cannot change the structure of the source XML document or modify it in any way.

A suitable language, designed especially for processing XML documentsand producing new XML documents, is XSLT.

The task specified in your question can be done in a very easy way using XSLT.

Please, provide a complete (but minimal) source XML document, the complete wanted result and specify any rules that the transformation should follow. Then many people will be able to give you correct and useful solutions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文