问题
I am looking at the Muenchian Grouping. I tried finding examples that are similar to my xml but can't find any. Most of the examples are well structured while mine is confusing.
Here's a shortened version of my XML (note that I can't change the XML structure because it's a standard thing and out of my hands), and I'm using XSLT 1 because the system only supports that version now.
<object>
<creator id="123">
<name>ABC</name>
<city>Hamilton</city>
</creator>
<creator><references>456</references></creator>
<contact><references>123</references></contact>
<creator id="456">
<name>XYZ</name>
<city>New York</city>
</creator>
<associatedParty><references>123</references>
<role>Sponsor</role>
</associatedParty>
</object>
The output that I desire is:
<party id="123">
<name>ABC</name>
<city>Hamilton</city>
<role>Creator</role>
<role>Contact</role>
<role>Sponsor</role>
</party>
<party id="456">
<name>XYZ</name>
<city>New York</city>
<role>Creator</role>
<role>Contact</role>
</party>
Now the id attribute is being used as a value for the references element. And the tag in the output can be either creator, contact, or whatever is inside element if it's under an associatedParty element.
I'm stuck with creating the key to group them from their id/references attribute. As far as I see the examples using xsl:key is only for nodes that have the same name, and the example I posted have different node names. Any help would be appreciated!!!!
回答1:
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kRefByVal" match="references"
use="."/>
<xsl:key name="kCreatorById" match="creator"
use="@id"/>
<xsl:key name="kRoleNameByRef"
match="*[not(self::associatedParty
or
self::creator
)
]"
use="references"/>
<xsl:key name="kAssocByRef"
match="associatedParty"
use="references"/>
<xsl:template match="/">
<xsl:variable name="vReferences" select=
"*/*/references
[generate-id()
=
generate-id(key('kRefByVal',.)[1])
]
"/>
<xsl:apply-templates select="$vReferences">
<xsl:sort select="." data-type="number"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="references" priority="3">
<party id="{.}">
<xsl:copy-of select="key('kCreatorById',.)/*"/>
<xsl:apply-templates select=
"key('kCreatorById',.)"/>
<xsl:apply-templates select=
"key('kRoleNameByRef',.)"/>
<xsl:copy-of select="key('kAssocByRef',.)/role"/>
</party>
</xsl:template>
<xsl:template match="*[not(self::associatedParty)]">
<role>
<xsl:value-of select="name()"/>
</role>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<object>
<creator id="123">
<name>ABC</name>
<city>Hamilton</city>
</creator>
<creator>
<references>456</references>
</creator>
<contact>
<references>123</references>
</contact>
<creator id="456">
<name>XYZ</name>
<city>New York</city>
</creator>
<associatedParty>
<references>123</references>
<role>Sponsor</role>
</associatedParty>
</object>
produces the wanted, correct result:
<party id="123">
<name>ABC</name>
<city>Hamilton</city>
<role>creator</role>
<role>contact</role>
<role>Sponsor</role>
</party>
<party id="456">
<name>XYZ</name>
<city>New York</city>
<role>creator</role>
</party>
回答2:
You can use this template:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="//creator[not(references)]"/>
</xsl:template>
<xsl:template match="creator">
<party id="{@id}">
<xsl:copy-of select="name"/>
<xsl:copy-of select="city"/>
<role>Creator</role>
<xsl:apply-templates select="../*[not(self::creator) and references = current()/@id]"/>
</party>
</xsl:template>
<xsl:template match="associatedParty" priority="1">
<xsl:copy-of select="role"/>
</xsl:template>
<xsl:template match="*[references]">
<role>
<xsl:value-of select="name()"/>
</role>
</xsl:template>
</xsl:stylesheet>
Output:
<party id="123">
<name>ABC</name>
<city>Hamilton</city>
<role>Creator</role>
<role>contact</role>
<role>Sponsor</role>
</party>
<party id="456">
<name>XYZ</name>
<city>New York</city>
<role>Creator</role>
</party>
来源:https://stackoverflow.com/questions/7061614/xslt-1-0-grouping-key-for-different-nodes-and-elements