将类似的XML记录/字段与XSL合并 [英] Combine similar XML records/fields with XSL
问题描述
我正在尝试将类似的记录(相同的UniqueID)与XSL合并.
I am trying to combine similar records (same UniqueID) with 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>
这是我希望输出看起来像的样子:
Here's what I'd like the output to look like:
<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>
我尝试了很多不同的事情,以至于我的头在旋转.我对此还很陌生,发现很难学习.
I've tried so many different things that my head is spinning. I'm still new to this, and I'm finding it difficult to learn.
我可能已经走了,但是到目前为止,这就是我所拥有的.首先,我只是尝试合并一个字段(Category),但它只是重复所有记录而不尝试合并...
I'm probably way off, but here's what I have so far. To start I was just trying to merge one of the fields (Category) but it just repeats all the records without attempting to merge...
<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>
任何帮助将不胜感激!
谢谢.
推荐答案
您发布的输入示例格式不正确,更正后得到
Well your posted input sample is not well-formed, when correcting it I get
<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
then your posted XSLT does use a namespace although the input does not have any, correcting the XSLT I get
<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>
然后,我认为您会得到所需的分组:
then I think you get the grouping you want:
<record>
<UniqueID>1234</UniqueID>
<Category>Math, Science, History</Category>
</record>
<record>
<UniqueID>2345</UniqueID>
<Category/>
</record>
然后您可以进一步编辑XSLT以创建根元素并添加位置,例如
You can then further edit the XSLT to create a root element and to add the location e.g.
<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输入即可.
So the main grouping in your XSLT is correctly coded, only you need to make sure you apply it to a well-formed XML input.
这篇关于将类似的XML记录/字段与XSL合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!