从XML创建XSLT以获得所需的XML

时间:2013-08-26 07:02:05

标签: xml xslt xpath

我有一个情况: 请考虑以下代码作为示例:

    <?xml version="1.0" encoding="utf-8"?>
<school id="1" alias="abc" name="St.Josephs" val="">
    <teacher id="1">Rose</teacher>
    <subject>maths</subject>    
</school>
<school id="2" alias="bcd" name="" val="">
    <teacher id="2">john</teacher>
    <subject>science</subject>
</school>
<school id="3" alias="abc" name="" val="">
    <student rollno="12">sarah</student>
    <age>13</age>

</school>
<school id="4" alias="bcd" name="St.Mary's" val="">
        <student rollno="14">Rosh</student>
        <age>14</age>
</school>

现在我需要设计一个XSLT,它将创建元素中包含数据的元素,其中别名是abc,bcd同时输出将是这样的:

<Institutes>
    <group>
    <content>
            <![CDATA[
      <html>
      <head>
       <title>'Rose' is a 'Maths' teacher</title>
      </head>
      <body>
          Rose is a maths teacher for sarah in St.josephs school
      </body>
      </html>
   ]]>
    </content>
    </group>
</Institutes>
<Institutes>
    <group>
        <content>
            <![CDATA[
      <html>
      <head>
       <title>'john' is a 'science' teacher</title>
      </head>
      <body>
          john is a science teacher for Rosh in St.Mary's school
      </body>
      </html>
   ]]>
        </content>
    </group>
</Institutes>


Is there any way to achieve this..??

1 个答案:

答案 0 :(得分:0)

您需要使用样式表顶部的xsl:output元素以CDATA的形式列出您需要的元素:

<xsl:output
method="xml" ...
cdata-section-elements="content"
/>

属性cdata-section-elements告诉XSLT处理器必须将元素content写为CDATA部分。