简体   繁体   中英

Checking multiple values of nodes in xml from xslt and displaying in html

I have an XML file which i got by exporting a database. now i need to show the xml data in html page as a table. to do so, i have to check the corresponding values for a particular data in the XML file from an XSLT file. since my database is a bit complicated, i am facing difficulties in checking the multiple values of differnt nodes and selecting a corresponding values from another node of the xml file. eg i have the following xml data-

   <?xml-stylesheet type='text/xsl' href='myXSL.xsl'?>

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <name1>
    <names>
      <id>5</id>
      <class>space</class>
      <from>Germany</from>      
      <fm>
        <id>9</id>
        <names>5</names>
        <name>Vienna</name>        
      </fm>
      <fm>
        <id>10</id>
        <names>5</names>
        <name>Prague</name>        
      </fm>
    </names>    
  </name1>
  <FFrom>    
    <effect>
      <id>11</id>
      <DVV>1</DVV>
      <SAT>0</SAT>
      <DDCC>0</DDCC>      
      <name>SAA Name</name>      
    </effect>    
    <effect>
      <id>23</id>
      <DVV>0</DVV>
      <SAT>0</SAT>
      <DDCC>1</DDCC>      
      <name>SAA Name2</name>      
    </effect>
  </FFrom>  
  <name2>
    <newNames>
      <id>1</id>
      <name>VSSS Name</name>
      <route1>
        <id>5</id>
        <identifyer>C</identifyer>
        <function>abc</function>
        <names>4</names>
        <naviagtes2>
          <id>9</id>
          <fm>7</fm>
          <effect>2</effect>          
        </naviagtes2>
        <naviagtes2>
          <id>10</id>
          <fm>8</fm>
          <effect>5</effect>         
        </naviagtes2>
      </route1>  
    </newNames>
    <newNames>
      <id>6</id>
      <name>VEE Name</name>
      <route1>
        <id>18</id>
        <identifyer>C0</identifyer>
        <function>abc</function>
        <names>5</names>
        <naviagtes2>
          <id>68</id>
          <fm>9</fm>
          <effect>11</effect>          
        </naviagtes2>
        <naviagtes2>
          <id>69</id>
          <fm>10</fm>
          <effect>7</effect>          
        </naviagtes2>
      </route1>       
    </newNames>
  </name2>
</root>

I used the following xslt codes

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

 <html>

 <head><title>title</title>
 <style type="text/css">

body {
    font: 10px Verdana, sans-serif;
    color: #000000;
    margin: 0;
    padding: 0;
}
tr.header2 {
    font-style:italic;
}
tr.newNames{
    background-color:#6495ED;
    margin-top:4px;
}
</style>
</head>
<body><h1><xsl:value-of select="Title" /></h1>
 <table width="800px" class="table" cellspacing="0px"> 
 <xsl:for-each select="root/name2/newNames"> 
  <tr class="newNames"><td colspan="12">
  <b>NNavigate:</b> <xsl:value-of select="name"/><br/>
  <b>NMNaviagate:</b> <xsl:value-of select="route1/function"/>
  </td></tr>
    <xsl:for-each select="/root/name1/names">
    <tr class="names"><td colspan="12">
    <b> CClass: </b><xsl:value-of select="class" />
    <b> FFrom: </b><xsl:value-of select="from" />   
    </td></tr>  
        <tr class="header2">
            <td>Route</td>
            <td>From</td>
            <td align="center">SA</td>
            <td align="center">DB</td>

        </tr>
        <xsl:for-each select="fm">
        <tr>
            <td class=""><xsl:value-of select="name" /></td>                
            <td class=""><xsl:value-of select="/root/FFrom/effect/name" /></td>
            <td class=""><xsl:value-of select="/root/FFrom/effect/SAT" /></td>
            <td class=""><xsl:value-of select="/root/FFrom/effect/DVV" /></td>              
        </tr>
        </xsl:for-each>
    </xsl:for-each>
</xsl:for-each>
</table>
 </body>
 </html>
</xsl:template>

</xsl:stylesheet>

I am stuck in the following point. if you notice there are fm nodes, effect nodes and naviagtes2 nodes. in my html page, there will be a table where values of first column come from fm nodes (root/name1/names/fm/name) and values of second column come from effect nodes (root/FFrom/effect/name). in the naviagtes2 nodes, there are fm and effect elements which are equivalent to fm/id and effect/id. that is, naviagtes2 nodes are used to check which values of effect/name will be against fm/name in the table. conditions are like following

root/name1/names/fm/name against root/FFrom/effect/name if 
root/name1/names/fm/id = root/name2/newNames/route1/naviagtes2/fm  and 
root/FFrom/effect/id = root/name2/newNames/route1/naviagtes2/effect

i am new in XSLT programming. Could anyone give me any clue please how to solve that in XSLT ?

in the naviagtes2 nodes, there are fm and effect elements which are equivalent to fm/id and effect/id. that is, naviagtes2 nodes are used to check which values of effect/name will be against fm/name in the table.

I am afraid I still don't understand your question, but in an effort of moving this forward, I'll take a stab (in the dark?) at it.

The following stylesheet uses keys to link each naviagtes2 node to the fm and effect elements whose ids are listed in the naviagtes2 node. Thus each naviagtes2 node creates a {fm,effect} "pair" and these pairs are listed in the resulting table, along with the values fetched from the paired elements:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="fm" match="name1/names/fm" use="id" />
<xsl:key name="effect" match="FFrom/effect" use="id" />

<xsl:template match="/">
    <table border="1" > 
        <tr>
            <th>id</th>
            <th>fm id</th>
            <th>effect id</th>
            <th>fm name</th>
            <th>effect name</th>
        </tr>
        <xsl:for-each select="root/name2/newNames/route1/naviagtes2"> 
            <tr>
                <td><xsl:value-of select="id" /></td>
                <td><xsl:value-of select="fm" /></td>
                <td><xsl:value-of select="effect" /></td>
                <td><xsl:value-of select="key('fm', fm)/name" /></td>
                <td><xsl:value-of select="key('effect', effect)/name" /></td>
            </tr>       
        </xsl:for-each>
    </table>
</xsl:template>

</xsl:stylesheet>

when applied to your example input, the result is:

<?xml version="1.0" encoding="UTF-8"?>
<table border="1">
   <tr>
      <th>id</th>
      <th>fm id</th>
      <th>effect id</th>
      <th>fm name</th>
      <th>effect name</th>
   </tr>
   <tr>
      <td>9</td>
      <td>7</td>
      <td>2</td>
      <td/>
      <td/>
   </tr>
   <tr>
      <td>10</td>
      <td>8</td>
      <td>5</td>
      <td/>
      <td/>
   </tr>
   <tr>
      <td>68</td>
      <td>9</td>
      <td>11</td>
      <td>Vienna</td>
      <td>SAA Name</td>
   </tr>
   <tr>
      <td>69</td>
      <td>10</td>
      <td>7</td>
      <td>Prague</td>
      <td/>
   </tr>
</table>

which in HTML would be rendered as:

在此处输入图片说明

Hopefully this gets you closer to your goal.

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