Caroline
Caroline

Reputation: 171

xsl fo matching attribute values

Given XML:

<expdur-entry><itemno id="exp-item1">1</itemno><maintenance lvl="f"/>
<nsn><fsc>1130</fsc><niin>00-2X3-1</niin></nsn><name>Beeswax</name><desc></desc><partno>Pure Beeswax</partno><cageno>X1148</cageno><ui>LB</ui></expdur-entry>

<expdur-entry><itemno id="exp-item2">2</itemno><maintenance lvl="f"/><nsn><fsc>X3X0</fsc>
<niin>00-221-082</niin></nsn><name>Cloth, Abrasive</name><desc></desc><partno>L9-C-4X8</partno><cageno>81348</cageno>
<ui>EA</ui></expdur-entry>

<expdur-entry><itemno id="exp-item6">3</itemno>
<maintenance lvl="f"/><nsn><fsc>130</fsc><niin>00-13-1802</niin></nsn>
<name>Cloth, Duck</name><desc></desc><partno>L9-C-433XCL 2 FGX04</partno><cageno></cageno><ui>YD</ui></expdur-entry>

<expdur-entry><itemno id="exp-item49">4</itemno><maintenance lvl="f"/>
<nsn><fsc>2X81</fsc><niin>01-331-2212</niin></nsn><name>Tape, Textile,
Nylon</name><desc></desc><partno>123-T-X38</partno>
<cageno></cageno><ui>YD</ui></expdur-entry>

<expdur-entry>
<itemno id="exp-item46">5</itemno><maintenance lvl="f"/><nsn><fsc></fsc><niin></niin></nsn><name>Cloth, Nylon Diamond Weave</name><desc></desc><partno>11-1-13</partno>
<cageno></cageno><ui></ui></expdur-entry>

<expdur-entry><itemno id="exp-item45">6</itemno><maintenance lvl="f"/><nsn><fsc></fsc><niin></niin></nsn>
<name>Cloth, Parachute, Nylon</name><desc></desc>
<partno>L9-C-4438, TY V</partno><cageno></cageno><ui></ui>
</expdur-entry>

<expdur-entry><itemno id="exp-item117">7</itemno>
<maintenance lvl="f"/><nsn><fsc>TBD</fsc><niin></niin></nsn><name>Cord, Nylon, Type X1</name><desc></desc><partno>L9-C-X1X</partno>
<cageno></cageno><ui>YD</ui></expdur-entry>

and

<bulk_itemswp>
<pi.item><qty>.08 YD</qty><common_part_ref idref="exp-item49"/>
</pi.item></bulk_itemswp>

I'm on pi.item and I want to pull <fsc> and <niin> from the <expdur-entry> with the <itemno> whose @id matches the @idref from <common_part_ref>. In this case it's <itemno id="exp-item49">

I thought I could do something like this in the <pi.item> template:

<xsl:apply-templates select="//expdur-entry/itemno[@id=common_part_ref/@idref]" mode="bulk">

but that didn't work. The XSL-FO below works, but I doubt it's the best way. I would prefer to correct the syntax of the select clause above, if possible.

Here is my XSLFO.

<xsl:template match="pi.item">
        <fo:table-row>
            <xsl:choose>
            <xsl:when test="common_part_ref">
                <xsl:apply-templates select="//expdur-entry/itemno" mode="bulk">
                <xsl:with-param name="id">
                    <xsl:value-of select="common_part_ref/@idref"/>
                </xsl:with-param>
                </xsl:apply-templates>
            </xsl:when>
            <xsl:otherwise />
            </xsl:choose>
        </fo:table-row>
    </xsl:template>

<xsl:template match="expdur-entry/itemno" mode="bulk">
<xsl:param name="id"/>
    <xsl:if test="@id = $id">
    <fo:table-cell>
        <fo:block>
            <xsl:value-of disable-output-escaping="no" select="../nsn/fsc"/>
            <xsl:text disable-output-escaping="no">-</xsl:text>
            <xsl:value-of disable-output-escaping="no" select="normalize-space(../nsn/niin)"/>
        </fo:block>
    </fo:table-cell>
    </xsl:if>
</xsl:template>

Upvotes: 0

Views: 299

Answers (2)

Daniel Haley
Daniel Haley

Reputation: 52888

Another option is to use an xsl:key...

<xsl:key name="expdurByID" match="expdur-entry" use="itemno/@id"/>

<xsl:template match="pi.item">
  <fo:table-row>
    <xsl:apply-templates 
      select="key('expdurByID',common_part_ref/@idref)" mode="bulk"/>
  </fo:table-row>
</xsl:template>

<xsl:template match="expdur-entry" mode="bulk">
  <fo:table-cell>
    <fo:block>
      <xsl:value-of select="concat(normalize-space(nsn/fsc),
        '-',normalize-space(nsn/niin))"/>
    </fo:block>
  </fo:table-cell>
</xsl:template>

Upvotes: 0

Tomalak
Tomalak

Reputation: 338426

The current() XSLT function solves precisely this problem:

<xsl:template match="pi.item">
    <fo:table-row>
        <xsl:apply-templates select="//expdur-entry/itemno[
            @id = current()/common_part_ref/@idref
        ]" mode="bulk" />
    </fo:table-row>
</xsl:template>

<xsl:template match="expdur-entry/itemno" mode="bulk">
    <fo:table-cell>
        <fo:block>
            <xsl:value-of select="concat(../nsn/fsc, '-', normalize-space(../nsn/niin))" />
        </fo:block>
    </fo:table-cell>
</xsl:template>

Upvotes: 1

Related Questions