我现有的映射,根据类型和许多其他逻辑,使用映射中的functoid将一个人从源架构转换为一个人从目标架构。由于目标模式最近发生了变化,我需要修改转换以使用密钥(guid)节点,这样我们就可以对数据进行分组。
我想实现一个内联XSLT脚本,用于类似//Person*/[Key=same]的内容,但不确定是否使用XSLT。
示例输入:
<Person>
<PersonType>PartnerParentsFarther</PersonType>
<Name>
<title>MR</title>
<Gender>MALE</Gender>
...
</Name>
<Address/>
<PersonKey>
<Key>a1093c4a-7c53-4de5-ad30-cb4140aec58</Key>
<ParentKey/>
</PersonKey>
</Person>
<Person>
<PersonType>PartnerParentsMother</PersonType>
<Name>
<title>MRS</title>
<Gender>FEMALE</Gender>
...
</Name>
<Address/>
<PersonKey>
<Key>a1093c4a-7c53-4de5-ad30-cb4140aec58</Key>
<ParentKey/>
</PersonKey>
</Person>
<Person>
<PersonType>PartnerParentsFarther</PersonType>
<Name>
<title>MR</title>
<Gender>MALE</Gender>
...
</Name>
<Address/>
<PersonKey>
<Key>a1093c4a-7c53-4de5-ad30-cb4140aec59</Key>
<ParentKey/>
</PersonKey>
</Person>
<Person>
<PersonType>PartnerParentsMother</PersonType>
<Name>
<title>MRS</title>
<Gender>FEMALE</Gender>
...
</Name>
<Address/>
<PersonKey>
<Key>a1093c4a-7c53-4de5-ad30-cb4140aec59</Key>
<ParentKey/>
</PersonKey>
</Person>
当前输出示例:
<PartnerParents>
<Father>
<Title>MR</Title>
<Gender>MALE</Gender>
</Father>
<Father>
<Title>MR</Title>
<Gender>MALE</Gender>
</Father>
<Mother>
<Title>MRS</Title>
<Gender>FEMALE</Gender>
</Mother>
<Mother>
<Title>MRS</Title>
<Gender>FEMALE</Gender>
</Mother>
</PartnerParents>
基于相同键值的所需输出示例:
<PartnerParents>
<Father>
<Title>MR</Title>
<Gender>MALE</Gender>
</Father>
<Mother>
<Title>MRS</Title>
<Gender>FEMALE</Gender>
</Mother>
</PartnerParents>
<PartnerParents>
<Father>
<Title>MR</Title>
<Gender>MALE</Gender>
</Father>
<Mother>
<Title>MRS</Title>
<Gender>FEMALE</Gender>
</Mother>
</PartnerParents>
当这个XSLT1.0解决方案:
<xsl:key name="groups" match="Test/Person/PersonKey/Key/text()" use="."/>
<xsl:template match="/">
<xsl:apply-templates select="/Test" />
</xsl:template>
<xsl:template match="/Test">
<Test>
<xsl:for-each select="Person/PersonKey/Key/text()[generate-id()=generate-id(key('groups',.))]">
<PartnerParents>
<Father>
<xsl:for-each select="key('groups',.)">
<xsl:if test="../../../Name/Gender/text() = 'MALE'">
<Title>
<xsl:value-of select="../../../Name/title/text()" />
</Title>
<Gender>
<xsl:value-of select="../../../Name/Gender/text()" />
</Gender>
</xsl:if>
</xsl:for-each>
</Father>
<Mother>
<xsl:for-each select="key('groups',.)">
<xsl:if test="../../../Name/Gender/text() = 'FEMALE'" >
<Title>
<xsl:value-of select="../../../Name/title/text()" />
</Title>
<Gender>
<xsl:value-of select="../../../Name/Gender/text()" />
</Gender>
</xsl:if>
</xsl:for-each>
</Mother>
</PartnerParents >
</xsl:for-each>
</Test>
应用于以下XML:
<Test>
<Person>
<PersonType>PartnerParentsFarther</PersonType>
<Name>
<title>MR</title>
<Gender>MALE</Gender>
</Name>
<Address/>
<PersonKey>
<Key>a1093c4a-7c53-4de5-ad30-cb4140aec58</Key>
<ParentKey/>
</PersonKey>
</Person>
<Person>
<PersonType>PartnerParentsMother</PersonType>
<Name>
<title>MRS</title>
<Gender>FEMALE</Gender>
</Name>
<Address/>
<PersonKey>
<Key>a1093c4a-7c53-4de5-ad30-cb4140aec58</Key>
<ParentKey/>
</PersonKey>
</Person>
<Person>
<PersonType>PartnerParentsFarther</PersonType>
<Name>
<title>MR</title>
<Gender>MALE</Gender>
</Name>
<Address/>
<PersonKey>
<Key>a1093c4a-7c53-4de5-ad30-cb4140aec59</Key>
<ParentKey/>
</PersonKey>
</Person>
<Person>
<PersonType>PartnerParentsMother</PersonType>
<Name>
<title>MRS</title>
<Gender>FEMALE</Gender>
</Name>
<Address/>
<PersonKey>
<Key>a1093c4a-7c53-4de5-ad30-cb4140aec59</Key>
<ParentKey/>
</PersonKey>
</Person>
</Test>
产生想要的结果:
<Test>
<PartnerParents>
<Father>
<Title>MR</Title>
<Gender>MALE</Gender>
</Father>
<Mother>
<Title>MRS</Title>
<Gender>FEMALE</Gender>
</Mother>
</PartnerParents>
<PartnerParents>
<Father>
<Title>MR</Title>
<Gender>MALE</Gender>
</Father>
<Mother>
<Title>MRS</Title>
<Gender>FEMALE</Gender>
</Mother>
</PartnerParents>
</Test>