【问题标题】:Checking multiple values of nodes in xml from xslt and displaying in html从xslt检查xml中节点的多个值并在html中显示
【发布时间】:2014-05-28 12:54:26
【问题描述】:

我有一个通过导出数据库获得的 XML 文件。现在我需要将 html 页面中的 xml 数据显示为表格。为此,我必须从 XSLT 文件检查 XML 文件中特定数据的相应值。由于我的数据库有点复杂,我在检查不同节点的多个值并从 xml 文件的另一个节点中选择相应的值时遇到了困难。例如我有以下 xml 数据-

   <?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>

我使用了以下 xslt 代码

<?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>

我被困在以下几点。 如果你注意到有 fm 节点、效果节点和 naviagtes2 节点。 在我的 html 页面中,将有一个表,其中第一列的值来自 fm 节点(root/name1/names/fm/name),第二列的值来自效果节点(root/FFrom/effect/name)。在 naviagtes2 节点中,有 fm 和 effect 元素,相当于 fm/id 和 effect/id。也就是说,naviagtes2 节点用于检查 effect/name 的哪些值将与表中的 fm/name 相对应。条件如下

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

我是 XSLT 编程的新手。谁能给我任何线索请如何在 XSLT 中解决这个问题?

【问题讨论】:

  • 如果您展示了无法正常工作的 XSLT,您更有可能得到有用的响应和解释。将更短的工作示例的服务简化为基本要素也是一件好事。
  • 添加一个有效的 XSLT(不仅仅是 sn-ps),这样我们就可以看到你尝试了什么。
  • 您的 XML 代码无效并且您的 XSLT 不完整。请发布代码,使我们能够通过复制并运行它而不进行修改来重现问题。
  • @michael.hor257k 实际上,我不适合发布原始代码。我只需要这个查询如何工作的提示。我已经解释了我的问题的重要部分。你能帮我吗?
  • 我不适合发布原始代码。”我不在乎它是原始的还是假的。我确实关心浪费我的时间来纠正您的代码以使其运行。只有通过运行它并重现问题,我才能建议如何解决它。另请注意,您说“它显示了错误的值” - 但您还应该说出“正确”的值是什么。

标签: html xml xslt xpath


【解决方案1】:

在 naviagtes2 节点中,有 fm 和 effect 元素,它们是 相当于 fm/id 和 effect/id。即使用naviagtes2节点 检查效果/名称的哪些值将与 fm/name 在 表。

恐怕我还是不明白你的问题,但为了推动这一进程,我会尝试一下(在黑暗中?)。

下面的样式表使用 keys 将每个 naviagtes2 节点链接到 fm 和 effect 元素,其 id 列在 naviagtes2 节点中。因此,每个 naviagtes2 节点都会创建一个 {fm,effect} “对”,这些对与从成对元素中获取的值一起列在结果表中:

<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>

当应用于您的示例输入时,结果是:

<?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>

在 HTML 中会被渲染为:

希望这能让你更接近你的目标。

【讨论】:

  • 非常感谢。你明白了我的意思,并部分解决了我的问题。现在 fm 名称和效果名称是一致的。但是您只使用了一个循环,而忽略了我在 XSLT 代码中提到的其他循环。这就是为什么它在每次迭代中显示所有值:(
  • @Cracker 您必须找到一种方法将其合并到您拥有的内容中,或者让您的问题更清晰。请注意,尽管 cmets 现在来回运行了一个多星期(!),但您仍然没有发布预期的结果。我愿意为此付出的时间和耐心是有限度的。
猜你喜欢
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
  • 1970-01-01
  • 2019-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多