Search code examples
xmlxsltdtd

How to separate idrefs and show their parent element's attribute value?


Let's say I have a XML file declared with a dtd, for example:

<?xml version="1.0"  encoding="utf-8"?>
<!DOCTYPE box SYSTEM "box.dtd">
<box>
    <title>My Powerful BOX</title>
    <tape_list>
        <tape tid="tr_1" name="tp_2000"/>
        <tape tid="tr_2" name="tp_20230"/>
        <tape tid="tr_3" name="tp_200412"/>
        <tape tid="tr_4" name="tp_202530341"/>
        <tape tid="tr_5" name="tp_20320"/>
        <tape tid="tr_6" name="tp_202340"/>
        <tape tid="tr_7" name="tp_20253"/>
        <tape tid="tr_8" name="tp_202613"/>
        <tape tid="tr_9" name="tp_20234"/>
        <tape tid="tr_10" name="tp_234000"/>
        <tape tid="tr_11" name="tp_202500"/>
        <tape tid="tr_12" name="tp_201200"/>
    </tape_list>
    <item_list>
        <item item_id="No_1">
            <item_info item_name="MY_item_NAME"/>
            <tapes ref_id="tr_10 tr_11 tr_4"/>
        </item>
        <item item_id="No_2">
            <item_info item_name="MY_item_NAME2"/>
            <tapes ref_id="tr_2"/>
        </item>
    </item_list>
</box>

And I'm making a xsl to show a table of items from item_list. I want to show the item_list/item/tapes name from tape_list/tape/@name, but keep in mind that ref_id type is IDREF and tid is unique ID. Something like that:

<--Outside of xsl:template-->
<xsl:key name="tape" match="tape_list/tape" use="@tid"/>
<--------------------------->
                    <table border="1">
                        <xsl:for-each select="box/item_list/item">
                                <tr bgcolor="#9acd32">
                                    <th>title</th>
                                    <th>used tapes</th>
                                </tr>
                                <tr>

                                    <td>
                                        <xsl:value-of select="./item_info/@item_name"/>
                                    </td>
                                    <td>
                                        
                                        <xsl:for-each select="key('tape', tapes/@ref_id)">
                                            <xsl:value-of select="@name"/>
                                        </xsl:for-each>
                                        
                                    </td>
                                </tr>
                        </xsl:for-each>
                    
                    </table>

But this returns info only for item NO_2 and returns nothing for NO_1. My question is why is this happening and how I can show the name for the corresponding id?

P.S: I'm using xsl version 2.0


Solution

  • You did not post the expected result. I am guessing you want to do something like:

    XSLT 2.0

    <xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:key name="tape" match="tape_list/tape" use="@tid"/>
    
    <xsl:template match="/box">
        <html>
            <body>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>title</th>
                        <th>used tapes</th>
                    </tr>
                    <xsl:for-each select="item_list/item">
                        <tr>
                            <td>
                                <xsl:value-of select="item_info/@item_name"/>
                            </td>
                            <td>
                                <xsl:value-of select="key('tape', tokenize(tapes/@ref_id, ' '))/@name" separator=", "/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Given your input, this will produce:

    Result

    <!DOCTYPE HTML><html>
       <body>
          <table border="1">
             <tr bgcolor="#9acd32">
                <th>title</th>
                <th>used tapes</th>
             </tr>
             <tr>
                <td>MY_item_NAME</td>
                <td>tp_202530341, tp_234000, tp_202500</td>
             </tr>
             <tr>
                <td>MY_item_NAME2</td>
                <td>tp_20230</td>
             </tr>
          </table>
       </body>
    </html>
    

    Rendered

    enter image description here