在第 n 级后代上对 Xmldocument 进行分组和拆分



我想将一个 XmlDocument 拆分为一个 XmlDocuments 数组,其中每个拆分的 XmlDocument 都包含特定时间段(年/月组合)的记录。更复杂的因素是,imo,分组应该发生在嵌套元素上。

示例输入:

<?xml version="1.0" encoding="utf-8"?>
<Example>
  <RecordA>
    <RecordA1>
      <RecordA11>
        <ElementA11></ElementA11>
      </RecordA11>
    </RecordA1>
    <RecordA2>
      <ElementA2></ElementA2>
    </RecordA2>
  </RecordA>
  <RecordB>
    <RecordB1>
      <ElementB1></ElementB1>
      <RecordB11>
        <ElementB11></ElementB11>
        <RecordB111>
          <RecordB1111>
            <RecordB11111>
              <ElementB11111></ElementB11111>
            </RecordB11111>
            <ElementB1111></ElementB1111>
            <RecordB11112>
              <Dates>
                <StartDate>2014-05-29</StartDate>
                <EndDate>2014-05-29</EndDate>
              </Dates>
            </RecordB11112>
            <RecordB11112>
              <Dates>
                <StartDate>2014-06-02</StartDate>
                <EndDate>2014-06-02</EndDate>
              </Dates>
            </RecordB11112>
            <RecordB11112>
              <Dates>
                <StartDate>2014-05-21</StartDate>
                <EndDate>2014-05-21</EndDate>
              </Dates>
            </RecordB11112>
            <RecordB11112>
              <Dates>
                <StartDate>2014-04-09</StartDate>
                <EndDate>2014-04-09</EndDate>
              </Dates>
            </RecordB11112>
            <RecordB11112>
              <Dates>
                <StartDate>2014-06-05</StartDate>
                <EndDate>2014-06-05</EndDate>
              </Dates>
            </RecordB11112>
          </RecordB1111>
        </RecordB111>
      </RecordB11>
    </RecordB1>
  </RecordB>
</Example>

想要的输出:

<?xml version="1.0" encoding="utf-8"?>
<Examples>
  <Example>
    <RecordA>
      <RecordA1>
        <RecordA11>
          <ElementA11></ElementA11>
        </RecordA11>
      </RecordA1>
      <RecordA2>
        <ElementA2></ElementA2>
      </RecordA2>
    </RecordA>
    <RecordB>
      <RecordB1>
        <ElementB1></ElementB1>
        <RecordB11>
          <ElementB11></ElementB11>
          <RecordB111>
            <RecordB1111>
              <RecordB11111>
                <ElementB11111></ElementB11111>
              </RecordB11111>
              <ElementB1111></ElementB1111>
              <RecordB11112>
                <Dates>
                  <StartDate>2014-05-29</StartDate>
                  <EndDate>2014-05-29</EndDate>
                </Dates>
              </RecordB11112>
              <RecordB11112>
                <Dates>
                  <StartDate>2014-05-21</StartDate>
                  <EndDate>2014-05-21</EndDate>
                </Dates>
              </RecordB11112>
            </RecordB1111>
          </RecordB111>
        </RecordB11>
      </RecordB1>
    </RecordB>
  </Example>
  <Example>
    <RecordA>
      <RecordA1>
        <RecordA11>
          <ElementA11></ElementA11>
        </RecordA11>
      </RecordA1>
      <RecordA2>
        <ElementA2></ElementA2>
      </RecordA2>
    </RecordA>
    <RecordB>
      <RecordB1>
        <ElementB1></ElementB1>
        <RecordB11>
          <ElementB11></ElementB11>
          <RecordB111>
            <RecordB1111>
              <RecordB11111>
                <ElementB11111></ElementB11111>
              </RecordB11111>
              <ElementB1111></ElementB1111>
              <RecordB11112>
                <Dates>
                  <StartDate>2014-04-09</StartDate>
                  <EndDate>2014-04-09</EndDate>
                </Dates>
              </RecordB11112>
            </RecordB1111>
          </RecordB111>
        </RecordB11>
      </RecordB1>
    </RecordB>
  </Example>
  <Example>
    <RecordA>
      <RecordA1>
        <RecordA11>
          <ElementA11></ElementA11>
        </RecordA11>
      </RecordA1>
      <RecordA2>
        <ElementA2></ElementA2>
      </RecordA2>
    </RecordA>
    <RecordB>
      <RecordB1>
        <ElementB1></ElementB1>
        <RecordB11>
          <ElementB11></ElementB11>
          <RecordB111>
            <RecordB1111>
              <RecordB11111>
                <ElementB11111></ElementB11111>
              </RecordB11111>
              <ElementB1111></ElementB1111>
              <RecordB11112>
                <Dates>
                  <StartDate>2014-06-02</StartDate>
                  <EndDate>2014-06-02</EndDate>
                </Dates>
              </RecordB11112>
              <RecordB11112>
                <Dates>
                  <StartDate>2014-06-05</StartDate>
                  <EndDate>2014-06-05</EndDate>
                </Dates>
              </RecordB11112>
            </RecordB1111>
          </RecordB111>
        </RecordB11>
      </RecordB1>
    </RecordB>
  </Example>
</Examples>

我认为您可以使用Muenchian分组来标识每个组中的第一项,然后您需要为每个组重新创建树:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="group" match="RecordB11112" use="substring(Dates/StartDate, 1, 7)"/>
<xsl:template match="/">
  <Examples>
    <xsl:apply-templates select="//RecordB11112[generate-id() = generate-id(key('group', substring(Dates/StartDate, 1, 7))[1])]"/>
  </Examples>
</xsl:template>
<xsl:template match="RecordB11112">
  <xsl:variable name="to-be-copied" select="key('group', substring(Dates/StartDate, 1, 7))"/>
  <xsl:apply-templates select="/*" mode="recreate">
    <xsl:with-param name="to-be-copied" select="$to-be-copied"/>
  </xsl:apply-templates>
</xsl:template>
<xsl:template match="@* | node()" mode="recreate">
  <xsl:param name="to-be-copied"/>
  <xsl:copy>
    <xsl:apply-templates select="@*" mode="recreate"/>
    <xsl:apply-templates mode="recreate">
      <xsl:with-param name="to-be-copied" select="$to-be-copied"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>
<xsl:template match="RecordB11112" mode="recreate">
  <xsl:param name="to-be-copied"/>
  <xsl:if test="$to-be-copied[generate-id() = generate-id(current())]">
    <xsl:copy-of select="."/>
  </xsl:if>
</xsl:template>
</xsl:stylesheet>

相关内容

  • 没有找到相关文章

最新更新