1

下面是我的 xml 摘录:

<?xml version="1.0"?>
    <catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>Superb story of army veteran</description>
   </book>
   </catalog>

我想根据书拆分它,以便将其作为输出::

<Object-bean>
<child-bean>
 <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
</child-bean>

<child-bean>
<book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>Superb story of army veteran</description>
   </book>
</child-bean>
</Object-bean>

子bean节点我需要通过注释与java类进行映射,以便将子bean节点列表作为字符串。这是我的java代码::

 @XmlRootElement(name = "Child-bean")
 @XmlAccessorType(XmlAccessType.NONE)
    public class ChildBean {
@XmlAttribute(name="price")
private String price;
@XmlAttribute(name="country")
private String country;


public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

}

这是我的对象 bean 类::

@XmlRootElement(name = "Object-bean")
@XmlAccessorType(XmlAccessType.FIELD)
 public class ObjectBean {

@XmlAttribute(name = "name")
private String name;
@XmlAttribute
private String id;

@XmlElement(name="Child-bean")
private List<ChildBean> childList = new ArrayList<ChildBean>();
   }
4

1 回答 1

0

我不了解您的 XML 和 Java 代码之间的关系,但就 XSLT 而言,它看起来好像您只是想将每个book元素包装成一个child-bean元素,并且您想要转换根元素 ( catalog-> Object-bean),所以您只需要

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

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

<xsl:template match="/catalog">
  <Object-bean>
   <xsl:apply-templates select="@* | node()"/>
  </Object-bean>
</xsl:template>
于 2013-08-22T10:54:13.907 回答