如何使用xslt对xml元素进行分组

时间:2012-03-08 13:27:24

标签: xml xslt

我需要将现有的XML结构转换为另一种XML结构。

XMLSource:

 <?xml version="1.0"?>
  <content>
                <first>Paragraph-1</first>
                <comment>Comment-1</comment>
                <likes>like-1</likes>

                <first>Paragraph-2</first>
                <comment>Comment-2</comment>
                <likes>like-2</likes>

                <first>Paragraph-3</first>
                <comment>Comment-3</comment>

 </content>

需要的输出格式:

<content1>
 <block>
  <aaa>Paragraph-1</aaa>
  <bbb>Comment-1</bbb>
  <ccc>like-1</ccc>
 </block>
 <block>
  <aaa>Paragraph-2</aaa>
  <bbb>Comment-2</bbb>
  <ccc>like-2</ccc>
 </block>
 <block>
  <aaa>Paragraph-3</aaa>
  <bbb>Comment-3</bbb>
 </block>
</content1>

如何使用XSLT完成此操作。 请帮忙。

3 个答案:

答案 0 :(得分:2)

请在下方观看强大的XML,如果您需要更多建议,请与我们联系。 :)我提供的不仅仅是你提出的问题。

INPUT XML:

<?xml version="1.0" encoding="utf-8"?>
    <content>
      <first>Paragraph-1</first>
      <likes>like-1</likes>

      <first>Paragraph-2</first>
      <comment>Comment-2</comment>
      <likes>like-2</likes>

      <first>Paragraph-3</first>
      <comment>Comment-3</comment>

      <first>Paragraph-4</first>
      <comment>Comment-4</comment>
      <likes>like-4</likes>
    </content>

XSLT代码:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="content">
    <xsl:element name="content1">
      <xsl:apply-templates select="node()|@*"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="first">
    <xsl:variable name="count" select="count(following-sibling::first)"/>
    <xsl:element name="block">
      <aaa>
        <xsl:apply-templates select="node()|@*"/>
      </aaa>
      <xsl:for-each select="following-sibling::comment[1][count(following-sibling::first) = $count]">
        <bbb>
          <xsl:apply-templates select="node()|@*"/>
        </bbb>
      </xsl:for-each>
      <xsl:for-each select="following-sibling::likes[1][count(following-sibling::first) = $count]">
        <ccc>
          <xsl:apply-templates select="node()|@*"/>
        </ccc>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>
  <xsl:template match="comment|likes"/>

</xsl:stylesheet>

输出XML:

<?xml version="1.0" encoding="utf-8"?>
<content1>
  <block>
    <aaa>Paragraph-1</aaa>
    <ccc>like-1</ccc>
  </block>

  <block>
    <aaa>Paragraph-2</aaa>
    <bbb>Comment-2</bbb>
    <ccc>like-2</ccc>
  </block>

  <block>
    <aaa>Paragraph-3</aaa>
    <bbb>Comment-3</bbb>
  </block>

  <block>
    <aaa>Paragraph-4</aaa>
    <bbb>Comment-4</bbb>
    <ccc>like-4</ccc>
  </block>
</content1>

这就像宝石一样!!!!!! :d

答案 1 :(得分:1)

这是一个XSLT 2.0示例:

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

  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:param name="start-name" select="'a'"/>
  <xsl:param name="nchars" select="3"/>
  <xsl:variable name="start-index" select="string-to-codepoints($start-name)"/>

  <xsl:template match="content">
    <content1>
      <xsl:for-each-group select="*" group-starting-with="first">
        <block>
          <xsl:apply-templates select="current-group()"/>
        </block>
      </xsl:for-each-group>
    </content1>
  </xsl:template>

  <xsl:template match="content/*">
    <xsl:variable name="pos" select="position()"/>
    <xsl:element name="{string-join(for $c in 1 to $nchars return codepoints-to-string($pos - 1 + $start-index), '')}">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

答案 2 :(得分:0)

这是我喜欢的另一个XSLT 1.0选项,因为一切都有它自己的模板。我认为这比使用多个xsl:for-each更容易增加样式表的复杂性。

XML输入

<content>
  <first>Paragraph-1</first>
  <likes>like-1</likes>

  <first>Paragraph-2</first>
  <comment>Comment-2</comment>
  <likes>like-2</likes>

  <first>Paragraph-3</first>
  <comment>Comment-3</comment>

  <first>Paragraph-4</first>
  <comment>Comment-4</comment>
  <likes>like-4</likes>
</content>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

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

  <xsl:template match="first">
    <block>
      <aaa>
        <xsl:apply-templates select="node()|@*"/>
      </aaa>
      <xsl:apply-templates select="following-sibling::*[1][name()='comment']" mode="mod"/>
      <xsl:apply-templates select="following-sibling::*[1][name()='likes']" mode="mod"/>      
    </block>
  </xsl:template>

  <xsl:template match="comment" mode="mod">
    <bbb>
      <xsl:apply-templates select="node()|@*"/>
    </bbb>
    <xsl:apply-templates select="following-sibling::*[1][name()='likes']" mode="mod"/>
  </xsl:template>

  <xsl:template match="likes" mode="mod">
    <ccc>
      <xsl:apply-templates select="node()|@*"/>
    </ccc>
  </xsl:template>

  <xsl:template match="comment|likes"/>

</xsl:stylesheet>

XML输出

<content1>
   <block>
      <aaa>Paragraph-1</aaa>
      <ccc>like-1</ccc>
   </block>
   <block>
      <aaa>Paragraph-2</aaa>
      <bbb>Comment-2</bbb>
      <ccc>like-2</ccc>
   </block>
   <block>
      <aaa>Paragraph-3</aaa>
      <bbb>Comment-3</bbb>
   </block>
   <block>
      <aaa>Paragraph-4</aaa>
      <bbb>Comment-4</bbb>
      <ccc>like-4</ccc>
   </block>
</content1>