帮助xml转换

时间:2011-02-05 00:06:16

标签: xml xslt transformation

我怎样才能改变像

这样的东西
<NewDataSet>
 <Table>
   <dnis>Dec 5 Call</dnis>
   <monthval>12</monthval>
   <Totalcalls>2</Totalcalls>
   <AvgDuration>23</AvgDuration>
   <TotalHangups>34</TotalHangups>
   <TotalHangupsaftClosedMsg>23</TotalHangupsaftClosedMsg>
   <TotalCallstoAgent>2</TotalCallstoAgent>
   <Row>
     <dnis>Dec 5 Call</dnis>
     <Week>Dec  5-Dec 11</Week>
     <Totalcalls>2</Totalcalls>
     <AvgDuration>23</AvgDuration>
     <TotalHangups>34</TotalHangups>
     <TotalHangupsaftClosedMsg>23</TotalHangupsaftClosedMsg>
     <TotalCallstoAgent>2</TotalCallstoAgent>
   </Row>
   <Row>
     <dnis>Dec 5 Call</dnis>
     <Week>Dec  5-Dec 11</Week>
     <Totalcalls>2</Totalcalls>
     <AvgDuration>23</AvgDuration>
     <TotalHangups>34</TotalHangups>
     <TotalHangupsaftClosedMsg>23</TotalHangupsaftClosedMsg>
     <TotalCallstoAgent>2</TotalCallstoAgent>
  </Row>
 </Table>
</NewDataSet>

<?xml version="1.0" encoding="utf-8"?>
  <NewDataSet>
    <Table>
      <Header>
        <dnis>Dec 5 Call</dnis>
        <monthval>12</monthval>
        <Totalcalls>2</Totalcalls>
        <AvgDuration>23</AvgDuration>
        <TotalHangups>34</TotalHangups>
        <TotalHangupsaftClosedMsg>23</TotalHangupsaftClosedMsg>
        <TotalCallstoAgent>2</TotalCallstoAgent>
      </Header>
      <Body>
        <Row>
          <dnis>Dec 5 Call</dnis>
          <Week>Dec  5-Dec 11</Week>
          <Totalcalls>2</Totalcalls>
          <AvgDuration>23</AvgDuration>
          <TotalHangups>34</TotalHangups>
          <TotalHangupsaftClosedMsg>23</TotalHangupsaftClosedMsg>
          <TotalCallstoAgent>2</TotalCallstoAgent>
        </Row>
        <Row>
          <dnis>Dec 5 Call</dnis>
          <Week>Dec  5-Dec 11</Week>
          <Totalcalls>2</Totalcalls>
          <AvgDuration>23</AvgDuration>
          <TotalHangups>34</TotalHangups>
          <TotalHangupsaftClosedMsg>23</TotalHangupsaftClosedMsg>
          <TotalCallstoAgent>2</TotalCallstoAgent>
        </Row>
      </Body>
    </Table>
  </NewDataSet>

3 个答案:

答案 0 :(得分:1)

试试这个:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/NewDataSet">
      <NewDataSet>
        <xsl:for-each select="Table">
        <Table>
          <Header>
            <xsl:for-each select="*[name() != 'Row']">
              <xsl:copy-of select="." />
            </xsl:for-each>
          </Header>
          <Body>
            <xsl:for-each select="Row">
              <xsl:copy-of select="." />
            </xsl:for-each>
          </Body>
        </Table>
        </xsl:for-each>
      </NewDataSet>
  </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:1)

基于身份模板的解决方案:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="Table">
    <xsl:copy>
      <Header>
        <xsl:apply-templates select="*[not(self::Row)]"/>
      </Header>
      <Body>
        <xsl:apply-templates select="Row"/>
      </Body>
    </xsl:copy>
  </xsl:template>

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

</xsl:stylesheet>

答案 2 :(得分:0)

您要么使用XSLT,要么只解析第一个Xml,提取节点,然后创建第二个Xml。