简体   繁体   English

使用 XSLT 转换 XML 结构

[英]Transform XML structure using XSLT

I want to transform an XML structure with XSLT.我想用 XSLT 转换 XML 结构。

 <detaileddescription>
   <para>Some text</para>
   <para>
    <bold>Title</bold>
   </para>
   <para>Intro text:
    <itemizedlist>
     <listitem>
      <para>Text</para>
     </listitem>
     <listitem>
      <para>Text</para>
     </listitem>
    </itemizedlist>
   </para>
 </detaileddescription>

This is what I want:这就是我要的:

 <detaileddescription>
   <para>Some text</para>
   <List>
    <Title>Title</Title>
    <Intro>
      Intro text:
    </Intro
    <ListItem>
      <para>Text</para>
     </ListItem>
     <ListItem>
      <para>Text</para>
     </ListItem>
    </List>
 </detaileddescription>

So in my own words: If there is a <bold> inside a <para> , check if the following-sibling of the <para> is also a <para> and has a child <para>Text</para> than I want to rebuild the structure as shown.所以,在我自己的话:如果有一个<bold>一个内部<para> ,检查以下同胞<para>也是<para>和具有子<para>Text</para>比我想要重建如图所示的结构。

I'm not sure if it is possible, because I have just started using xslt/xpath.我不确定是否可能,因为我刚刚开始使用 xslt/xpath。 Can anyone give me a little help?谁能给我一点帮助?

If you want to use conditions on nodes then I would suggest to put them into match patterns:如果您想在节点上使用条件,那么我建议将它们放入匹配模式中:

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

    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="detaileddescription/para[bold][following-sibling::*[1][self::para[.//para]]]">
       <List>
           <Title>
               <xsl:value-of select="bold"/>
           </Title>
           <xsl:apply-templates select="following-sibling::*[1]"  mode="intro-list"/>
       </List>
    </xsl:template>

    <xsl:template match="detaileddescription/para[.//para][preceding-sibling::*[1][self::para[bold]]]"/>

    <xsl:template match="detaileddescription/para/text()[1]" mode="intro-list">
        <Intro>
            <xsl:value-of select="."/>
        </Intro>
    </xsl:template>

    <xsl:template match="listitem" mode="intro-list">
        <ListItem>
            <xsl:apply-templates/>
        </ListItem>
    </xsl:template>
</xsl:transform>

See http://xsltransform.net/3NzcBtK/2 for a sample.有关示例,请参阅http://xsltransform.net/3NzcBtK/2

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

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