使用规则将XML转换为HTML表的通用XSLT [英] Generic XSLT that transforms XML into HTML table with rules

查看:87
本文介绍了使用规则将XML转换为HTML表的通用XSLT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想制作一个XSLT,它可以按照规则转换任何XML:

 < root1> 
< elem1>文字< / elem1>
< elem2>文字< / elem2>
...
< elemx>文字< / elemx>
...
< image> someimgreference< / image
< linkref> alink< / linkref>
< / root1>

< root2>
< elem1>文字< / elem1>
< elem2>文字< / elem2>
...
< elemx>文字< / elemx>
...
< image> someimgreference< / image
< linkref> alink< / linkref>
< / root2>

使用XSLT我会得到一个带有标题的表格,当我有文本时,我只显示文本,当我有图像链接时,我得到的实际图像与

 < xsl:template match =image> 
< img src ={@ url}/>
< / xsl:template>

或者
,当我有一个linkref时,我只发布可点击链接

root1 / root2只是一个例子...这些可以是任何名称,elem1 / 2/3 /也是一样...我希望xslt能够与任何具有该结构的XML。所以没有引用元素名称。



HTML表格看起来像:

  elem1 | elem2 | ... | image | linkref 
------------------------------------
text | text | ... | photo | link
text |我在网上发现了一些很好的参考资料,但没有一个真正帮助我得到该表或没有足够通用。我想要的可能吗?或者我有多接近得到这个通用的xslt?



这是我发现的最通用的。我只需要将图像链接显示为实际照片并链接到可点击链接的引用。

 <?xml version = ? 1.0 > 
< xsl:stylesheet version =1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform>

< xsl:template match =/>
< HTML>
< BODY>
< xsl:apply-templates />
< / BODY>
< / HTML>
< / xsl:template>

< xsl:template match =/ *>
< TABLE BORDER =1>
< TR>
< xsl:for-each select =* [position()= 1] / *>
< TD>
< xsl:value-of select =local-name()/>
< / TD>
< / xsl:for-each>
< / TR>
< xsl:apply-templates />
< / TABLE>
< / xsl:template>

< xsl:template match =/ * / *>
< TR>
< xsl:apply-templates />
< / TR>
< / xsl:template>

< xsl:template match =/ * / * / *>
< TD>
< xsl:value-of select =。/>
< / TD>
< / xsl:template>
< / xsl:stylesheet>

我不知道在哪里插入图片模板或链接模板(或者如何编写链接模板)
我试图插入它像一个普通的模板,它不起作用。它不会对链接做任何事情。更不用说可点击的...
任何想法?完全可以吗?

解决方案

您在评论中使用了正确的模板,但是您可能需要添加 priority 属性,以确保匹配之前匹配 / * / * / *

 < xsl:template match =imagepriority =2> 
< td>
< img src ={。}/>
< / td>
< / xsl:template>

另外,要保存重复输出 td 标签对于每个模板,在应用模板匹配子节点之前,您可以在匹配 tr 的模板中输出 试试这个XSLT

 < xsl:stylesheet version =1.0xmlns:xsl =http://www.w3。组织/ 1999 / XSL /变换> 
< xsl:template match =/>
< HTML>
< BODY>
< xsl:apply-templates />
< / BODY>
< / HTML>
< / xsl:template>

< xsl:template match =/ *>
< TABLE BORDER =1>
< TR>
< xsl:for-each select =* [position()= 1] / *>
< TD>
< xsl:value-of select =local-name()/>
< / TD>
< / xsl:for-each>
< / TR>
< xsl:apply-templates />
< / TABLE>
< / xsl:template>

< xsl:template match =/ * / *>
< TR>
< xsl:for-each select =*>
< td>
< / td>
< / xsl:for-each>
< / TR>
< / xsl:template>

< xsl:template match =image>
< img src ={。}/>
< / xsl:template>

< xsl:template match =linkref>
< a href ={。}>
< / a>
< / xsl:template>
< / xsl:stylesheet>


So I want to make an XSLT that would transform any XML following the rule:

<root1>
   <elem1>text</elem1>
   <elem2>text</elem2>
   ...
   <elemx>text</elemx>
   ... 
   <image>someimgreference</image
   <linkref>alink</linkref>
</root1>

<root2>
   <elem1>text</elem1>
   <elem2>text</elem2>
   ...
   <elemx>text</elemx>
   ... 
   <image>someimgreference</image
   <linkref>alink</linkref>
</root2>

using the XSLT I would get a table with the titles and when I have text, I only show the text, when I have image link I get the actual image with

<xsl:template match="image">
    <img src="{@url}" />
</xsl:template>

or something and when I have a linkref, I just post the click-able link

root1/root2 is just an exemple... those could be any names, same goes for elem1/2/3/... I want the xslt to work with any XML with that structure. So no element name references.

HTML table would look like:

    elem1 | elem2 |... |image|linkref
  ------------------------------------
    text  | text  |... |photo|link
    text  | text  |... |photo|link

I found some good references online but none really helped me get the table or none were generic enough. Is it possible what I want? Or how close can I get to getting this generic xslt?

This is the most generic I've found. I just need the image links to be shows as actual photos and link references to click-able links

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="/">
<HTML>
<BODY>
    <xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>

<xsl:template match="/*">
<TABLE BORDER="1">
<TR>
        <xsl:for-each select="*[position() = 1]/*">
          <TD>
              <xsl:value-of select="local-name()"/>
          </TD>
        </xsl:for-each>
</TR>
      <xsl:apply-templates/>
</TABLE>
</xsl:template>

<xsl:template match="/*/*">
<TR>
    <xsl:apply-templates/>
</TR>
</xsl:template>

<xsl:template match="/*/*/*">
<TD>
    <xsl:value-of select="."/>
</TD>
</xsl:template>
</xsl:stylesheet>

I don't know where to insert the image template or the link templates (or how to write the link template) I tried inserting it like a normal template and it doesn't work. It won't do anything with the link. Not to mention the clickable one... Any ideas? At all?

解决方案

You've got the right template to use in comments, however you probably need to add a priority attribute to it to ensure it gets matched before the one matching /*/*/*

<xsl:template match="image" priority="2">
   <td>
    <img src="{.}" /> 
   </td>
</xsl:template>

Alternatively, to save repeatedly outputting td tags for each template, you could output in the template that matches tr before applying the templates to match the child nodes

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/">
<HTML>
<BODY>
    <xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>

<xsl:template match="/*">
<TABLE BORDER="1">
<TR>
        <xsl:for-each select="*[position() = 1]/*">
          <TD>
              <xsl:value-of select="local-name()"/>
          </TD>
        </xsl:for-each>
</TR>
      <xsl:apply-templates/>
</TABLE>
</xsl:template>

<xsl:template match="/*/*">
<TR>
    <xsl:for-each select="*">
      <td>
        <xsl:apply-templates select="." />
      </td>
  </xsl:for-each>
</TR>
</xsl:template>

<xsl:template match="image">
   <img src="{.}" /> 
</xsl:template>

<xsl:template match="linkref">
  <a href="{.}">
    <xsl:value-of select="." />
  </a>
</xsl:template>
</xsl:stylesheet>

这篇关于使用规则将XML转换为HTML表的通用XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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