以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XSL/XSLT/XSL-FO/CSS 』  (http://bbs.xml.org.cn/list.asp?boardid=8)
----  通用的结果集处理xsl  (http://bbs.xml.org.cn/dispbbs.asp?boardid=8&rootid=&id=58919)


--  作者:hanjack
--  发布时间:2/18/2008 3:46:00 PM

--  通用的结果集处理xsl
我有一个xml文件,该文件包含一个结果集和结果集对应的Column信息,如下:
<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/xsl" href="DefaultTemplate.xsl"?>
<ResultSet version="1.0">
 <DataGrid>
  <Rows>
   <Row id="1">
    <PROGRAMID>ProgramDef</PROGRAMID>
    <PROGRAMCAPTION>程序定义</PROGRAMCAPTION>
    <PROGRAMJSPFILE>/Frame/ProgramDef/ProgramDef_Main.jsp</PROGRAMJSPFILE>
    <PROGRAMPROCESSER>com.jillsoft.MainServlet.JillCustomServlet</PROGRAMPROCESSER>
    <PROGRAMPARAM />
   </Row>
   <Row id="2">
    <PROGRAMID>UserDef</PROGRAMID>
    <PROGRAMCAPTION>用户定义</PROGRAMCAPTION>
    <PROGRAMJSPFILE>/Frame/UserDef/UserDef_Main.jsp</PROGRAMJSPFILE>
    <PROGRAMPROCESSER>com.jillsoft.JillFrame.Jill_UserDef</PROGRAMPROCESSER>
    <PROGRAMPARAM />
   </Row>
   <Row id="3">
    <PROGRAMID>SysDef</PROGRAMID>
    <PROGRAMCAPTION>系统定义</PROGRAMCAPTION>
    <PROGRAMJSPFILE>/Frame/SysDef/SysDef_Main.jsp</PROGRAMJSPFILE>
    <PROGRAMPROCESSER>com.jillsoft.MainServlet.JillCustomServlet</PROGRAMPROCESSER>
    <PROGRAMPARAM />
   </Row>
   <Row id="4">
    <PROGRAMID>DeptDef</PROGRAMID>
    <PROGRAMCAPTION>部门定义</PROGRAMCAPTION>
    <PROGRAMJSPFILE>/Frame/DeptDef/DeptDef_Main.jsp</PROGRAMJSPFILE>
    <PROGRAMPROCESSER>com.jillsoft.MainServlet.JillCustomServlet</PROGRAMPROCESSER>
    <PROGRAMPARAM />
   </Row>
   <Row id="5">
    <PROGRAMID>StyleSet</PROGRAMID>
    <PROGRAMCAPTION>风格设置</PROGRAMCAPTION>
    <PROGRAMJSPFILE>/StyleSet.jsp</PROGRAMJSPFILE>
    <PROGRAMPROCESSER />
    <PROGRAMPARAM />
   </Row>
  </Rows>
  <Cols>
   <Column>
    <FieldName>ProgramID</FieldName>
    <ColumnTitle>程序代码</ColumnTitle>
    <ColumnWidth>500</ColumnWidth>
   </Column>
   <Column>
    <FieldName>ProgramCaption</FieldName>
    <ColumnTitle>程序标题</ColumnTitle>
    <ColumnWidth>100</ColumnWidth>
   </Column>
   <Column>
    <FieldName>ProgramJspFile</FieldName>
    <ColumnTitle>程序页面</ColumnTitle>
    <ColumnWidth>100</ColumnWidth>
   </Column>
   <Column>
    <FieldName>ProgramProcesser</FieldName>
    <ColumnTitle>业务处理器</ColumnTitle>
    <ColumnWidth>100</ColumnWidth>
   </Column>
   <Column>
    <FieldName>ProgramParam</FieldName>
    <ColumnTitle>程序参数</ColumnTitle>
    <ColumnWidth>100</ColumnWidth>
   </Column>
  </Cols>
 </DataGrid>
</ResultSet>
其中DataGrid是结果集,Rows下是记录数据,Cols下是Column数据,Column中定义了每个记录的字段的信息。Row下面的节点的名字是由Column/FieldName来决定的,每一个Column对应Row下面的一个节点,这样我可以根据不同的结构输出不同的Cols和Rows。
我通过下面的XSL进行显示:
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:key name="lists" match="//Row" use="attribute::id"/>
 <xsl:template match="/ResultSet">
  <html>
   <head>
    <title>XML</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
   </head>
   <body leftmargin="0" topmargin="0">
    <table id="tbl" width="100%" border="1" cellspacing="1" cellpadding="2">
     <THEAD align="center">
      <xsl:for-each select="DataGrid/Cols/Column">
       <th>
        <xsl:attribute name="width">
         <xsl:value-of select="ColumnWidth"/>
        </xsl:attribute>
        <xsl:value-of select="ColumnTitle"/>
       </th>
      </xsl:for-each>
     </THEAD>
     <TBODY>
      <xsl:for-each select="DataGrid/Rows/Row">
       <tr>
        <xsl:variable name="CNode" select="current()"/>
        <xsl:variable name="CPos" select="position()"/>
        <xsl:for-each select="/ResultSet/DataGrid/Cols/Column">
         <xsl:variable name="ColumnName" select="FieldName"></xsl:variable>
         <td>
          <xsl:apply-templates select="key('lists', $CPos)">
           <xsl:with-param name="fn" select="FieldName"/>
          </xsl:apply-templates>
         </td>
        </xsl:for-each>
       </tr>
      </xsl:for-each>
     </TBODY>
    </table>
   </body>
  </html>
 </xsl:template>
    <xsl:template match="Row">
        <xsl:param name="fn"/>
        <xsl:value-of select="$fn"/>
    </xsl:template>
</xsl:stylesheet>
现在的问题是列标题可以显示出来,但是每列的数据始终不能显示出来,就是XSL文件的最后几句话里,<xsl:param name="fn"/>,fn是字段的名字,应该显示出来Row下面对应的fn的节点的值,一直也没弄出来,看看哪位高手能帮我搞定它!!!


--  作者:enyaxp
--  发布时间:2/20/2008 10:38:00 PM

--  
是我眼花了吗,怎么找不到fn节点?
--  作者:孤独
--  发布时间:2/22/2008 3:42:00 PM

--  
@enyaxp
<xsl:with-param name="fn" select="FieldName"/>
@hanjack
Here is the xslt for you.
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:key name="lists" match="//Row" use="attribute::id"/>
  <xsl:template match="/ResultSet">
    <html>
      <head>
        <title>XML</title>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
      </head>
      <body leftmargin="0" topmargin="0">
        <table id="tbl" width="100%" border="1" cellspacing="1" cellpadding="2">
          <THEAD align="center">
            <xsl:for-each select="DataGrid/Cols/Column">
              <th>
                <xsl:attribute name="width">
                  <xsl:value-of select="ColumnWidth"/>
                </xsl:attribute>
                <xsl:value-of select="ColumnTitle"/>
              </th>
            </xsl:for-each>
          </THEAD>
          <TBODY>
            <xsl:for-each select="DataGrid/Rows/Row">
              <tr>
                <xsl:variable name="CNode" select="current()"/>
                <xsl:variable name="CPos" select="position()"/>
                <xsl:for-each select="/ResultSet/DataGrid/Cols/Column">
                  <xsl:variable name="ItemPos" select="position()"/>
                  <xsl:variable name="ColumnName" select="FieldName"></xsl:variable>
                  <td>
                    <xsl:apply-templates select="key('lists', $CPos)">
                      <xsl:with-param name="fn" select="$CNode/*[position()=$ItemPos]"/>
                    </xsl:apply-templates>
                  </td>
                </xsl:for-each>
              </tr>
            </xsl:for-each>
          </TBODY>
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="Row">
    <xsl:param name="fn"/>
    <xsl:value-of select="$fn"/>
  </xsl:template>
</xsl:stylesheet>
I think you want to get this and I Really don't know what your code's meaning is,such as
"<xsl:apply-templates select="key('lists', $CPos)">"~
--  作者:孤独
--  发布时间:2/22/2008 3:43:00 PM

--  
And Here is the result
程序代码 程序标题 程序页面 业务处理器 程序参数
ProgramDef 程序定义 /Frame/ProgramDef/ProgramDef_Main.jsp com.jillsoft.MainServlet.JillCustomServlet  
UserDef 用户定义 /Frame/UserDef/UserDef_Main.jsp com.jillsoft.JillFrame.Jill_UserDef  
SysDef 系统定义 /Frame/SysDef/SysDef_Main.jsp com.jillsoft.MainServlet.JillCustomServlet  
DeptDef 部门定义 /Frame/DeptDef/DeptDef_Main.jsp com.jillsoft.MainServlet.JillCustomServlet  
StyleSet 风格设置 /StyleSet.jsp   

W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
78.125ms