未显示 XSL 属性值

XSL attribute value not getting displayed

这是我的 xml:

<RESPONSE heading="Broker List">
     <RESULTSET nbr="1" rowcount="11">
         <ROW nbr="1">
             <COL nbr="1" name="broker_name" datatype="String" href="api">BARC</COL>
             <COL nbr="2" name="broker_id" datatype="String">11</COL>
         </ROW>
     </RESULTSET>
</RESPONSE>

这个 xls 有效:

<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/RESPONSE">
<html>
<head>
</head>
<body class='data'>
  <h2><xsl:value-of select="./@heading"/></h2>
    <xsl:apply-templates select="RESULTSET" />
</body>
</html>
</xsl:template> 

<xsl:template match="RESULTSET">
     <table id="result" class="sortable-theme-finder" data-sortable="">
        <thead>
         <xsl:for-each select="ROW[1]/COL">
             <th> <xsl:value-of select="@name" /></th>
         </xsl:for-each>
        </thead>  
        <tbody>
          <xsl:apply-templates select="ROW" />
        </tbody>  
    </table>
    <br/>
</xsl:template>

<xsl:template match="ROW" >
      <tr>
        <xsl:apply-templates select="COL" />
     </tr>  
   </xsl:template>

   <xsl:template match="COL">
       <td border="1">
           <a><xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute>
            <xsl:value-of select="."/>
          </a>  
       </td>
   </xsl:template>  
</xsl:stylesheet>

结果是:

<tr>
    <td border="1"><a href="broker_name">BARC</a></td>
    <td border="1"><a href="broker_id">11 </a></td>
 </tr>

但是,如果我更改此行:

<xsl:value-of select="@name" />

<xsl:value-of select="@href" />

结果是这样的:

<tr>
    <td border="1"><a href="">BARC</a></td>
    <td border="1"><a href="">11 </a></td>
 </tr>

为什么@href 属性没有被选中?它只接受名称属性。也试过 'id' 但不行。我正在使用 C# XslCompiledTransform 来执行转换。谢谢!

改变

<xsl:attribute name="href"><xsl:value-of select="@name"/></xsl:attribute>

<xsl:attribute name="href"><xsl:value-of select="@name"/></xsl:attribute>

是否在此上下文中工作(xsl:for-each 内的当前上下文节点)。我已将您的样式表稍微调整为以下内容,但其工作方式类似:

XSLT 样式表

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output method="xml" omit-xml-declaration="no" encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*"/>

    <xsl:template match="ROW" >
      <tr>
        <xsl:apply-templates/>
     </tr>  
   </xsl:template>

   <xsl:template match="COL">
       <td border="1">
           <a>
               <xsl:copy-of select="@href"/>
               <xsl:apply-templates/>
           </a>
       </td>
   </xsl:template>
</xsl:transform>

XML输出

输出将是,准确给出您显示的输入文档:

<?xml version="1.0" encoding="UTF-8"?>
<tr>
   <td border="1">
      <a href="api">BARC</a>
   </td>
   <td border="1">
      <a>11</a>
   </td>
</tr>

如您所见,href 属性被保留 - 对于它在输入中出现的单个 COL 元素。

试试这个解决方案 online here


I thought perhaps href was a reserved word so i tried ref and other attributes names.

不,"href" 不是 XML 中的保留名称,并且没有预定义的语义,就像在 HTML 中一样。

<xsl:value-of select="@href"/>

只会从名为 "href" 的属性中检索内容。