使用xslt重新排序xml中的节点

时间:2010-05-20 17:01:28

标签: xml xslt

我如何转换像这样的xml:

<xml>
   <paragraph>
        <data>
            <label> title </label>
         </data>
         <data>
            <label> subtitle </label>
         </data>

         <data>
            <label> text </label>
         </data>

         <data>
            <label> image </label>
         </data>
   </paragraph>
</xml>

在以下xml(文本前的图像)中:

<xml>
   <paragraph>
        <data>
            <label> title </label>
         </data>
         <data>
            <label> subtitle </label>
         </data>
         <data>
            <label> image </label>
         </data>
         <data>
            <label> text </label>
         </data>
   </paragraph>
</xml>

如果下一个节点是图像,我真的需要调用图像的模板而不是文本。 (xml源在该字段中很乱)

然后我复制一些我正在使用xslt的代码:

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          

                  <xsl:when test="etiqueta = 'Imagen'">
                    <xsl:call-template name="imprimeImagenParrafo">
                      <xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
                      <xsl:with-param name="valor" select="valor"></xsl:with-param>
                      <xsl:with-param name="longitud" select="longitud"></xsl:with-param>
                      <xsl:with-param name="comentario" select="comentario"></xsl:with-param>
                      <xsl:with-param name="enlace" select="enlace"></xsl:with-param>
                      <xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
                    </xsl:call-template>
                  </xsl:when>
                </xsl:when>

1 个答案:

答案 0 :(得分:4)

你必须调整它以匹配你的数据(我假设样本源文件不是你真正拥有的),但也许它会激励你:

  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Identity copy that just passes through everything -->
<xsl:template match="@*|node()">
  <xsl:apply-templates select="@*|node()"/>
</xsl:template>

<!-- Here we match the paragraph element, copy it and its attrs, then rearrange it's children -->
<xsl:template match="paragraph">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <!-- Edit the predicates as necessary for your real data -->
    <!-- predicates are the things in the []...like the WHERE in a SQL query -->
    <xsl:apply-templates select="data[normalize-space(label) = 'title']"/>
    <xsl:apply-templates select="data[normalize-space(label) = 'subtitle']"/>
    <xsl:apply-templates select="data[normalize-space(label) = 'image']"/>
    <xsl:apply-templates select="data[normalize-space(label) = 'text']"/>
  </xsl:copy>
</xsl:template>

祝你好运, 大卫