|
Subject:
|
XPath help
|
|
Posted By:
|
migake
|
Post Date:
|
4/21/2006 10:14:32 AM
|
Given the next XML file, I need to generate the output shown by the edge tag. The number in the antecedent attribute refers to the id in the itemset tag.
<PMML> <AssociationModel> <Item id=”1” value=”wine”/> <Item id=”2” value=”fish”/> <Item id=”3” value=”beer”/>
<Itemset id=”5” numberOfItems=”2”> <ItemRef itemRef=”2”/> <ItemRef itemRef=”3”/> </Itemset> <Itemset id=”6” numberOfItems=”1”> <ItemRef itemRef=”1”/> </Itemset>
<AssociationRule id=”12” antecedent=”6” consequent=”5”/> </AssociationModel> </PMML>
<edge id=”12” source=”1” target=”2,3”> (correct) <edge id=”12” source=”6” target=”5”> (incorrect)
Is it possible to navigate in the XML document with XPath expressions made in the "mirror template" and return the value in itemref and copy it in the antecedent attribute of association rule? Or exist another way or syntax? Thanks for your help.
<xsl:template match=”Itemset”> <xsl:apply-templates/> </xsl:template>
<xsl:template match=”itemref”> <xsl:value-of select=”@itemRef”/> </xsl:template>
<xsl:template match=”rule”> <xsl:call-template name=”mirror”> <xsl:with-param name=”data”> <xsl:value-of select=”@antecedent”/> </xsl:with-param> </xsl:call-template> <xsl:element name=”edge”> <xsl:attribute name=”id”> <!-- get value from mirror -- > </xsl:attribute> </xsl:element> </xsl:template>
<xsl:template match=”mirror”> <xsl:param name=”data”> <!-- more instructions -- > </xsl:template>
|
|
Reply By:
|
mhkay
|
Reply Date:
|
4/21/2006 10:24:31 AM
|
The best way to do this sort of thing is with keys. For example if you define the key
<xsl:key name="ItemKey" match="Item" use="@id"/>
then the function call
key('ItemKey', @itemRef)
will find the Item with id=2 when the context node is the first ItemRef in your data sample.
Michael Kay http://www.saxonica.com/ Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|
Reply By:
|
migake
|
Reply Date:
|
4/21/2006 10:49:06 AM
|
Thanks Michael, I'll try it in that way.
|