使用XSLT拆分重复的文本()并将非重复项组合在一起

时间:2015-01-20 11:23:23

标签: xml xslt

我有以下输入XML:

<root>
    <element>
          <id>1</id>
          <text><![CDATA[My text 1]]></text>
    </element>
    <element>
          <id>2</id>
          <text><![CDATA[My text 1]]></text>
    </element>
    <element>
          <id>3</id>
          <text><![CDATA[My text 2]]></text>
    </element>
    <element>
          <id>4</id>
          <text><![CDATA[My text 2]]></text>
    </element>
    <element>
          <id>5</id>
          <text><![CDATA[My text 3]]></text>
    </element>
</root>

我希望使用XSLT 2.0对其进行转换,在文本元素中拆分重复的文本()并将我的非重复项组合成单独的文件(用于任意数量的重复项 - 我的例子只显示了两个)。所以我的任何输出文件中都没有重复的text(),并且需要将它们分组为尽可能少的文件。我上面的输出应该是这样的:

document1.xml

<root>
    <element>
          <id>1</id>
          <text><![CDATA[My text 1]]></text>
    </element>
    <element>
          <id>3</id>
          <text><![CDATA[My text 2]]></text>
    </element>
    <element>
          <id>5</id>
          <text><![CDATA[My text 3]]></text>
    </element>
</root>

document2.xml

<root>
    <element>
          <id>2</id>
          <text><![CDATA[My text 1]]></text>
    </element>
    <element>
          <id>4</id>
          <text><![CDATA[My text 2]]></text>
    </element>
</root>

我现有的XSLT片段看起来像这样:我觉得我需要在我的for-each-groups中收集我的副本(为了按位置拆分),但显然这导致每个元素一个文件:

<xsl:for-each-group select="descendant::element" group-by="text[text() = preceding::text/text() or text() = following::text/text()]">
            <xsl:result-document href="{concat($outputdir,'\document',position(),'.xml')}" method="xml" indent="yes" cdata-section-elements="text">
        <root>
            <xsl:copy-of select="."/>   
        </root>
    </xsl:result-document>
</xsl:for-each-group>

感谢您提供的任何帮助。提前谢谢。

2 个答案:

答案 0 :(得分:2)

以下内容看起来像是使用XSLT完成的必要解决方案,但我认为它可以完成这项任务:

<xsl:stylesheet
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">

<xsl:output indent="yes"/>

<xsl:template match="root">
  <xsl:variable name="groups">
    <xsl:for-each-group select="element" group-by="text">
      <group key="{current-grouping-key()}">
        <xsl:copy-of select="current-group()"/>
      </group>
    </xsl:for-each-group>
  </xsl:variable>
  <xsl:variable name="max-size" select="max($groups/group/count(element))"/>
  <xsl:for-each select="1 to $max-size">
    <xsl:result-document href="document{.}.xml">
      <root>
        <xsl:copy-of select="$groups/group/element[position() eq current()]"/>
      </root>
    </xsl:result-document>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

答案 1 :(得分:0)

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

    <xsl:template match="/root">
        <xsl:call-template name="out">
            <xsl:with-param name="level" select="1"/>
            <xsl:with-param name="root" select="."/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="out">
        <xsl:param name="root"/>
        <xsl:param name="level"/>

        <xsl:if test="$root/*">
            <xsl:result-document href="document{$level}.xml">
                <root>
                    <xsl:for-each-group select="$root/*" group-by="text">
                        <xsl:copy-of select="current()"/>
                    </xsl:for-each-group>
                </root>
            </xsl:result-document>

            <xsl:call-template name="out">
                <xsl:with-param name="level" select="$level+1"/>
                <xsl:with-param name="root">
                    <xsl:for-each-group select="$root/*" group-by="text">
                        <xsl:copy-of select="current-group()[. != current()]"/>
                    </xsl:for-each-group>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>