【问题标题】:Create XSLT from an XML to get desired XML从 XML 创建 XSLT 以获得所需的 XML
【发布时间】:2013-08-26 07:02:04
【问题描述】:

我这里有一个情况: 以下面的代码为例:

    <?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..??

【问题讨论】:

  • 您是否可以使用一些更有意义的数据填充输入和输出 XML 示例,而不是仅仅说 values from &lt;x&gt;&lt;p&gt; where name=123,因为不完全清楚应该如何映射元素。谢谢!
  • 嗨蒂姆,我已经修改了代码以便更好地理解。谢谢!
  • 我很好奇您为什么要将 html 输出为转义 (CDATA)。

标签: xml xslt xpath


【解决方案1】:

您需要使用样式表顶部的 xsl:output 元素来列出您需要作为 CDATA 的元素,这样:

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

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

【讨论】:

  • 谢谢比尔!但我的问题是是否有可能使用 XSLT 实现我上面提到的场景。
猜你喜欢
  • 1970-01-01
  • 2014-09-23
  • 2011-05-04
  • 2016-07-11
  • 1970-01-01
  • 1970-01-01
  • 2018-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多