2

私はいくつかの異常な問題を抱えています。私の仕事は「XMLを構造化する」ことです。これは入力 XML です (例):

<documents>
  <document>Review</document>
  <document_id>REV#1</document_id>
  <item>List of terms</item>
  <item_id>10</item_id>
  <item_description>Terms used in documents</item_description>
  <item_attribute>Term</item_attribute>
  <item_attribute_name>SA</item_attribute_name>
  <item_attribute_value>Some Attribute</item_attribute_value>
  <item_attribute_name>SOA</item_attribute_name>
  <item_attribute_value>Some Other Attribute</item_attribute_value>

  <document>Interface</document>
  <document_id>AC-163</document_id>
  <item>List of terms</item>
  <item_id>15</item_id>
  <item_description>Another item</item_description>
  <item_attribute>Term</item_attribute>
  <item_attribute_name>Name#1</item_attribute_name>
  <item_attribute_value>Att#1</item_attribute_value>
  <item_attribute_name>Name#2</item_attribute_name>
  <item_attribute_value>Att#2</item_attribute_value>
</documents>

私がすべきことは、それを次のエンティティ構造に変換することです:

ドキュメント 1..* ドキュメント 1..1 アイテム 1..* アイテム 1..1 属性 1..* 属性

つまり、要素 'documents' には多くの 'document' を含めることができ、'document' には 'items' という名前の要素を 1 つだけ含めることができ、要素 'items' には多くの要素 'item' を含めることができます。

上記の例の期待される出力は次のとおりです。

<documents>
    <document>
        <document_id>REV#1</document_id>
        <items>
            <item>
                <id>10</id>
                <description>Terms used in documents</description>
                <attributes>
                    <attribute>
                        <name>SA</name>
                        <value>Some Attribute</value>
                    </attribute>
                    <attribute>
                        <name>SOA</name>
                        <value>Some Other Attribute</value>
                    </attribute>
                </attributes>
            </item>
        </items>
    </document>
    <document>
        <document_id>AC-163</document_id>
        <items>
            <item>
                <id>15</id>
                <description>Another item</description>
                <attributes>
                    <attribute>
                        <name>Name#1</name>
                        <value>Att#1</value>
                    </attribute>
                    <attribute>
                        <name>Name#2</name>
                        <value>Att#2</value>
                    </attribute>
                </attributes>
            </item>
        </items>
    </document>
</documents>

このタスクで問題が発生する必要があります.... 助けを求めてもよろしいですか? これはxmlを「構造化」するのは珍しいことです - 何かアイデアはありますか?

よろしくお願いします!

4

1 に答える 1

1

XSLT 2.0 を使用している場合は、 XSLT グループ化を使用してこれを簡単に実現できますが、XSLT 1.0 で行き詰まった場合に備えて、XSLT 1.0 互換のソリューションを次に示します。

スタイルシート

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="document">
    <!-- Store the unique ID of the current element into a variable. -->
    <xsl:variable name="id" select="generate-id()"/>

    <xsl:copy>
      <xsl:apply-templates select="following-sibling::document_id[1]"/>
      <items>
        <!--
        Apply all <item_attribute_name> elements whose first preceding
        <document> sibling is the element that's currently being processed. This
        is to keep from processing *all* of the rest of the
        <item_attribute_name> elements in the document.
        -->
        <xsl:apply-templates select="following-sibling::item
          [preceding-sibling::document[1][generate-id() = $id]]"/>
      </items>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="item">
    <xsl:variable name="id" select="generate-id()"/>

    <xsl:copy>
      <xsl:apply-templates select="following-sibling::item_id[1]"/>
      <xsl:apply-templates select="following-sibling::item_description[1]"/>
      <attributes>
        <xsl:apply-templates select="following-sibling::item_attribute_name
          [preceding-sibling::item[1][generate-id() = $id]]"/>
      </attributes>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="documents">
    <xsl:copy>
      <xsl:apply-templates select="document"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="item_description">
    <description>
      <xsl:apply-templates/>
    </description>
  </xsl:template>

  <xsl:template match="item_id">
    <id>
      <xsl:apply-templates/>
    </id>
  </xsl:template>

  <xsl:template match="item_attribute_name">
    <attribute>
      <name>
        <xsl:apply-templates/>
      </name>

      <xsl:apply-templates select="following-sibling::item_attribute_value[1]"/>
    </attribute>
  </xsl:template>

  <xsl:template match="item_attribute_value">
    <value>
      <xsl:apply-templates/>
    </value>
  </xsl:template>

  <xsl:template match="item_attribute"/>

</xsl:stylesheet>

入力

<documents>
  <document>Review</document>
  <document_id>REV#1</document_id>
  <item>List of terms</item>
  <item_id>10</item_id>
  <item_description>Terms used in documents</item_description>
  <item_attribute>Term</item_attribute>
  <item_attribute_name>SA</item_attribute_name>
  <item_attribute_value>Some Attribute</item_attribute_value>
  <item_attribute_name>SOA</item_attribute_name>
  <item_attribute_value>Some Other Attribute</item_attribute_value>

  <document>Interface</document>
  <document_id>AC-163</document_id>
  <item>List of terms</item>
  <item_id>15</item_id>
  <item_description>Another item</item_description>
  <item_attribute>Term</item_attribute>
  <item_attribute_name>Name#1</item_attribute_name>
  <item_attribute_value>Att#1</item_attribute_value>
  <item_attribute_name>Name#2</item_attribute_name>
  <item_attribute_value>Att#2</item_attribute_value>
</documents>

出力

<?xml version="1.0"?>
<documents>
  <document>
    <document_id>REV#1</document_id>
    <items>
      <item>
        <id>10</id>
        <description>Terms used in documents</description>
        <attributes>
          <attribute>
            <name>SA</name>
            <value>Some Attribute</value>
          </attribute>
          <attribute>
            <name>SOA</name>
            <value>Some Other Attribute</value>
          </attribute>
        </attributes>
      </item>
    </items>
  </document>
  <document>
    <document_id>AC-163</document_id>
    <items>
      <item>
        <id>15</id>
        <description>Another item</description>
        <attributes>
          <attribute>
            <name>Name#1</name>
            <value>Att#1</value>
          </attribute>
          <attribute>
            <name>Name#2</name>
            <value>Att#2</value>
          </attribute>
        </attributes>
      </item>
    </items>
  </document>
</documents>
于 2013-02-20T21:10:29.877 に答える