Getting parent element in XSLT

If you want your templates to be atomic (isolated and reusable), you should be referencing a parent node this way. Instead, when calling the template, pass in the reference you want to be able to use. This way you could use this template for the same type of node, even if it has a different context/parent (so long as you can still load the parameter).

<xsl:template match="PurchaseOrder">
    <xsl:apply-templates select="PurchaseOrderLine">
        <xsl:with-param name="PurchaseOrder" select="."/>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="PurchaseOrderLine">
    <xsl:param name="PurchaseOrder"/>
    <!-- I want to get the PurchaseOrder\ID here for the current PurchaseOrder -->
</xsl:template>

Now in your PurchaseOrderLine template, you can reference the $PurchaseOrder variable.


Seems like you have skipped some basic reading on XPath.

<xsl:template match="PurchaseOrderLine">
    <xsl:value-of select="../ID" />
</xsl:template>

Tags:

Xml

Xpath

Xslt