我如何使用xslt在一个元素中包含不同的元素组

时间:2017-06-28 09:39:24

标签: xml xslt

如何使用xslt

将不同元素包装在一个根元素中

我的XML代码是:

<section>
<body>
<p>Trying text</p>
<Head>Heading 1</Head>
  <ListItem1>item 1</ListItem1>
  <ListItem2>item 2</ListItem2>
  <ListItem2>item 3</ListItem2>
  <ListItem3>item 4</ListItem3>
  <ListItem4>item 4</ListItem4>
  <ListItem1>item 1</ListItem1>
  <ListItem2>item 2</ListItem2>
  <ListItem2>item 3</ListItem2>
  <ListItem3>item 4</ListItem3>
  <ListItem3>item 4</ListItem3>
  <ListItem4>item 4</ListItem4>
  <ListItem3>item 4</ListItem3>
  <ListItem1>item 1</ListItem1>
  <ListItem2>item 2</ListItem2>
  <ListItem2>item 3</ListItem2>
  <ListItem3>item 4</ListItem3>
  <ListItem3>item 4</ListItem3>
  <ListItem2>item 2</ListItem2>
  <ListItem2>item 3</ListItem2>
<Head>Heading 2</Head>
  <ListItem1>item 1</ListItem1>
  <ListItem2>item 2</ListItem2>
  <ListItem2>item 3</ListItem2>
  <ListItem3>item 4</ListItem3>
  <ListItem1>item 1</ListItem1>
  <ListItem2>item 2</ListItem2>
  <ListItem2>item 3</ListItem2>
  <ListItem3>item 4</ListItem3>
  <ListItem1>item 1</ListItem1>
  <ListItem2>item 2</ListItem2>
  <ListItem2>item 3</ListItem2>
  <ListItem3>item 4</ListItem3>
  <ListItem3>item 4</ListItem3>
  <ListItem2>item 2</ListItem2>
  <ListItem2>item 3</ListItem2>
</body>
</section>

我正在使用XSLT:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

  <xsl:template match="Section">
  <Section>
    <xsl:apply-templates/>
  </Section>
 </xsl:template>

 <xsl:template match="ListItem1">
        <div>
            <title><xsl:call-template name="ListItem"/></title>
        </div>
        <xsl:apply-templates select="following-sibling::node()
                                      [not(self::ListItem1|ListItem2)][1]"/>
    </xsl:template>

</xsl:stylesheet>

输出需要为:

在一个根元素中包装listitem1,2,3,4并在一个根元素中标题为标题

<Notes>
<p>Trying text</p>
<div>
<title>Heading 1</title>
<entry>
  <primary>item 1</primary>
  <secondary>item 2</secondary>
  <secondary>item 3</secondary>
  <teritary level="1">item 4</teritary>
  <teritary level="2">item 4</teritary>
</entry>
<entry>
  <primary>item 1</primary>
  <secondary>item 2</secondary>
  <secondary>item 3</secondary>
  <teritary level="1">item 4</teritary>
  <teritary level="1">item 4</teritary>
  <teritary level="2">item 4</teritary>
  <teritary level="1">item 4</teritary>
</entry>
<entry>
  <primary>item 1</primary>
  <secondary>item 2</secondary>
  <secondary>item 3</secondary>
  <teritary level="1">item 4</teritary>
  <teritary level="1">item 4</teritary>
  <secondary>item 2</secondary>
  <secondary>item 3</secondary>
</entry>
</div>
<div>
<title>Heading 2</title>
<entry>
  <primary>item 1</primary>
  <secondary>item 2</secondary>
  <secondary>item 3</secondary>
  <teritary level="1">item 4</teritary>
</entry>
<entry>
  <primary>item 1</primary>
  <secondary>item 2</secondary>
  <secondary>item 3</secondary>
  <teritary level="1">item 4</teritary>
</entry>
<entry>
  <primary>item 1</primary>
  <secondary>item 2</secondary>
  <secondary>item 3</secondary>
  <teritary level="1">item 4</teritary>
  <teritary level="1">item 4</teritary>
  <secondary>item 2</secondary>
  <secondary>item 3</secondary>
</entry>
</div>
</Notes>

请帮助我

先谢谢

1 个答案:

答案 0 :(得分:0)

以下是执行换行的样式表(diventry):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:output indent="yes"/>

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

    <xsl:template match="section">
        <xsl:copy>
            <xsl:for-each-group select="*" group-starting-with="Head">
                <xsl:choose>
                    <xsl:when test="self::Head">
                        <div>
                            <title>
                                <xsl:apply-templates/>
                            </title>
                            <xsl:for-each-group select="current-group() except ." group-starting-with="ListItem1">
                                <entry>
                                    <xsl:apply-templates select="current-group()"/>
                                </entry>
                            </xsl:for-each-group>
                        </div>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="current-group()"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="ListItem1">
        <primary>
            <xsl:apply-templates/>
        </primary>
    </xsl:template>

    <xsl:template match="ListItem2">
        <secondary>
            <xsl:apply-templates/>
        </secondary>
    </xsl:template>

    <xsl:template match="ListItem3 | ListItem4">
        <tertiary>
            <xsl:apply-templates/>
        </tertiary>
    </xsl:template>

</xsl:stylesheet>

输出

<?xml version="1.0" encoding="UTF-8"?>
<section>
   <p>Trying text</p>
   <div>
      <title>Heading 1</title>
      <entry>
         <primary>item 1</primary>
         <secondary>item 2</secondary>
         <secondary>item 3</secondary>
         <tertiary>item 4</tertiary>
         <tertiary>item 4</tertiary>
      </entry>
      <entry>
         <primary>item 1</primary>
         <secondary>item 2</secondary>
         <secondary>item 3</secondary>
         <tertiary>item 4</tertiary>
         <tertiary>item 4</tertiary>
         <tertiary>item 4</tertiary>
         <tertiary>item 4</tertiary>
      </entry>
      <entry>
         <primary>item 1</primary>
         <secondary>item 2</secondary>
         <secondary>item 3</secondary>
         <tertiary>item 4</tertiary>
         <tertiary>item 4</tertiary>
         <secondary>item 2</secondary>
         <secondary>item 3</secondary>
      </entry>
   </div>
   <div>
      <title>Heading 2</title>
      <entry>
         <primary>item 1</primary>
         <secondary>item 2</secondary>
         <secondary>item 3</secondary>
         <tertiary>item 4</tertiary>
      </entry>
      <entry>
         <primary>item 1</primary>
         <secondary>item 2</secondary>
         <secondary>item 3</secondary>
         <tertiary>item 4</tertiary>
      </entry>
      <entry>
         <primary>item 1</primary>
         <secondary>item 2</secondary>
         <secondary>item 3</secondary>
         <tertiary>item 4</tertiary>
         <tertiary>item 4</tertiary>
         <secondary>item 2</secondary>
         <secondary>item 3</secondary>
      </entry>
   </div>
</section>

到目前为止,我还没有看到level属性来自哪个明确的解释,或者是将其添加到哪些元素的规则,似乎正在使用

<xsl:template match="ListItem3">
    <tertiary level="1">
        <xsl:apply-templates/>
    </tertiary>
</xsl:template>

<xsl:template match="ListItem4">
    <tertiary level="2">
        <xsl:apply-templates/>
    </tertiary>
</xsl:template>

而不是

    <xsl:template match="ListItem3 | ListItem4">
        <tertiary>
            <xsl:apply-templates/>
        </tertiary>
    </xsl:template>

为您的样品提供正确的结果。