0

類似のレコード(同じUniqueID)をXSLと組み合わせようとしています。

これが私のXMLです:

<ExportXML xmlns="http://www.taleo.com/ws/integration/toolkit/2005/07">
  <record>
    <field name="UniqueID">1234</field>
    <field name="Location">Michigan</field>
    <field name="Category">Math</field>
  </record>
  <record>
    <field name="UniqueID">1234</field>
    <field name="Location">Texas</field>
    <field name="Category">Science</field>
  </record>
  <record>
    <field name="UniqueID">1234</field>
    <field name="Location"></field>
    <field name="Category">History</field>
  </record>
  <record>
    <field name="UniqueID">2345</field>
    <field name="Location">Ohio</field>
    <field name="Category"></field>
  </record>
</ExportXML>

出力を次のように表示します。

<ExportXML>
  <record>
    <field name="UniqueID">1234</field>
    <field name="Location">Michigan, Texas</field>
    <field name="Category">Math, Science, History</field>
  </record>
  <record>
    <field name="UniqueID">2345</field>
    <field name="Location">Ohio</field>
    <field name="Category"></field>
  </record>
</ExportXML>

私は頭が回転するほど多くの異なることを試みました。私はまだこれに不慣れで、学ぶのが難しいと感じています。

私はおそらくかなり離れていますが、これが私がこれまでに持っているものです。まず、フィールドの1つ(カテゴリ)をマージしようとしましたが、マージを試行せずにすべてのレコードを繰り返すだけです...

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:t="http://www.taleo.com/ws/integration/toolkit/2005/07"
exclude-result-prefixes="t">

<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

<xsl:key name="distinctRecord" match="t:record" use="t:field[@name='UniqueID']" />

<xsl:template match="/">

  <xsl:for-each select="//t:record[generate-id(.) = generate-id(key('distinctRecord', t:field[@name='UniqueID'])[1])]">
    <xsl:variable name="UniqueID" select="t:field[@name='UniqueID']" />
    <record>
          <UniqueID><xsl:value-of select="$UniqueID" /></UniqueID>
          <Category>
            <xsl:for-each select="key('distinctRecord', $UniqueID)">
                  <xsl:if test="position() != 1">, </xsl:if>
                  <xsl:value-of select="t:field[@name='Category']"/>
            </xsl:for-each>
          </Category>
    </record>
  </xsl:for-each>

</xsl:template>
</xsl:stylesheet>

どんな助けでも大歓迎です!

ありがとうございました。

4

2 に答える 2

1

投稿された入力サンプルは整形式ではありません。修正すると、

<ExportXML>
  <record>
    <field name="UniqueID">1234</field>
    <field name="Location">Michigan</field>
    <field name="Category">Math</field>
  </record>
  <record>
    <field name="UniqueID">1234</field>
    <field name="Location">Texas</field>
    <field name="Category">Science</field>
  </record>
  <record>
    <field name="UniqueID">1234</field>
    <field name="Location"></field>
    <field name="Category">History</field>
  </record>
  <record>
    <field name="UniqueID">2345</field>
    <field name="Location">Ohio</field>
    <field name="Category"></field>
  </record>
</ExportXML>

次に、投稿されたXSLTは名前空間を使用しますが、入力には名前空間がなく、取得したXSLTを修正します

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

<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

<xsl:key name="distinctRecord" match="record" use="field[@name='UniqueID']" />

<xsl:template match="/">

  <xsl:for-each select="//record[generate-id(.) = generate-id(key('distinctRecord', field[@name='UniqueID'])[1])]">
    <xsl:variable name="UniqueID" select="field[@name='UniqueID']" />
    <record>
          <UniqueID><xsl:value-of select="$UniqueID" /></UniqueID>
          <Category>
            <xsl:for-each select="key('distinctRecord', $UniqueID)">
                  <xsl:if test="position() != 1">, </xsl:if>
                  <xsl:value-of select="field[@name='Category']"/>
            </xsl:for-each>
          </Category>
    </record>
  </xsl:for-each>

</xsl:template>
</xsl:stylesheet>

次に、必要なグループ化が得られると思います。

<record>
   <UniqueID>1234</UniqueID>
   <Category>Math, Science, History</Category>
</record>
<record>
   <UniqueID>2345</UniqueID>
   <Category/>
</record>

次に、XSLTをさらに編集して、ルート要素を作成し、場所を追加できます。

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

<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

<xsl:key name="distinctRecord" match="record" use="field[@name='UniqueID']" />

<xsl:template match="/">
 <ExportXML>
  <xsl:for-each select="//record[generate-id(.) = generate-id(key('distinctRecord', field[@name='UniqueID'])[1])]">
    <xsl:variable name="UniqueID" select="field[@name='UniqueID']" />
    <record>
          <UniqueID><xsl:value-of select="$UniqueID" /></UniqueID>
          <Location><xsl:value-of select="field[@name='Location']"/></Location>
          <Category>
            <xsl:for-each select="key('distinctRecord', $UniqueID)">
                  <xsl:if test="position() != 1">, </xsl:if>
                  <xsl:value-of select="field[@name='Category']"/>
            </xsl:for-each>
          </Category>
    </record>
  </xsl:for-each>
 </ExportXML>
</xsl:template>
</xsl:stylesheet>

したがって、XSLTのメインのグループ化は正しくコーディングされており、整形式のXML入力に適用することを確認する必要があるだけです。

于 2013-02-20T13:50:05.103 に答える
0

この回答は、XSLT 2.0(Saxon 9.0以降)にアクセスできるかどうかによって異なります。私はSaxon9.4.0.4を使用しています。これにより、「グループごと」の使用が可能になります。


<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:t="http://www.taleo.com/ws/integration/toolkit/2005/07"
exclude-result-prefixes="t">

<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="t:ExportXML">
    <ExportXML>
        <xsl:for-each-group select="t:record" group-by="t:field[@name='UniqueID']">
            <record>
                <field name="UniqueID">
                    <xsl:value-of select="t:field[@name='UniqueID']"></xsl:value-of>
                </field>
                <field name="Location">
                    <xsl:for-each select="current-group()">
                        <xsl:if test="position() != 1  and  t:field[@name='Location'] &gt; ' '">,</xsl:if>
                        <xsl:value-of select="t:field[@name='Location']"/>
                    </xsl:for-each>
                </field>
                <field name="Category">
                    <xsl:for-each select="current-group()">
                        <xsl:if test="position() != 1  and  t:field[@name='Category'] &gt; ' '">,</xsl:if>
                        <xsl:value-of select="t:field[@name='Category']"/>
                    </xsl:for-each>
                </field>
            </record>
        </xsl:for-each-group>
    </ExportXML>
</xsl:template>

于 2013-02-20T16:15:08.000 に答える