我现有的地图,根据类型和其他一些逻辑,使用地图中的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>
答案 0 :(得分:1)
当这个XSLT 1.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>