XSLT using lookup table

Here should go questions about transforming XML with XSLT and FOP.
cdeiter
Posts: 3
Joined: Tue Mar 30, 2021 11:00 pm

XSLT using lookup table

Post by cdeiter » Tue Mar 30, 2021 11:12 pm

Hello,
I am a beginner writing XSLTs and I am stumped with writing one that refs to a lookup table and returns a result. I have the following:
<mrp><mrp-setup-item>
<name>Rivet</name>
<qty>2</qty>
<itemref>
<xref wpid="S20002#ROW015" itemid="ROW015"/>
</itemref>
</mrp-setup-item>
<mrp-setup-item>
<name>Washer</name>
<qty>4</qty>
<itemref>
<xref wpid="S20002#ROW083" itemid="ROW083"/>
</itemref>
</mrp-setup-item>
I need to grab the xref itemid attribute, find it in my lookup.xml and return the child of itemid in the lookup.xml into the attribute.
My lookup.xml looks like this:
<item><rowitem><id>ROW015</id><itemid>MRP_NSN_5320-01-080-9250</itemid></rowitem></item>
<item><rowitem><id>ROW016</id><itemid>MRP_NSN_5315-01-210-7570</itemid></rowitem></item>
<item><rowitem><id>ROW017</id><itemid>MRP_NSN_5315-00-240-0997</itemid></rowitem></item>
<item><rowitem><id>ROW018</id><itemid>MRP_NSN_5315-00-902-8352</itemid></rowitem></item>
<item><rowitem><id>ROW019</id><itemid>MRP_NSN_5315-00-298-9845</itemid></rowitem></item>
<item><rowitem><id>ROW019-10</id><itemid>MRP_NSN_5315-00-882-1438</itemid></rowitem></item>
<item><rowitem><id>ROW020</id><itemid>MRP_NSN_5315-00-823-8746</itemid></rowitem></item>
<item><rowitem><id>ROW021</id><itemid>MRP_NSN_5315-00-058-9756</itemid></rowitem></item>

Currently, my XSLT has this written and it returns an empty itemid attribute.
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:variable name="MRPITEMS">file:///C:/BAE/FAASV/Maintwp/mrp_Lookup.xml</xsl:variable>
<xsl:variable name="MRPnum" select="@itemid"></xsl:variable>


<xsl:template match="mrp-setup-item">
<xsl:choose>
<xsl:when test="contains(xref/@itemid,ROW)"></xsl:when>
<mrp-setup-item>
<xsl:copy-of select="name"></xsl:copy-of>
<xsl:copy-of select="qty"></xsl:copy-of>
<itemxref><xref>
<xsl:attribute name="wpid">S20006-9-2350-372</xsl:attribute>
<xsl:attribute name="itemid"><xsl:value-of select="document($MRPITEMS)//child::id=$MRPnum/child::itemid"/>"</xsl:attribute>
</xref></itemxref>
</mrp-setup-item>
<xsl:otherwise/>
</xsl:choose>
</xsl:template>
I look forward to any help I receive.
Cathy

tavy
Posts: 275
Joined: Thu Jul 01, 2004 12:29 pm

Re: XSLT using lookup table

Post by tavy » Mon Apr 05, 2021 11:44 am

Hello Cathy,

I think a possible solution is to have a stylesheet something like this:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:variable name="MRPITEMS" select="document('mrp_Lookup.xml')//rowitem"/>

    <xsl:template match="mrp-setup-item">
        <xsl:variable name="itemid" select="itemref/xref/@itemid"/>
        <xsl:choose>
            <xsl:when test="contains($itemid, 'ROW')">
                <mrp-setup-item>
                    <xsl:copy-of select="name"/>
                    <xsl:copy-of select="qty"/>
                    <itemxref>
                        <xref>
                            <xsl:attribute name="wpid">S20006-9-2350-372</xsl:attribute>
                            <xsl:attribute name="itemid" select="$MRPITEMS[id/text() = $itemid]/itemid/text()" />
                        </xref>
                    </itemxref>
                </mrp-setup-item>
            </xsl:when>
            <xsl:otherwise/>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
For "MRPITEMS" I think would be better to use maps. You can fond more details here:
https://www.saxonica.com/html/documenta ... s/map.html
https://www.w3.org/TR/xslt-30/#element-map

I recommend you to address this questions on the XSLT list (xsl-list at lists.mulberrytech dot com), there are more people to answer there.

Best Regards,
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com

cdeiter
Posts: 3
Joined: Tue Mar 30, 2021 11:00 pm

Re: XSLT using lookup table

Post by cdeiter » Mon Apr 05, 2021 3:57 pm

I will try this and if that doesn't work, I will look on the mulberry page. Thank you very much for the response.
Cathy

cdeiter
Posts: 3
Joined: Tue Mar 30, 2021 11:00 pm

Re: XSLT using lookup table

Post by cdeiter » Tue Apr 06, 2021 7:02 pm

Thank you Octavian, that worked perfectly. I am having difficulties with another one if you don't mind helping. I did search the xsl:lists link you sent and I am not seeing anything. I need to lookup up an old file number based off of an attribute, look at the lookup file and pull in the new number.

I have something like this
<proc>
<title>FOLLOW-ON MAINTENANCE</title>
<step1 id="M20340-03.01.01">
<para> Install access cover. <xref wpid="M20236" pretext="(" posttext=")"/>
</para>
</step1>
<step1 id="M20340-03.01.02">
<para> Fill cooling system to proper fluid level. <xref wpid="M20333" pretext="(" posttext=")"/>
</para>
</step1>

My Lookup file is like this:
<row><NewFile id="M20236">M20016-9-2350-372</Newfile></row>
<row><NewFile id="M20234">M20381-9-2350-372</Newfile></row>
<row><NewFile id="M20232">M20017-9-2350-372</Newfile></row>

This is my XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:include href="copy.xsl"/>

<xsl:template match="xref">
<xsl:variable name="OLDFILE"><xsl:value-of select="./@wpid"/></xsl:variable>
<xsl:variable name="NEWFILE">file:///C:/BAE/FAASV/OldnumberFAASV/Newfile_lookup.xml</xsl:variable>
<xref>
<xsl:attribute name="wpid">
<xsl:choose>
<xsl:when test="$OLDFILE = document($NEWFILE)//NewFile/@id">
<xsl:value-of select="document($NEWFILE)//NewFile[@id=$OLDFILE]"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."></xsl:copy-of>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xref>
Currently it is only returning it's original value, not the new file number.

tavy
Posts: 275
Joined: Thu Jul 01, 2004 12:29 pm

Re: XSLT using lookup table

Post by tavy » Wed Apr 07, 2021 9:45 am

Hello Cathy,

For me it works if I have the stylesheet something like this:

Code: Select all

 <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output method="xml" indent="yes"/>
    <!--<xsl:include href="copy.xsl"/>-->
    
    <xsl:template match="xref">
        <xsl:variable name="OLDFILE"><xsl:value-of select="@wpid"/></xsl:variable>
        <xsl:variable name="NEWFILE">Newfile_lookup.xml</xsl:variable>
        <xref>
            <xsl:attribute name="wpid">
                <xsl:choose>
                    <xsl:when test="$OLDFILE = document($NEWFILE)//NewFile/@id">
                        <xsl:value-of select="document($NEWFILE)//NewFile[@id=$OLDFILE]"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy-of select="$OLDFILE"></xsl:copy-of>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
        </xref>
    </xsl:template>
</xsl:stylesheet>
Please note that I commented the include of copy.xsl. Maybe you have something there that does not work.

For the XSL List, you can register and ask the questions there. I am also registered on that list, and I can answer also there.

Best Regards,
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com

Post Reply