感谢您抽出宝贵时间来查看我的问题。
我有一个输入结构(下图),其中每一行代表从一个位置项到另一个位置项的移动。因此,每一行都有一个位置和一个位置。
目标是嵌套的xml,其中location对象具有一些标题和项级别信息。我需要从所有行中的位置选择distint并映射其项目级别详细信息。
输入示例和所需的转换输出。
我有一个类似于
的平面数据库提取xml<Data>
<row>
<Movement>Home to Work</Movement>
<COMP_ID_F>HID001</COMP_ID_F>
<ITEM_ID_F>1</ITEM_ID_F>
<FIELDS_F> More home related data </FIELDS_F>
<COMP_ID_T>WID001</COMP_ID_T>
<ITEM_ID_T>2</ITEM_ID_T>
<FIELDS_T> More work related data</FIELDS_T>
</row>
<row>
<Movement>Home to Work</Movement>
<COMP_ID_F>HID001</COMP_ID_F>
<ITEM_ID_F>1</ITEM_ID_F>
<FIELDS_F> More home related data </FIELDS_F>
<COMP_ID_T>WID001</COMP_ID_T>
<ITEM_ID_T>2</ITEM_ID_T>
<FIELDS_T> More work related data</FIELDS_T>
</row>
<row>
<Movement>Home to Work</Movement>
<COMP_ID_F>HID001</COMP_ID_F>
<ITEM_ID_F>1</ITEM_ID_F>
<FIELDS_F> More home related data </FIELDS_F>
<COMP_ID_T>WID001</COMP_ID_T>
<ITEM_ID_T>3</ITEM_ID_T>
<FIELDS_T> More work related data</FIELDS_T>
</row>
<row>
<Movement>Home to Work</Movement>
<COMP_ID_F>HID001</COMP_ID_F>
<ITEM_ID_F>2</ITEM_ID_F>
<FIELDS_F> More home related data </FIELDS_F>
<COMP_ID_T>WID001</COMP_ID_T>
<ITEM_ID_T>4</ITEM_ID_T>
<FIELDS_T> More work related data</FIELDS_T>
</row>
<row>
<Movement>Home to Work</Movement>
<COMP_ID_F>HID002</COMP_ID_F>
<ITEM_ID_F>1</ITEM_ID_F>
<FIELDS_F> More home related data </FIELDS_F>
<COMP_ID_T>WID001</COMP_ID_T>
<ITEM_ID_T>5</ITEM_ID_T>
<FIELDS_T> More work related data</FIELDS_T>
</row>
<row>
<Movement>Home to Work</Movement>
<COMP_ID_F>HID002</COMP_ID_F>
<ITEM_ID_F>2</ITEM_ID_F>
<FIELDS_F> More home related data </FIELDS_F>
<COMP_ID_T>WID002</COMP_ID_T>
<ITEM_ID_T>1</ITEM_ID_T>
<FIELDS_T> More work related data</FIELDS_T>
</row>
</Data>
转换后的输出需要
<Target>
<Events>
<Movement>Home to Work</Movement>
<Movement>Home to Work</Movement>
<Movement>Home to Work</Movement>
<Movement>Home to Work</Movement>
<Movement>Home to Work</Movement>
<Movement>Home to Work</Movement>
</Events>
<Locations>
<Home>
<id>HID001</id>
<item>
<id>1</id>
</item>
<item>
<id>2</id>
</item>
</Home>
<Home>
<id>HID002</id>
<item>
<id>1</id>
</item>
<item>
<id>2</id>
</item>
</Home>
<Work>
<id>WID001</id>
<item>
<id>1</id>
</item>
<item>
<id>2</id>
</item>
<item>
<id>3</id>
</item>
<item>
<id>4</id>
</item>
<item>
<id>5</id>
</item>
</Work>
<Work>
<id>WID002</id>
<item>
<id>1</id>
</item>
</Work>
</Locations>
</Target>
现在,我已经尝试了许多使用Keys并生成id的选项。所以,请看看我指出我出错的地方。或者,如果有更好的方法来实现此输出。
我创建了4个键
<xsl:key name="fcreg" match="row" use="COMP_ID_F"/>
<xsl:key name="fcireg" match="row" use="ITEM_ID_F"/>
<xsl:key name="tcreg" match="row" use="COMP_ID_T"/>
<xsl:key name="tcireg" match="row" use="ITEM_ID_T"/>
我的XSLT执行以下逻辑
<Target>
<!--transform all events-->
<Events>
<xsl:for-each select="Data/row">
<!-- get movement-->
</xsl:for-each>
</Events>
<!--transform all from locations-->
<xsl:for-each select="Data/row[generate-id() = generate-id(key('fcreg',COMP_ID_F)[1])]">
<xsl:for-each select="key('fcreg',COMP_ID_F)[generate-id() = generate-id(key('fcireg', ITEM_ID_F)[COMP_ID_F=current()/COMP_ID_F][1])]">
<xsl:variable name="items" select="key('fcreg', COMP_ID_F)[ITEM_ID_F = current()/ITEM_ID_F]"/>
<home>
<xsl:value-of select="COMP_ID_F"/>
<xsl:for-each select="$items">
<item>
<id>
<xsl:value-of select="ITEM_ID_F"/>
</id>
</item>
</xsl:for-each>
</home>
</xsl:for-each>
</xsl:for-each>
<!--transform all to locations-->
<xsl:for-each select="Data/row[generate-id() = generate-id(key('tcreg',COMP_ID_T)[1])]">
<xsl:for-each select="key('tcreg',COMP_ID_T)[generate-id() = generate-id(key('tcireg', ITEM_ID_T)[COMP_ID_T=current()/COMP_ID_T][1])]">
<xsl:variable name="items" select="key('tcreg', COMP_ID_T)[ITEM_ID_T = current()/ITEM_ID_T]"/>
<work>
<xsl:value-of select="COMP_ID_T"/>
<xsl:for-each select="$items">
<item>
<id>
<xsl:value-of select="ITEM_ID_T"/>
</id>
</item>
</xsl:for-each>
</work>
</xsl:for-each>
</xsl:for-each>
</Target>
我哪里错了?非常感谢任何帮助。
提前致谢。
答案 0 :(得分:1)
看起来您正在尝试获取不同的COMP_ID_F
元素,并且对于每个不同的值,您希望列出与其关联的所有不同ITEM_ID_F
。
您的fcreg
密钥很好,但因为ITEM_ID_F
项取决于COMP_ID_F
的值,您需要fcireg
的连锁密钥
<xsl:key name="fcireg" match="row" use="concat(COMP_ID_F, '|', ITEM_ID_F)"/>
所以,你可以像以前一样选择ITEM_ID_F
......
<xsl:for-each select="Data/row[generate-id() = generate-id(key('fcreg',COMP_ID_F)[1])]">
但在此范围内,您可以为当前ITEM_ID_F
元素选择不同的COMP_ID_F
项。这是这样做的:
<xsl:for-each select="Data/row[generate-id() = generate-id(key('fcreg',COMP_ID_F)[1])]">
<home>
<id><xsl:value-of select="COMP_ID_F" /></id>
<xsl:for-each select="key('fcreg',COMP_ID_F)[generate-id() = generate-id(key('fcireg',concat(COMP_ID_F, '|', ITEM_ID_F))[1])]">
<item>
<id>
<xsl:value-of select="ITEM_ID_F"/>
</id>
</item>
</xsl:for-each>
</home>
</xsl:for-each>
对于“T”元素,你会有类似的元素
试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:key name="fcreg" match="row" use="COMP_ID_F"/>
<xsl:key name="fcireg" match="row" use="concat(COMP_ID_F, '|', ITEM_ID_F)"/>
<xsl:key name="tcreg" match="row" use="COMP_ID_T"/>
<xsl:key name="tcireg" match="row" use="concat(COMP_ID_T, '|', ITEM_ID_T)"/>
<xsl:template match="/">
<Target>
<Events>
<xsl:copy-of select="Data/row/Movement" />
</Events>
<xsl:for-each select="Data/row[generate-id() = generate-id(key('fcreg',COMP_ID_F)[1])]">
<Home>
<id><xsl:value-of select="COMP_ID_F" /></id>
<xsl:for-each select="key('fcreg',COMP_ID_F)[generate-id() = generate-id(key('fcireg',concat(COMP_ID_F, '|', ITEM_ID_F))[1])]">
<item>
<id>
<xsl:value-of select="ITEM_ID_F"/>
</id>
</item>
</xsl:for-each>
</Home>
</xsl:for-each>
<xsl:for-each select="Data/row[generate-id() = generate-id(key('tcreg',COMP_ID_T)[1])]">
<Work>
<id><xsl:value-of select="COMP_ID_T" /></id>
<xsl:for-each select="key('tcreg',COMP_ID_T)[generate-id() = generate-id(key('tcireg',concat(COMP_ID_T, '|', ITEM_ID_T))[1])]">
<item>
<id>
<xsl:value-of select="ITEM_ID_T"/>
</id>
</item>
</xsl:for-each>
</Work>
</xsl:for-each>
</Target>
</xsl:template>
</xsl:stylesheet>