帮助进行 xml 转换

发布于 2024-10-16 08:16:38 字数 2710 浏览 3 评论 0原文

这样的东西

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

我怎样才能改变像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>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秉烛思 2024-10-23 08:16:38

试试这个:

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

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>
掩饰不了的爱 2024-10-23 08:16:38

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

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

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>
笔芯 2024-10-23 08:16:38

您可以使用 XSLT 或仅解析第一个 Xml、提取节点并创建第二个 Xml。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文