开发者

help with xml transformation

how can I transform some thing like

<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</Av开发者_开发百科gDuration>
     <TotalHangups>34</TotalHangups>
     <TotalHangupsaftClosedMsg>23</TotalHangupsaftClosedMsg>
     <TotalCallstoAgent>2</TotalCallstoAgent>
  </Row>
 </Table>
</NewDataSet>

To

<?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>


Try this:

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


An identity template based solution:

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


You an either use XSLT or just parse the first Xml, extract the nodes, and create the second Xml.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜