9

I have an XML like this:

<PurchaseOrder>
    <ID>1</ID>
    <PurchaseOrderLine>
        <DATA>100<DATA>
    </PurchaseOrderLine>
    <PurchaseOrderLine>
        <DATA>200<DATA>
    </PurchaseOrderLine>
    <PurchaseOrderLine>
        <DATA>300<DATA>
    </PurchaseOrderLine>
</PurchaseOrder>
<PurchaseOrder>
    <ID>2</ID>
    <PurchaseOrderLine>
        <DATA>100<DATA>
    </PurchaseOrderLine>
    <PurchaseOrderLine>
        <DATA>200<DATA>
    </PurchaseOrderLine>
    <PurchaseOrderLine>
        <DATA>300<DATA>
    </PurchaseOrderLine>
</PurchaseOrder>
<PurchaseOrder>
    <ID>3</ID>
    <PurchaseOrderLine>
        <DATA>100<DATA>
    </PurchaseOrderLine>
    <PurchaseOrderLine>
        <DATA>200<DATA>
    </PurchaseOrderLine>
    <PurchaseOrderLine>
        <DATA>300<DATA>
    </PurchaseOrderLine>
</PurchaseOrder>

and XSL:

<xsl:template match="PurchaseOrder">
    <xsl:apply-templates select="PurchaseOrderLine"/>
</xsl:template>

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

How can I get current parent element value (PurchaseOrder\ID) in PurchaseOrderLine?

3 Answers 3

11

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.

Sign up to request clarification or add additional context in comments.

Comments

5

Seems like you have skipped some basic reading on XPath.

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

Comments

0

Not sure if this is where you are going but you can match the Parent node by doing the following, this checks if the parent node has the Child node.

<xsl:template match="//*[PurchaseOrderLine]>
    <!--- Do you stuff here with parent context--->
</xsl:template>

With this you can do several things, you can select a PurchaseOrderLine with an ID and Data value.

<xsl:template match="//PurchaseOrder[ID=3 and PurchaseOrderLine/DATA=100]">
     <!--- Do stuff with parent that has the ID of 3 And the DATA of 200 --->
</xsl:template>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.