繁体   English   中英

如何使用xslt将兄弟元素合并为一个

[英]How to combine sibling elements into one using xslt

我的xml摘录是

    <p outputclass="figurecaption">Sections</p>
            <p outputclass="figure">
                <image href="9528.gif">
                    <alt></alt>
                </image>
</p>

我希望它转变成

<figure>
  <title>Sections</title>
  <graphic href="D:/9528.gif"/>
 </figure>

我对xslt相当陌生,并已使用身份转换来转换此xml中的其他元素。 似乎无法弄清楚这一点。

此样式表仅转换相邻(从左到右)的p [@ outputclass ='figurecaption']和p [@ outputclass ='figure']

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

  <xsl:output method="xml" indent="yes"/>

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

  <!-- process only those figure paragraphs that are immeditately preceded by figurecaption paragraph --> 
  <xsl:template match="p[@outputclass='figure'][preceding-sibling::*[1][self::p and @outputclass='figurecaption']]">
    <figure>
      <title><xsl:value-of select="preceding-sibling::p[1][@outputclass='figurecaption']"/></title>
      <graphic href="{image/@href}"/>
    </figure>
  </xsl:template>

  <!-- do nothing with figurecaption paragraph immediately followed by figure paragraph because if was processed in previous template -->
  <xsl:template match="p[@outputclass='figurecaption'][following-sibling::*[1][self::p and @outputclass='figure']]"/>

</xsl:stylesheet>

暂无
暂无

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

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