简体   繁体   中英

XSLT transforming an XML with references

I'm pretty new to xml & xslt and programming in general. I'm trying to transform an xml-file with xslt. My xml-file should reflect this ERD . In order to design the relationships between the entities I decided to use some id's. Here is my xml-code:

<?xml version="1.0" encoding= "UTF-8"?>
<data>
  <aisles>
    <aisle idaisle="A">
      <letteraisle>A</letteraisle>
    </aisle>
    <aisle idaisle="B">
      <letteraisle>B</letteraisle>
    </aisle>
  </aisles>
  <shelves>
    <shelf idshelf = "A1">
      <numbershelf>1</numbershelf>
      <lettershelf>A</lettershelf>
      <aisleid idaisle="A"/>
    </shelf>
    <shelf idshelf = "A2">
      <numbershelf>2</numbershelf>
      <lettershelf>A</lettershelf>
      <aisleid idaisle="A"/>
    </shelf>
    <shelf idshelf = "B1">
      <numbershelf>1</numbershelf>
      <lettershelf>B</lettershelf>
      <aisleid idaisle="B"/>
    </shelf>
    <shelf idshelf = "B2">
      <numbershelf>1</numbershelf>
      <lettershelf>B</lettershelf>
      <aisleid idaisle="B"/>
    </shelf>
  </shelves>
  <boardgames>
    <boardgame>
      <name>Cuckoo in a Dark Forest (Kukacka v temném lese) </name>
      <productnumber>1301976024</productnumber>
      <price>94.7</price>
      <shelfid idshelf="B1"/>
    </boardgame>
    <boardgame>
      <name>The Underneath</name>
      <productnumber>9117616387</productnumber>
      <price>107.4</price>
      <shelfid idshelf="A2"/>
    </boardgame>
  </boardgames>
</data>

And here is my xslt-code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <head>
        <link rel="stylesheet"
        href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
      </head>
      <body>
        <table>
          <tr>
            <td>Name</td>
            <td>Price</td>
            <td>Product number</td>
            <td>Shelf</td>
            <td>Shelfnumber</td>
            <td>Aisle</td>
          </tr>
          <xsl:for-each select="data/boardgames/boardgame">
          <tr>
            <td><xsl:value-of select="name"/></td>
            <td><xsl:value-of select="price"/></td>
            <td><xsl:value-of select="productnumber"/></td>
            <td><xsl:value-of select="shelfid"/></td>
          </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

I wanted my xslt to transform all information contained in the boardgame-element. I thought if I would select "shelfid" in the boardgame-element then it would select the referenced element and its values as well. I tried to transform it. The price, name and productnumber worked as they were transformed correctly. But the values in the shelf-element didn't, which I tried to transform via the shelfid in the boardgame-element. Is it possible to select all values of a referenced sibling? If yes, how? I hope you understand what I mean. Thanks for your help!

XSLT has a built-in key mechanism for resolving cross-references. Consider the following example:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="aisle" match="aisle" use="@idaisle" />
<xsl:key name="shelf" match="shelf" use="@idshelf" />

<xsl:template match="/data">
    <html>
        <body>
            <table border="1">
                <tr>
                    <th>Name</th>
                    <th>Price</th>
                    <th>Product Number</th>
                    <th>Shelf Letter</th>
                    <th>Shelf Number</th>
                    <th>Aisle Letter</th>
                </tr>
                <xsl:for-each select="boardgames/boardgame">
                    <tr>
                        <td>
                            <xsl:value-of select="name"/>
                        </td>
                        <td>
                            <xsl:value-of select="price"/>
                        </td>
                        <td>
                            <xsl:value-of select="productnumber"/>
                        </td>
                        <xsl:variable name="shelf" select="key('shelf', shelfid/@idshelf)" />
                        <td>
                            <xsl:value-of select="$shelf/lettershelf"/>
                        </td>
                        <td>
                            <xsl:value-of select="$shelf/numbershelf"/>
                        </td>
                        <td>
                            <xsl:value-of select="key('aisle', $shelf/aisleid/@idaisle)/letteraisle"/>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
      </body>
    </html>
</xsl:template>
  
</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM