使用XSLT将XML属性值传递给HTML Atrribute值 [英] Passing XML Attribute value to HTML Atrribute value using XSLT

查看:91
本文介绍了使用XSLT将XML属性值传递给HTML Atrribute值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含特定数据的XML文件,我必须将其转换为HTML表格。有3-4个只有2列的表格和4-5个有更多列的表格。我想传递XML属性值,例如根据列的数量, tableWidth = 200 或 tableWidth = 500



示例XML文件 -

 < tab>如果需要标题,则使用
<! - b =Y - >
< r b =Y>
< d>名称< / d>
< d>年龄< / d>
< / r>
< r>
< d> ABC< / d>
< d> 23< / d>
< / r>
< / tab>

以下是XSLT文件 -

 < xsl:stylesheet version ='1.0'xmlns:xsl ='http://www.w3.org/1999/XSL/Transform'> 
< xsl:output method ='html'media-type ='text / html'/>
< xsl:template match =Tab>
< xsl:for-each select =R>
< tr>
< xsl:when test =@ b ='Y'>
< xsl:for-each select =D>
< td align =''style ='border:1px solid black'>
< b>< xsl:value-of select =。/>< / b>
< / td>
< / xsl:for-each>
< / xsl:when>
< xsl:otherwise>
< xsl:for-each select =D>
< td align =''style ='border:1px solid black'>
< xsl:value-of select =。/>
< / td>
< / xsl:for-each>
< / xsl:otherwise>
< / tr>
< / xsl:for-each>
< / table>
< / xsl:template>
< / xsl:stylesheet>

我想添加一个属性,例如 tablewidth 到XML文件并在< table> 标记中使用它是XSLT文件...

解决方案

假设你已经修改了XML以包含一个 tableWidth 属性,如下所示...

 < tab tableWidth =500> 
....

有两种方法可以在XSLT中使用该属性。首先,更详细的方式....

 < xsl:template match =Tab> 
< table cellpadding =6cellspacing =0align =center>
< xsl:attribute name =width>< xsl:value-of select =@ tableWidth/>< / xsl:attribute>

但是使用属性值模板通常要干净得多。然后你只需要这样做:

 < xsl:template match =Tab> 
< table width ={@ tableWidth}cellpadding =6cellspacing =0align =center>

这两个应该输出以下内容:

 < table width =500cellpadding =6cellspacing =0align =center> 


I have an XML file with certain data which I have to convert it into HTML Table. There are 3-4 table with only 2 columns and 4-5 tables with more columns. I want to pass XML attribute value, say tableWidth=200 or tableWidth=500 depending upon the number of columns.

Sample XML File -

<tab>
  <!-- b="Y" will be used if Heading is required -->
  <r b="Y">
    <d>Name</d>
    <d>Age</d>
  </r>
  <r>
    <d>ABC</d>
    <d>23</d>
  </r>
</tab>

Following is the XSLT File -

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='html' media-type='text/html'/>
<xsl:template match="Tab">
<table width="500" cellpadding="6" cellspacing="0" align="center">
  <xsl:for-each select="R">
<tr>
    <xsl:choose>
        <xsl:when test="@b = 'Y'">
            <xsl:for-each select="D">
                <td align='' style='border:1px solid black'> 
                    <b><xsl:value-of select="."/></b>
                </td>
            </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
            <xsl:for-each select="D">
                <td align='' style='border:1px solid black'> 
                    <xsl:value-of select="."/>  
                </td>
            </xsl:for-each>
        </xsl:otherwise>
    </xsl:choose>
</tr>
  </xsl:for-each>
 </table>
 </xsl:template>
 </xsl:stylesheet>   

I want to add an attribute say tablewidth to XML file and use it in the <table> tag is XSLT file...

解决方案

Assuming you have amended your XML to include an tableWidth attribute, like so...

<tab tableWidth="500">
    ....

There are two ways to make use of the attribute in the XSLT. Firstly, the more verbose way....

<xsl:template match="Tab">
   <table cellpadding="6" cellspacing="0" align="center"> 
      <xsl:attribute name="width"><xsl:value-of select="@tableWidth" /></xsl:attribute>

But it is often much cleaner to use Attribute Value Templates. Then you only have to do this:

<xsl:template match="Tab">
   <table width="{@tableWidth}" cellpadding="6" cellspacing="0" align="center"> 

Both of these should output the following:

<table width="500" cellpadding="6" cellspacing="0" align="center">

这篇关于使用XSLT将XML属性值传递给HTML Atrribute值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆