开发者

How to extend an XML File using ant xmltask by copying tags from a source-file to the dest-file in specific positions

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&l开发者_如何学Pythont;/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>


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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜