使用的xsl:for-各组(using xsl:for-each-group)

2019-09-20 08:41发布

这里是我的要求。 我的样品输入文档是像下面。 (我已经添加了白线,使之清楚)

<body>
       <p name="h-title" other="main">Introduction</p>
       <p name="h-titledesc " other="other-desc">XSLT and XQuery</p>


       <p name=""> XSLT is used to write stylesheets.</p>
    <p name="section-title" other=" other-section">XSLT</p>
    <p name="section-desc" other=" other-sectionsdesc">XSLT</p>
    <p name=""> Some text.</p>
    <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
    <p name="h1-title" other=" other-h1">XSLT</p>
    <p name=""> Some text.</p>
       <p name="h2-title " name="other-h2">XQuery</p>
       <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
       <p name="h3-title" name="other-h3">XQuery and stylesheets</p>
       <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>


    <p name="section-title" other=" other-section">XSLT</p>
    <p name="section-desc" other=" other-sectionsdesc">XSLT</p>
    <p name=""> Some text.</p>
    <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
    <p name="h1-title" other=" other-h1">XSLT</p>
    <p name=""> Some text.</p>
       <p name="h2-title " name="other-h2">XQuery</p>
       <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
       <p name="h3-title" name="other-h3">XQuery and stylesheets</p>
       <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>


       <p name ="summary-title">this is summary</p>
       <p name="summary-desc " other="other-summarydesc">the summary</p>
    </body>

现在我想要的输出是这样的。

<body>
       <p name="h-title" other="main">Introduction</p>
       <p name="h-titledesc " other="other-desc">XSLT and XQuery</p>


       <p name=""> XSLT is used to write stylesheets.</p>

    <body-contents>
        <p name="section-title" other=" other-section">XSLT</p>
        <p name="section-desc" other=" other-sectionsdesc">XSLT</p>
        <p name=""> Some text.</p>
        <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
        <h1>
        <p name="h1-title" other=" other-h1">XSLT</p>
        <p name=""> Some text.</p>
        <h2>
           <p name="h2-title " name="other-h2">XQuery</p>
           <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
        <h3>
           <p name="h3-title" name="other-h3">XQuery and stylesheets</p>
           <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
        </h3>
    </h2>
    </h1>
    </body-contents>
    <body-contents>
        <p name="section-title" other=" other-section">XSLT</p>
        <p name="section-desc" other=" other-sectionsdesc">XSLT</p>
        <p name=""> Some text.</p>
        <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
        <h1>
        <p name="h1-title" other=" other-h1">XSLT</p>
        <p name=""> Some text.</p>
        <h2>
           <p name="h2-title " name="other-h2">XQuery</p>
           <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
        <h3>
           <p name="h3-title" name="other-h3">XQuery and stylesheets</p>
           <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
        </h3>
    </h2>
    </h1>

    </body-contents>
    <body-contents>
           <p name ="summary-title">this is summary</p>
           <p name="summary-desc " other="other-summarydesc">the summary</p>
    </body-contents>
    </body>

请帮我解决这个问题。

{ 选用有类似的限制:

  • H1,H2,H3顺序来(这意味着,H3不h1和h2之间来)
  • 与NAME =“节标题”行应该来名之前=“部分,说明”
  • H1,H2,H3,等以后要来的部分,递减。

我解决了H1,H2,H3等问题在这里 。 我知道这是很了。 任何帮助是巨大的。

如果这些规则违反转型不应该发生。 }

Answer 1:

这是我在以前发布的样式表的适应,它只是做一个额外的group-starting-with调用递归函数的分组之前的水平。 我意识到这是一种相同的建议,在你做,但到目前为止,为什么该建议不适合你的工作是不是很清楚,我先前的职位。

因此,这里的样式表:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:mf="http://example.com/mf"
  exclude-result-prefixes="xs mf">

<xsl:param name="prefix" as="xs:string" select="'h'"/>
<xsl:param name="suffix" as="xs:string" select="'-title'"/>

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

<xsl:function name="mf:group" as="node()*">
  <xsl:param name="items" as="node()*"/>
  <xsl:param name="level" as="xs:integer"/>
  <xsl:for-each-group select="$items" group-starting-with="p[@name = concat($prefix, $level, $suffix)]">
    <xsl:choose>
      <xsl:when test="not(self::p[@name = concat($prefix, $level, $suffix)])">
        <xsl:apply-templates select="current-group()"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:element name="h{$level}">
          <xsl:apply-templates select="."/>
          <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/>
        </xsl:element>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each-group>
</xsl:function>

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

<xsl:template match="body">
  <xsl:copy>
    <xsl:for-each-group select="*" group-starting-with="p[@name = 'section-title' or @name = 'summary-title']">
      <xsl:choose>
        <xsl:when test="not(self::p[@name = 'section-title' or @name = 'summary-title'])">
          <xsl:apply-templates select="current-group()"/>
        </xsl:when>
        <xsl:otherwise>
          <body-contents>
             <xsl:sequence select="mf:group(current-group(), 1)"/>
          </body-contents>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

当我与撒克逊9.4应用该样式表来校正输入

<body>
       <p name="h-title" other="main">Introduction</p>
       <p name="h-titledesc " other="other-desc">XSLT and XQuery</p>


       <p name=""> XSLT is used to write stylesheets.</p>
    <p name="section-title" other=" other-section">XSLT</p>
    <p name="section-desc" other=" other-sectionsdesc">XSLT</p>
    <p name=""> Some text.</p>
    <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
    <p name="h1-title" other=" other-h1">XSLT</p>
    <p name=""> Some text.</p>
       <p name="h2-title" other="other-h2">XQuery</p>
       <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
       <p name="h3-title" other="other-h3">XQuery and stylesheets</p>
       <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>


    <p name="section-title" other=" other-section">XSLT</p>
    <p name="section-desc" other=" other-sectionsdesc">XSLT</p>
    <p name=""> Some text.</p>
    <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
    <p name="h1-title" other=" other-h1">XSLT</p>
    <p name=""> Some text.</p>
       <p name="h2-title" other="other-h2">XQuery</p>
       <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
       <p name="h3-title" other="other-h3">XQuery and stylesheets</p>
       <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>


       <p name ="summary-title">this is summary</p>
       <p name="summary-desc " other="other-summarydesc">the summary</p>
    </body>

我得到的输出

<body>
   <p name="h-title" other="main">Introduction</p>
   <p name="h-titledesc " other="other-desc">XSLT and XQuery</p>
   <p name=""> XSLT is used to write stylesheets.</p>
   <body-contents>
      <p name="section-title" other=" other-section">XSLT</p>
      <p name="section-desc" other=" other-sectionsdesc">XSLT</p>
      <p name=""> Some text.</p>
      <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
      <h1>
         <p name="h1-title" other=" other-h1">XSLT</p>
         <p name=""> Some text.</p>
         <h2>
            <p name="h2-title" other="other-h2">XQuery</p>
            <p name="">
               <p1 name="bold"> XQuery is used to query XML databases.</p1>
            </p>
            <h3>
               <p name="h3-title" other="other-h3">XQuery and stylesheets</p>
               <p name="">
                  <p1 name="bold"> XQuery is used to query XML databases.</p1>
               </p>
            </h3>
         </h2>
      </h1>
   </body-contents>
   <body-contents>
      <p name="section-title" other=" other-section">XSLT</p>
      <p name="section-desc" other=" other-sectionsdesc">XSLT</p>
      <p name=""> Some text.</p>
      <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
      <h1>
         <p name="h1-title" other=" other-h1">XSLT</p>
         <p name=""> Some text.</p>
         <h2>
            <p name="h2-title" other="other-h2">XQuery</p>
            <p name="">
               <p1 name="bold"> XQuery is used to query XML databases.</p1>
            </p>
            <h3>
               <p name="h3-title" other="other-h3">XQuery and stylesheets</p>
               <p name="">
                  <p1 name="bold"> XQuery is used to query XML databases.</p1>
               </p>
            </h3>
         </h2>
      </h1>
   </body-contents>
   <body-contents>
      <p name="summary-title">this is summary</p>
      <p name="summary-desc " other="other-summarydesc">the summary</p>
   </body-contents>
</body>

请下一次,当你提供一个输入样本确保它是良好的,到目前为止,你一直贴这样的东西<p name="h2-title " name="other-h2">XQuery</p>和两个同名的属性是不可能的。



文章来源: using xsl:for-each-group