xslt-1.0使用<;创建xml标记;h1>&&书信电报;h2>;

xslt-1.0使用<;创建xml标记;h1>&&书信电报;h2>;,xml,xslt,xslt-1.0,xslt-grouping,Xml,Xslt,Xslt 1.0,Xslt Grouping,我对xslt非常陌生。我想把下面的输入转换成输出。我想将h1和h2元素分组,创建属性,并在2个h1或h2标记之间添加数据作为子标记值。请帮忙。谢谢 input.xml <ATTRIBUTE-VALUE> <THE-VALUE> <div xmlns="http://www.w3.org/1999/xhtml"> <h1 dir="ltr" id="_1536217498885">Main Descri

我对xslt非常陌生。我想把下面的输入转换成输出。我想将h1和h2元素分组,创建属性,并在2个h1或h2标记之间添加数据作为子标记值。请帮忙。谢谢

input.xml

<ATTRIBUTE-VALUE>
    <THE-VALUE>
        <div xmlns="http://www.w3.org/1999/xhtml">
            <h1 dir="ltr" id="_1536217498885">Main Description</h1>
            <p>Line1 The main description text goes here.</p>
            <p>Line2 The main description text goes here.</p>
            <h1 dir="ltr" id="_1536217498886">Steps</h1>
            <h2 dir="ltr" id="_1536217498886">Section Name One</h2>
            <p>Line1 The steps text goes here.</p>
            <ul>Line2 The section description text goes here.</ul>
            <h2 dir="ltr" id="_1536217498886">Section Name Two</h2>
            <p>Line1 The key consideration text goes here.</p>
            <ul><li>Line2 The key consideration text goes here.</li></ul>
            <h1 dir="ltr" id="_1536217498887">Skills</h1>
            <p>Line1 The Skills text goes here.</p>
            <p>Line2 The Skills text goes here.</p>
            <p>Line3 The Skills text goes here.</p>
            <h1 dir="ltr" id="_1536217498888">Synonyms</h1>
            <p>The Synonyms text goes here.</p>
        </div>
    </THE-VALUE>
</ATTRIBUTE-VALUE>

主要描述
第1行主要描述文本显示在此处

第2行主要说明文字如下

台阶 节名一 第1行步骤文本位于此处

    第2行此处是章节说明文字。
章节名称二 第1行关键注意事项文本如下

  • 第2行关键注意事项文本如下。
    • 技能 第1行技能文本在这里

      第2行技能文本在这里

      第3行技能文本在这里

      同义词 同义词文本在这里

output.xml

<MainDescription>
    <![CDATA[
        <p>Line1 The main description text goes here.</p>
        <p>Line2 The main description text goes here.</p>
    ]]>
</MainDescription>
<Section name="Section Name One">
    <Description>
        <p>Line1 The section text goes here.</p>
        <p>Line2 The description text goes here.</p>
    </Description>
</Section>
<Section name="Section Name Two">
    <Description>
        <p>Line1 The description text goes here.</p>
        <ul><li>Line2 The description text goes here.</li></ul>
    </Description>
</Section>
<Skills>
    <p>Line1 The Skills text goes here.</p>
    <p>Line2 The Skills text goes here.</p>
    <p>Line3 The Skills text goes here.</p>
</Skills>
<Synonyms>
    <p>The Synonyms text goes here.</p>
</Synonyms>

第1行主要描述文本显示在此处

第2行主要说明文字如下

]]> 第1行本节文本位于此处

第2行描述文本位于此处

第1行描述文本位于此处

  • 第2行描述文本在这里。
    • 第1行技能文本在这里

      第2行技能文本在这里

      第3行技能文本在这里

      同义词文本在这里

我使用了下面的xsl对标记进行分组,但我无法获得output.xml所需的内容。如果我对h1和h2都应用相同的模板,我只会得到h1或h2的结果,并使用下面的xsl

xsl文件:

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    xmlns:exsl="http://exslt.org/common"
    exclude-result-prefixes="xhtml exsl"
    version="1.0">

  <xsl:import href="http://lenzconsulting.com/xml-to-string/xml-to-string.xsl"/>

  <xsl:output method="xml" indent="yes"
    cdata-section-elements="MainDescription KeyConsideration"/>
  <xsl:strip-space elements="*"/>

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

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

  <xsl:key name="h1-group" match="xhtml:div/*[not(self::xhtml:h1)]" use="generate-id(preceding-sibling::xhtml:h1[1])"/>

  <xsl:template match="xhtml:div[xhtml:h1]">
      <xsl:apply-templates select="xhtml:h1"/>
  </xsl:template>

  <xsl:template match="xhtml:h1">
      <xsl:element name="{translate(., ' ', '')}">
          <xsl:variable name="rtf-with-xhtml-ns-stripped">
              <xsl:apply-templates select="key('h1-group', generate-id())"/>
          </xsl:variable>
          <xsl:apply-templates select="exsl:node-set($rtf-with-xhtml-ns-stripped)/node()" mode="xml-to-string"/>
      </xsl:element>
  </xsl:template>

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

</xsl:stylesheet>



我认为您需要为
h2
分组定义第二个键

<xsl:key name="h2-group" match="xhtml:div/*[not(self::xhtml:h2)]" use="generate-id(preceding-sibling::xhtml:h2[1])"/>
在模板匹配
h2
中,您可以进行分组

<xsl:template match="xhtml:h2">
  <xsl:variable name="h1Group" select="key('h1-group', generate-id(preceding-sibling::xhtml:h1[1]))" />
  <Section name="{.}">
    <Description>
      <xsl:apply-templates select="key('h2-group', generate-id())[count(. | $h1Group) = count($h1Group)]"/>
   </Description>
  </Section>
</xsl:template>

我认为您需要为
h2
分组定义第二个键

<xsl:key name="h2-group" match="xhtml:div/*[not(self::xhtml:h2)]" use="generate-id(preceding-sibling::xhtml:h2[1])"/>
在模板匹配
h2
中,您可以进行分组

<xsl:template match="xhtml:h2">
  <xsl:variable name="h1Group" select="key('h1-group', generate-id(preceding-sibling::xhtml:h1[1]))" />
  <Section name="{.}">
    <Description>
      <xsl:apply-templates select="key('h2-group', generate-id())[count(. | $h1Group) = count($h1Group)]"/>
   </Description>
  </Section>
</xsl:template>