无法在a中插入<;tr>;使用XSLT从XML获取属性的内容

无法在a中插入<;tr>;使用XSLT从XML获取属性的内容,xml,xslt,xpath,Xml,Xslt,Xpath,我正在尝试使用自己编写的VB脚本从rest调用接收的XML文件进行简单的XSL转换。这是我收到的XML(清理后): 这是我为其编写的简单XSL文件: <?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

我正在尝试使用自己编写的VB脚本从rest调用接收的XML文件进行简单的XSL转换。这是我收到的XML(清理后):


这是我为其编写的简单XSL文件:

<?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>
  <body>
    <h2>GN4 User Information</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Obj ID</th>
        <th>User Complete Name</th>
      </tr>
      <tr>
        <td> <xsl:value-of select="obj/@id"/></td>
        <td> Some Text </td>
      </tr>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

GN4用户信息
对象ID
用户全名
一些文本


XSLT生成的HTML代码显示了该表,您可以看到写有“Some Text”的单元格(正如我在XSL代码中指定的那样),但在打印“id”属性的内容时,我什么也没有收到。我玩Xpath有点认为我在路径中做了一些错误的事情,但我总是什么都没有收到

您有几个问题:

  • 实际上,您并没有将模板应用于(我假设)希望迭代的
    obj
    节点
  • 您的输入文档具有默认名称空间URI。在开始使用XSLT时,这是一个非常常见的问题
  • 我对以下转换进行了注释,以帮助澄清对原始转换的更改

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
                                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                                xmlns:gn="http://www.teradp.com/schemas/GN4/1/Results.xsd">
    
      <!-- Note that you need to match the namespace of the nodes in the input
             document.  This namespace is declared above, and needs to be used with
             each element in an XPath expression where you expect to match a node with
             that namespace URI. -->
      <xsl:template match="/gn:result">
        <html>
            <body>
                <h2>GN4 User Information</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Obj ID</th>
                        <th>User Complete Name</th>
                    </tr>
    
                    <!-- Right now the context node is the root "gn:result" element
                             (because that's what this template matched).  From here, apply
                             the row template on the "gn:obj" elements.  Everything else in
                             this template is essentially static content. -->
                    <xsl:apply-templates select="gn:objsListResult/gn:obj" />
                </table>
            </body>
        </html>
      </xsl:template>
    
      <!-- When this template is called, it will be executed once for each gn:obj
             element that matches. -->
      <xsl:template match="gn:obj">
        <tr>
            <!-- Attributes don't need to be namespaced. -->
            <td> <xsl:value-of select="@id"/></td>
            <td> Some Text </td>
        </tr>
      </xsl:template>
    
    </xsl:stylesheet>
    
    
    GN4用户信息
    对象ID
    用户全名
    一些文本
    
    应用于您的输入,它会产生以下输出(为清晰起见缩进):

    
    GN4用户信息
    对象ID
    用户全名
    4.
    一些文本
    5.
    一些文本
    1608
    一些文本
    1610
    一些文本
    2209
    一些文本
    
    我想这就是你想要的

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
                                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                                xmlns:gn="http://www.teradp.com/schemas/GN4/1/Results.xsd">
    
      <!-- Note that you need to match the namespace of the nodes in the input
             document.  This namespace is declared above, and needs to be used with
             each element in an XPath expression where you expect to match a node with
             that namespace URI. -->
      <xsl:template match="/gn:result">
        <html>
            <body>
                <h2>GN4 User Information</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Obj ID</th>
                        <th>User Complete Name</th>
                    </tr>
    
                    <!-- Right now the context node is the root "gn:result" element
                             (because that's what this template matched).  From here, apply
                             the row template on the "gn:obj" elements.  Everything else in
                             this template is essentially static content. -->
                    <xsl:apply-templates select="gn:objsListResult/gn:obj" />
                </table>
            </body>
        </html>
      </xsl:template>
    
      <!-- When this template is called, it will be executed once for each gn:obj
             element that matches. -->
      <xsl:template match="gn:obj">
        <tr>
            <!-- Attributes don't need to be namespaced. -->
            <td> <xsl:value-of select="@id"/></td>
            <td> Some Text </td>
        </tr>
      </xsl:template>
    
    </xsl:stylesheet>
    
    <html xmlns:gn="http://www.teradp.com/schemas/GN4/1/Results.xsd">
        <body>
            <h2>GN4 User Information</h2>
            <table border="1">
                <tr bgcolor="#9acd32">
                    <th>Obj ID</th>
                    <th>User Complete Name</th>
                </tr>
                <tr>
                    <td>4</td>
                    <td> Some Text </td>
                </tr>
                <tr>
                    <td>5</td>
                    <td> Some Text </td>
                </tr>
                <tr>
                    <td>1608</td>
                    <td> Some Text </td>
                </tr>
                <tr>
                    <td>1610</td>
                    <td> Some Text </td>
                </tr>
                <tr>
                    <td>2209</td>
                    <td> Some Text </td>
                </tr>
            </table>
        </body>
    </html>