简体   繁体   English

从XML文档,XSLT和JAXB中删除元素

[英]Removing elements from an XML document, XSLT and JAXB

This question is a follow up to my earlier question: Creating a valid XSD that is open using <all> and <any> elements 这个问题是我之前提出的问题的后续问题: 创建一个使用<all>和<any>元素打开的有效XSD

Given that I have a Java String containing an XML document of the following form: 鉴于我有一个包含以下形式的XML文档的Java String:

<TRADE>
  <TIME>12:12</TIME>
  <MJELLO>12345</MJELLO>
  <OPTIONAL>12:12</OPTIONAL>
  <DATE>25-10-2011</DATE>
  <HELLO>hello should be ignored</HELLO>
</TRADE>

How can I use XSLT or similar (in Java by using JAXB) to remove all elements not contained in a set of elements. 我如何使用XSLT或类似的东西(在Java中使用JAXB)来删除一组元素中未包含的所有元素。 In the above example I am only interested in (TIME, OPTIONAL, DATE), so I would like to transform it into: 在上面的例子中,我只对(TIME,OPTIONAL,DATE)感兴趣,所以我想将其转换为:

<TRADE>
  <TIME>12:12</TIME>
  <OPTIONAL>12:12</OPTIONAL>
  <DATE>25-10-2011</DATE>
</TRADE>

The order of the elements is not fixed. 元素的顺序不固定。

This transformation: 这种转变:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pNames" select="'|TIME|OPTIONAL|DATE|'"/>
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="*/*">
  <xsl:if test="contains($pNames, concat('|', name(), '|'))">
   <xsl:call-template name="identity"/>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document: 当应用于提供的XML文档时:

<TRADE>
    <TIME>12:12</TIME>
    <MJELLO>12345</MJELLO>
    <OPTIONAL>12:12</OPTIONAL>
    <DATE>25-10-2011</DATE>
    <HELLO>hello should be ignored</HELLO>
</TRADE>

produces the wanted, correct result: 产生想要的,正确的结果:

<TRADE>
   <TIME>12:12</TIME>
   <OPTIONAL>12:12</OPTIONAL>
   <DATE>25-10-2011</DATE>
</TRADE>

Explanation : 说明

  1. The identity rule (template) copies every node "as-is". 身份规则(模板) “按原样” 复制每个节点

  2. The identity rule is overridden by a template matching any element that is not the top element of the document. 标识匹配任何不是文档顶部元素元素的模板会覆盖标识规则 Inside the template a check is made if the name of the matched element is one of the names specified in the external parameter $pNames in a pipe-delimited string of wanted names. 在模板内部,检查匹配元素的名称是否是在管道分隔的$pNames名称字符串中的外部参数$pNames中指定的名称之一。

  3. See the documentation of your XSLT processor on how to pass a parameter to a transformation -- this is implementation-dependent and differs from processor to processor. 有关如何将参数传递给转换的信息,请参阅XSLT处理器的文档 - 这与实现有关,因处理器而异。

I haven't tried yet, but maybe the javax.xml.tranform package can help: 我还没有尝试过,但也许javax.xml.tranform包可以帮助:

http://download.oracle.com/javase/6/docs/api/javax/xml/transform/package-summary.html http://download.oracle.com/javase/6/docs/api/javax/xml/transform/package-summary.html

JAXB & XSLT JAXB和XSLT

JAXB integrates very cleanly with XSLT for an example see: JAXB非常干净地与XSLT集成,例如:

Your Other Question 你的其他问题

Based on your previous question (see link below), the transform is really unnecessary as JAXB will just ignore attributes and elements that are not mapped to fields/properties in your domain object. 根据您之前的问题(请参阅下面的链接),转换实际上是不必要的,因为JAXB将忽略未映射到域对象中的字段/属性的属性和元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM