如何为此文档转换xslt

编程入门 行业动态 更新时间:2024-10-27 03:28:45
如何为此文档转换xslt - > html(How to transform xslt -> html for the this document)

XML

<?xml version='1.0' encoding='UTF-8'?> <folder> <document> <h1>Harry Potter</h1> <h2>j.rowling</h2> <subheading>book</subheading> </document> <document> <p type="num"> <num>55</num> </p> <h1>Hi-Tech</h1> </document> </folder>

我做不到 :

如果找到元素h1,h2,subheading的顺序,则在一行上显示h1和子标题,然后输出元素h2;

现在:

<document> <h1>Harry Potter</h1> <h2>j.rowling</h2> <subheading>book</subheading> </document>

应该:

<div class="document"> <h1 style="display:inline-block">Harry Potter book </h1> <h5 style="display:inline-block">book</h5> <br/> <h2>j.rowling</h2> </div>
如果在h1元素之前有一个元素“p”,其属性为type =“num”,其中没有空元素num - 在元素h1的一行上显示它。

xml

<?xml version='1.0' encoding='UTF-8'?> <folder> <document> <h1>Harry Potter</h1> <h2>j.rowling</h2> <subheading>book</subheading> </document> <document> <p type="num"> <num>55</num> </p> <h1>Hi-Tech</h1> </document> </folder>

I can not do it :

If the order of the elements h1, h2, subheading is found, then display h1 and subheading on one line, then output the element h2;

NOW:

<document> <h1>Harry Potter</h1> <h2>j.rowling</h2> <subheading>book</subheading> </document>

SHOULD BE:

<div class="document"> <h1 style="display:inline-block">Harry Potter book </h1> <h5 style="display:inline-block">book</h5> <br/> <h2>j.rowling</h2> </div>
If before the h1 element there is an element "p" with the attribute type = "num", inside of which there is not an empty element num - display it on one line with the element h1.

最满意答案

首先考虑第一个要求,如果您知道输入具有序列(h1,h2,subheading),那么使用此逻辑可以轻松完成:

<xsl:template match="document"> <div class="document"> <xsl:apply-templates select="h1"/> <xsl:apply-templates select="subheading"/> <br/> <xsl:apply-templates select="h2"/> </div> </xsl:template> <xsl:template match="h1"/> <h1 style="display:inline-block"><xsl:value-of select="."/></h1> </xsl:template> <xsl:template match="h2"/> <h2><xsl:value-of select="."/></h2> </xsl:template> <xsl:template match="subheading"/> <h5 style="display:inline-block"><xsl:value-of select="."/></h5> </xsl:template>

但问题出现了:如果它不是这种格式怎么办? 遗憾的是,您没有告诉我们其他格式是否可行,以及遇到这些格式时您想要生成什么。 因此,如果没有更多信息,我们无法真正做到这一点。

第二条规则可以转换为XSLT

<xsl:template match="h1[preceding-sibling::*[1][self::p/@num][not(child::num)]"> <h1> <xsl:value-of select="preceding-sibling::*[1][self::p/@num]/@num"/> <xsl:value-of select="."/> </h1> </xsl:template>

Taking the first requirement first, if you know that the input has the sequence (h1, h2, subheading) then it's easily done with this logic:

<xsl:template match="document"> <div class="document"> <xsl:apply-templates select="h1"/> <xsl:apply-templates select="subheading"/> <br/> <xsl:apply-templates select="h2"/> </div> </xsl:template> <xsl:template match="h1"/> <h1 style="display:inline-block"><xsl:value-of select="."/></h1> </xsl:template> <xsl:template match="h2"/> <h2><xsl:value-of select="."/></h2> </xsl:template> <xsl:template match="subheading"/> <h5 style="display:inline-block"><xsl:value-of select="."/></h5> </xsl:template>

But then the question arises: what if it isn't in this format? Unfortunately you haven't told us what other formats are possible, and what you want to generate when they are encountered. So we can't really take this any further without more information.

The second rule can be translated into XSLT as

<xsl:template match="h1[preceding-sibling::*[1][self::p/@num][not(child::num)]"> <h1> <xsl:value-of select="preceding-sibling::*[1][self::p/@num]/@num"/> <xsl:value-of select="."/> </h1> </xsl:template>

更多推荐

本文发布于:2023-07-24 08:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1243467.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   xslt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!