Xml xslt中的条件检查

Xml xslt中的条件检查,xml,xslt-1.0,xslt-2.0,Xml,Xslt 1.0,Xslt 2.0,在我之前的问题中有一点更新@Dimetre已经回答了这个问题 输入XML <tutorial> <lessons> <lesson> chapter Bat 20 </lesson> <lesson> chapter Pen Ball 10~ </lesson> <lesson> chapter Book </lesso

在我之前的问题中有一点更新@Dimetre已经回答了这个问题

输入XML

<tutorial>
<lessons>
   <lesson>
     chapter Bat 20 
</lesson>
    <lesson>
        chapter Pen Ball 10~ 
    </lesson>
    <lesson>
        chapter Book 
    </lesson>
    <lesson>
        note lesson
    </lesson>
 <lessons1>
    <lesson>
        chapter Pencil 10
    </lesson>
    <lesson>
        description page
    </lesson>
    <lesson>
        chapter Car Tank 25
    </lesson>
</lessons1>
</lessons>

第20章
第十章钢笔球
章节书
笔记课
第十章铅笔
说明页
第25章汽车油箱

输出将是

<Geography>


<historical>
  <social>
     <toc1>
        <toc>
           <chapter>chapter</chapter>
           <unit>Bat</unit>
           <pages>20</pages>
        </toc>
        <toc>
           <chapter>chapter</chapter>
           <unit>Pen Ball</unit>
           <pages>10</pages>
        </toc>
        <toc>
           <chapter>chapter</chapter>
           <unit>Book</unit>
           <pages>10</pages>
        </toc>
        <sample>
           <original>note lesson</original>
        </sample>
     </toc1>
     <toc2>
        <toc>
           <chapter>chapter</chapter>
           <unit>Pencil</unit>
           <pages>10</pages>
        </toc>
        <sample>
           <original>description page</original>
        </sample>
        <toc>
           <chapter>chapter</chapter>
           <unit>Car Tank</unit>
           <pages>25</pages>
        </toc>
     </toc2>
  </social>

章
球棒
20
章
钢笔球
10
章
书
10
笔记课
章
铅笔
10
说明页
章
汽车油箱
25

在这里,我用输入XML作为示例,写了两节课(第一课,第1课),但实际上它有n节课。我想我要求的更多,但我同时也在学习

请帮助我,指引我

提前谢谢

问候
Karthic

这是您自己的XML格式吗?通过将课程编号放入课程元素,您似乎在限制自己的数据(或删除结构)。如果您使用XSL来处理它,那么每次添加新课程时,您都必须更新XSL以应对新类型,或者制作一个复杂的样式表。与此相反,使用属性不是更好吗?因此,不要将第一课和第一课用于你所学的章节

 <lesson id='1'>
  ....
 </lesson>
 <lesson id='2'>
 ....
 </lesson>

....
....

如果你有1节课或50000节课,那么你就不必更改代码,你可以使用dimitre为你编写的修改版本

这是你自己的XML格式吗?通过将课程编号放入课程元素,您似乎在限制自己的数据(或删除结构)。如果您使用XSL来处理它,那么每次添加新课程时,您都必须更新XSL以应对新类型,或者制作一个复杂的样式表。与此相反,使用属性不是更好吗?因此,不要将第一课和第一课用于你所学的章节

 <lesson id='1'>
  ....
 </lesson>
 <lesson id='2'>
 ....
 </lesson>

....
....

如果你有1节课或50000节课,你就不必更改代码,你可以使用dimitre为你编写的修改版

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
            <xsl:output omit-xml-declaration="yes" indent="yes"/>
            <xsl:strip-space elements="*"/>

         <xsl:template match="tutorial">
            <Geography>
              <historical>
                <social>
                     <xsl:apply-templates select=
                     "*[starts-with(name(),'lessons')]"/>
                </social>
              </historical>
            </Geography>
         </xsl:template>

         <xsl:template match="*[starts-with(name(), 'lessons')]">
          <xsl:variable name="vPos" select="position()"/>

          <xsl:element name="toc{$vPos}">
           <xsl:apply-templates/>
          </xsl:element>

         </xsl:template>

         <xsl:template match="lesson[starts-with(normalize-space(), 'chapter')]">
          <xsl:variable name="vNorm" select=
                         "translate(normalize-space(), '~', '')"/>
          <xsl:variable name="vAtUnit" select=
                         "substring-after($vNorm, 'chapter')"/>

          <xsl:variable name="vUnit" select=
          "replace($vAtUnit, '([^0123456789]+)(\d*)', '$1')"/>

          <xsl:variable name="vLastPart" as="xs:string" select=
           "substring-after($vAtUnit, $vUnit)"/>

          <xsl:variable name="vNum"
            select="concat($vLastPart, '10'[not($vLastPart)])"/>

          <toc>
            <chapter>chapter</chapter>
            <unit><xsl:value-of select="normalize-space($vUnit)"/></unit>
            <pages><xsl:value-of select="$vNum"/></pages>
          </toc>
         </xsl:template>

         <xsl:template match="lesson">
           <toc>
               <sample>
                 <original><xsl:value-of select="normalize-space()"/></original>
               </sample>
           </toc>
         </xsl:template>
</xsl:stylesheet>
<Geography>
   <historical>
      <social>
         <toc1>
            <toc>
               <chapter>chapter</chapter>
               <unit>Bat</unit>
               <pages>20</pages>
            </toc>
            <toc>
               <chapter>chapter</chapter>
               <unit>Pen Ball</unit>
               <pages>10</pages>
            </toc>
            <toc>
               <chapter>chapter</chapter>
               <unit>Book</unit>
               <pages>10</pages>
            </toc>
            <toc>
               <sample>
                  <original>note lesson</original>
               </sample>
            </toc>
         </toc1>
         <toc2>
            <toc>
               <chapter>chapter</chapter>
               <unit>Pencil</unit>
               <pages>10</pages>
            </toc>
            <toc>
               <sample>
                  <original>description page</original>
               </sample>
            </toc>
            <toc>
               <chapter>chapter</chapter>
               <unit>Car Tank</unit>
               <pages>25</pages>
            </toc>
         </toc2>
      </social>
   </historical>
</Geography>
生成所需的正确结果

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
            <xsl:output omit-xml-declaration="yes" indent="yes"/>
            <xsl:strip-space elements="*"/>

         <xsl:template match="tutorial">
            <Geography>
              <historical>
                <social>
                     <xsl:apply-templates select=
                     "*[starts-with(name(),'lessons')]"/>
                </social>
              </historical>
            </Geography>
         </xsl:template>

         <xsl:template match="*[starts-with(name(), 'lessons')]">
          <xsl:variable name="vPos" select="position()"/>

          <xsl:element name="toc{$vPos}">
           <xsl:apply-templates/>
          </xsl:element>

         </xsl:template>

         <xsl:template match="lesson[starts-with(normalize-space(), 'chapter')]">
          <xsl:variable name="vNorm" select=
                         "translate(normalize-space(), '~', '')"/>
          <xsl:variable name="vAtUnit" select=
                         "substring-after($vNorm, 'chapter')"/>

          <xsl:variable name="vUnit" select=
          "replace($vAtUnit, '([^0123456789]+)(\d*)', '$1')"/>

          <xsl:variable name="vLastPart" as="xs:string" select=
           "substring-after($vAtUnit, $vUnit)"/>

          <xsl:variable name="vNum"
            select="concat($vLastPart, '10'[not($vLastPart)])"/>

          <toc>
            <chapter>chapter</chapter>
            <unit><xsl:value-of select="normalize-space($vUnit)"/></unit>
            <pages><xsl:value-of select="$vNum"/></pages>
          </toc>
         </xsl:template>

         <xsl:template match="lesson">
           <toc>
               <sample>
                 <original><xsl:value-of select="normalize-space()"/></original>
               </sample>
           </toc>
         </xsl:template>
</xsl:stylesheet>
<Geography>
   <historical>
      <social>
         <toc1>
            <toc>
               <chapter>chapter</chapter>
               <unit>Bat</unit>
               <pages>20</pages>
            </toc>
            <toc>
               <chapter>chapter</chapter>
               <unit>Pen Ball</unit>
               <pages>10</pages>
            </toc>
            <toc>
               <chapter>chapter</chapter>
               <unit>Book</unit>
               <pages>10</pages>
            </toc>
            <toc>
               <sample>
                  <original>note lesson</original>
               </sample>
            </toc>
         </toc1>
         <toc2>
            <toc>
               <chapter>chapter</chapter>
               <unit>Pencil</unit>
               <pages>10</pages>
            </toc>
            <toc>
               <sample>
                  <original>description page</original>
               </sample>
            </toc>
            <toc>
               <chapter>chapter</chapter>
               <unit>Car Tank</unit>
               <pages>25</pages>
            </toc>
         </toc2>
      </social>
   </historical>
</Geography>

章
球棒
20
章
钢笔球
10
章
书
10
笔记课
章
铅笔
10
说明页
章
汽车油箱
25

此转换:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
            <xsl:output omit-xml-declaration="yes" indent="yes"/>
            <xsl:strip-space elements="*"/>

         <xsl:template match="tutorial">
            <Geography>
              <historical>
                <social>
                     <xsl:apply-templates select=
                     "*[starts-with(name(),'lessons')]"/>
                </social>
              </historical>
            </Geography>
         </xsl:template>

         <xsl:template match="*[starts-with(name(), 'lessons')]">
          <xsl:variable name="vPos" select="position()"/>

          <xsl:element name="toc{$vPos}">
           <xsl:apply-templates/>
          </xsl:element>

         </xsl:template>

         <xsl:template match="lesson[starts-with(normalize-space(), 'chapter')]">
          <xsl:variable name="vNorm" select=
                         "translate(normalize-space(), '~', '')"/>
          <xsl:variable name="vAtUnit" select=
                         "substring-after($vNorm, 'chapter')"/>

          <xsl:variable name="vUnit" select=
          "replace($vAtUnit, '([^0123456789]+)(\d*)', '$1')"/>

          <xsl:variable name="vLastPart" as="xs:string" select=
           "substring-after($vAtUnit, $vUnit)"/>

          <xsl:variable name="vNum"
            select="concat($vLastPart, '10'[not($vLastPart)])"/>

          <toc>
            <chapter>chapter</chapter>
            <unit><xsl:value-of select="normalize-space($vUnit)"/></unit>
            <pages><xsl:value-of select="$vNum"/></pages>
          </toc>
         </xsl:template>

         <xsl:template match="lesson">
           <toc>
               <sample>
                 <original><xsl:value-of select="normalize-space()"/></original>
               </sample>
           </toc>
         </xsl:template>
</xsl:stylesheet>
<Geography>
   <historical>
      <social>
         <toc1>
            <toc>
               <chapter>chapter</chapter>
               <unit>Bat</unit>
               <pages>20</pages>
            </toc>
            <toc>
               <chapter>chapter</chapter>
               <unit>Pen Ball</unit>
               <pages>10</pages>
            </toc>
            <toc>
               <chapter>chapter</chapter>
               <unit>Book</unit>
               <pages>10</pages>
            </toc>
            <toc>
               <sample>
                  <original>note lesson</original>
               </sample>
            </toc>
         </toc1>
         <toc2>
            <toc>
               <chapter>chapter</chapter>
               <unit>Pencil</unit>
               <pages>10</pages>
            </toc>
            <toc>
               <sample>
                  <original>description page</original>
               </sample>
            </toc>
            <toc>
               <chapter>chapter</chapter>
               <unit>Car Tank</unit>
               <pages>25</pages>
            </toc>
         </toc2>
      </social>
   </historical>
</Geography>
生成所需的正确结果

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
            <xsl:output omit-xml-declaration="yes" indent="yes"/>
            <xsl:strip-space elements="*"/>

         <xsl:template match="tutorial">
            <Geography>
              <historical>
                <social>
                     <xsl:apply-templates select=
                     "*[starts-with(name(),'lessons')]"/>
                </social>
              </historical>
            </Geography>
         </xsl:template>

         <xsl:template match="*[starts-with(name(), 'lessons')]">
          <xsl:variable name="vPos" select="position()"/>

          <xsl:element name="toc{$vPos}">
           <xsl:apply-templates/>
          </xsl:element>

         </xsl:template>

         <xsl:template match="lesson[starts-with(normalize-space(), 'chapter')]">
          <xsl:variable name="vNorm" select=
                         "translate(normalize-space(), '~', '')"/>
          <xsl:variable name="vAtUnit" select=
                         "substring-after($vNorm, 'chapter')"/>

          <xsl:variable name="vUnit" select=
          "replace($vAtUnit, '([^0123456789]+)(\d*)', '$1')"/>

          <xsl:variable name="vLastPart" as="xs:string" select=
           "substring-after($vAtUnit, $vUnit)"/>

          <xsl:variable name="vNum"
            select="concat($vLastPart, '10'[not($vLastPart)])"/>

          <toc>
            <chapter>chapter</chapter>
            <unit><xsl:value-of select="normalize-space($vUnit)"/></unit>
            <pages><xsl:value-of select="$vNum"/></pages>
          </toc>
         </xsl:template>

         <xsl:template match="lesson">
           <toc>
               <sample>
                 <original><xsl:value-of select="normalize-space()"/></original>
               </sample>
           </toc>
         </xsl:template>
</xsl:stylesheet>
<Geography>
   <historical>
      <social>
         <toc1>
            <toc>
               <chapter>chapter</chapter>
               <unit>Bat</unit>
               <pages>20</pages>
            </toc>
            <toc>
               <chapter>chapter</chapter>
               <unit>Pen Ball</unit>
               <pages>10</pages>
            </toc>
            <toc>
               <chapter>chapter</chapter>
               <unit>Book</unit>
               <pages>10</pages>
            </toc>
            <toc>
               <sample>
                  <original>note lesson</original>
               </sample>
            </toc>
         </toc1>
         <toc2>
            <toc>
               <chapter>chapter</chapter>
               <unit>Pencil</unit>
               <pages>10</pages>
            </toc>
            <toc>
               <sample>
                  <original>description page</original>
               </sample>
            </toc>
            <toc>
               <chapter>chapter</chapter>
               <unit>Car Tank</unit>
               <pages>25</pages>
            </toc>
         </toc2>
      </social>
   </historical>
</Geography>

章
球棒
20
章
钢笔球
10
章
书
10
笔记课
章
铅笔
10
说明页
章
汽车油箱
25

hi@Woody这不是我的XML格式,我正在使用这种XML格式。hi@Woody这不是我的XML格式,我正在使用这种XML格式。hi@Dimitre我很抱歉,有相同问题的小更新,请帮助我>@karthic:那么,更新是什么?你为什么不提交一个新问题?嗨@Dimitre我已经主持了一个新问题,这是链接嗨@Dimitre我很抱歉有相同问题的小更新请帮助我>@karthic:那么,更新是什么?你为什么不提交一个新问题?嗨@Dimitre我已经主持了一个新问题,这里是链接