复制特定的xml块

时间:2011-08-08 09:36:09

标签: xml xslt xpath

我有一个xml如下。

   <emp>
      <Name>Rice</Name>
      <Designation>Operator</Designation>
      <sal>$2000</sal> 
      <hobbies><description>1.Playaing Games 2.Watching Movies</description></hobbies>
   </emp>
   <emp>
      <Name>Jeff</Name>
      <Designation>Admin</Designation>
      <sal>$1000</sal> 
      <hobbies><description>1.Listening Music</description></hobbies>
   </emp>

每当遇到标记<hobbies>时,我都需要复制业余部分(<hobbies>1.Playaing Games 2.Watching Movies</hobbies>),因为它在结果文档中。 我正在使用xsl从一个xml转换为另一个xml.My out应该如下所示。

 <EmployeeDetails>
    Rice,Operator,$2000
    <hobbies><description>1.Playaing Games 2.Watching Movies</description></hobbies>
  </EmployeeDetails>
   <EmployeeDetails>
    Jeff,Admin,$1000
    <hobbies><description>1.Listening Music</description></hobbies> 
  </EmployeeDetails>

请给我一些指示,以实现相同的目标。

1 个答案:

答案 0 :(得分:0)

使用此模板:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="emp">
        <EmployeeDetails>
            <xsl:value-of select="concat(Name, ',', Designation, ',', sal)"/>

            <xsl:copy-of select="hobbies"/>
        </EmployeeDetails>
    </xsl:template>

</xsl:stylesheet>

输出:

<EmployeeDetails>
    Rice,Operator,$2000<hobbies>
        <description>1.Playaing Games 2.Watching Movies</description>
    </hobbies>
</EmployeeDetails>
<EmployeeDetails>
    Jeff,Admin,$1000<hobbies>
        <description>1.Listening Music</description>
    </hobbies>
</EmployeeDetails>