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>

我尝试了很多不同的东西,我的头都在旋转。我还是新手,我发现很难学习。

我可能很遥远,但这就是我到目前为止所拥有的。一开始我只是试图合并其中一个字段(类别),但它只是重复所有记录而不尝试合并......

<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 或更高版本)我使用的是 Saxon 9.4.0.4。这允许使用“for each group”。


<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 回答