How to transform some tags to another using XSLT

牧云@^-^@ 提交于 2019-12-11 04:33:10

问题


I have the following xml:

<box>
   <title>bold text</title>
   some text
</box>

and the following xsl:

<xsl:template match="box">
    <p style="background:red;">
        <xsl:apply-templates/>
    </p>
</xsl:template>

<xsl:template match="title">
    <p style='background:green;'>
        <xsl:apply-templates/>
    </p>
</xsl:template>

And I got following:

<p style="background:red;"> </p>
<p style="background:green;">bold text</p>
some text
<p></p>

But I want following:

<p style="background:red;">
   <p style="background:green;">bold text</p>
   some text
</p>

How do I do this?


回答1:


When I run this xslt/xml I get this result:

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="box">
    <p style="background:red;">
        <xsl:apply-templates/>
    </p>
  </xsl:template>

  <xsl:template match="title">
    <p style='background:green;'>
        <xsl:apply-templates/>
    </p>
  </xsl:template>
</xsl:stylesheet>

Xml Output:

<?xml version="1.0"?>
<p style="background:red;">
   <p style="background:green;">bold text</p>
   some text
</p>

This is what you want correct?



来源:https://stackoverflow.com/questions/5757431/how-to-transform-some-tags-to-another-using-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!