简体   繁体   English

XSLT将XML转换为XHTML

[英]XSLT to transform XML to XHTML

Definitely I will never find success thinking in XSLT... 绝对我永远不会在XSLT中找到成功的想法...

Here is what I have: 这是我所拥有的:

   <par font="Arial" size="24">text 24</par>
   <par font="Arial" size="12">text 12</par>
   <par font="Times New Roman" size="12">text times 12</par>

And here is what I want: 这就是我想要的:

   <font style="font-family:Arial"><font style="font-size:24">text 24</font></font>
   <font style="font-family:Arial"><font style="font-size:12">text 12</font></font>
   <font style="font-family:Times New Roman"><font style="font-size:12">text times 12</font></font>

Of course, something like this is good as well, but for me this solution seems more complicated: 当然,像这样的东西也很好,但是对我来说,这种解决方案似乎更复杂:

<font style="font-family:Arial;font-size:24">text 24</font>
<font style="font-family:Arial;font-size:12">text 12</font>
<font style="font-family:Times New Roman;font-size:12">text times 12</font>

I think (but I may be wrong) I will have to specify some code for each size and each font. 我认为(但我可能错了),我将不得不为每种尺寸和每种字体指定一些代码。 This is not a problem because there are only few sizes and fonts available in my xml. 这不是问题,因为在我的xml中只有很少的大小和字体可用。

Many many thanks for your help, I start loosing my hair trying to do it by myself... 非常感谢您的帮助,我开始松开头发,尝试自己动手做...

You could do something like this: 您可以执行以下操作:

XML Input (modified to be well-formed) XML输入 (修改为格式正确)

<doc>
    <par font="Arial" size="24">text 24</par>
    <par font="Arial" size="12">text 12</par>
    <par font="Times New Roman" size="12">text times 12</par>   
</doc>

XSLT 1.0 XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="doc">
        <html>
            <xsl:apply-templates/>
        </html>
    </xsl:template>

    <xsl:template match="par">
        <font>
            <xsl:attribute name="style">
                <xsl:apply-templates select="@*"/>
            </xsl:attribute>
            <xsl:apply-templates select="node()"/>
        </font>
    </xsl:template>

    <xsl:template match="par/@size">
        <xsl:value-of select="concat('font-size: ',.,';')"/>
    </xsl:template>
    <xsl:template match="par/@font">
        <xsl:value-of select="concat('font-family: ',.,';')"/>
    </xsl:template>

</xsl:stylesheet>

Output 输出量

<html>
   <font style="font-family: Arial;font-size: 24;">text 24</font>
   <font style="font-family: Arial;font-size: 12;">text 12</font>
   <font style="font-family: Times New Roman;font-size: 12;">text times 12</font>
</html>

Also, you can change the match="par" to match="par[@font or @size]" if you only want to output <font> if it has a font or size attribute. 另外,如果您只想输出具有fontsize属性的<font>则可以将match="par"更改为match="par[@font or @size]"

Congradulations to DevNull. 转换为DevNull。 His was the first correct answer. 他是第一个正确答案。 James answer is wrong. 詹姆斯的回答是错误的。

I am just being very pedantic here, but the actual simplest tightest solution is .... <drumroll/>.... 我在这里只是很书呆子,但实际上最简单的最严格的解决方案是.... <drumroll /> .....

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

<xsl:template match="/">
 <xsl:for-each select="*/par">
  <font style="{ string-join(
   for $a in (@size|@font) return
     concat('font-', if ($a except @font) then 'size' else 'family', ':', $a),
     ';')}">
   <xsl:copy-of select="node()"/>
  </font>
 </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

The above XSLT 2.0 style-sheet will transform this input ... 上面的XSLT 2.0样式表将转换此输入...

<doc>
    <par font="Arial" size="24">text 24</par>
    <par font="Arial" size="12">text 12</par>
    <par font="Times New Roman" size="12">text times 12</par>   
    <par font="Courier">No size specified!</par>   
    <par size="16">No font specified!</par>   
</doc>

... into this output ... ...到这个输出...

<font style="font-family:Arial;font-size:24">text 24</font>
<font style="font-family:Arial;font-size:12">text 12</font>
<font style="font-family:Times New Roman;font-size:12">text times 12</font>
<font style="font-family:Courier">No size specified!</font>
<font style="font-size:16">No font specified!</font>

One small template and no extraneous semicolons in the style attribute value! 一个小的模板,样式属性值中没有多余的分号!

There was no mention of what XSLT version was required. 没有提到需要什么XSLT版本。 So if the OP is on XSLT 1.0, then this solution may not be useful to the OP. 因此,如果OP在XSLT 1.0上,则此解决方案可能对OP无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM