使用 xsl:for-each-group [英] using xsl:for-each-group

查看:23
本文介绍了使用 xsl:for-each-group的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的要求.我的示例输入文档如下所示.(我加了白线说清楚)

Here is my requirement. My sample input document is like below. (I have added white lines to make it clear)

<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>

现在我想要的输出是这个.

Now my wanted output is this.

<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="section-title" 的行应该在 name="section-desc" 之前
  • h1、h2、h3 等应该在 section-desc 之后.

我在这里解决了h1、h2、h3等问题.我知道这是非常有的.任何帮助都很棒.

I solved the problem of h1,h2,h3,etc here. I know this is very had. Any help is great.

如果违反这些规则,则不应发生转换.}

The transformation should not happen if these rules violate. }

推荐答案

这里是我对之前发布的样式表的改编,它只是在调用递归函数分组之前做了一个额外的group-starting-with水平.我意识到这与您在之前的帖子中提出的建议相同,但到目前为止我不清楚为什么该建议对您不起作用.

Here is my adaption of the previously posted stylesheet, it simply does an additional group-starting-with before calling the recursive function grouping the levels. I realize that is kind of the same suggestion as in an earlier post you made but so far it is not clear to me why that suggestion does not work for you.

这里是样式表:

<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>

当我将带有 Saxon 9.4 的样式表应用到更正后的输入时

When I apply that stylesheet with Saxon 9.4 to the corrected input

<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>,两个属性同名是不可能的.

Please next time when you provide an input sample make sure it is well-formed, so far you have always posted stuff like <p name="h2-title " name="other-h2">XQuery</p>, and two attributes of the same name are not possible.

这篇关于使用 xsl:for-each-group的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆