check condition in xslt

∥☆過路亽.° 提交于 2019-12-02 05:51:26

This transformation:

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

      <xsl:variable name="vNames" select="'chapter', 'unit', 'pages'"/>

     <xsl:template match="lessons">
        <Geography>
          <historical>
            <social>
               <toc1>
                 <xsl:apply-templates select="lesson"/>
               </toc1>
               <xsl:apply-templates select="lessons1"/>
            </social>
          </historical>
        </Geography>
     </xsl:template>

     <xsl:template match="lesson[starts-with(normalize-space(), 'chapter')]">
      <xsl:variable name="vNorm" select=
                     "translate(normalize-space(), '~', '')"/>
      <xsl:variable name="vAtNumber" select=
                     "substring-after($vNorm, 'chapter unit')"/>
      <xsl:variable name="vNum" select=
       "if(matches($vAtNumber, '^\s*\d+'))
          then replace($vAtNumber, '(^\s*(\d+)).*$', '$2')
          else '10'
       "/>
      <xsl:analyze-string select="."
       regex="(chapter\s+)(unit\s*)(((\d*~?)\s+)?page)">
        <xsl:matching-substring>
          <toc>
             <chapter>chapter</chapter>
             <unit>unit <xsl:value-of select="$vNum"/></unit>
             <pages>page</pages>
          </toc>
        </xsl:matching-substring>
      </xsl:analyze-string>
     </xsl:template>

     <xsl:template match="lesson">
       <sample>
         <original><xsl:value-of select="normalize-space()"/></original>
       </sample>
     </xsl:template>

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

when applied on the provided XML document:

<tutorial>
    <lessons>
       <lesson>
         chapter unit 1 page
    </lesson>
        <lesson>
            chapter unit 10~ page
        </lesson>
        <lesson>
            chapter unit page
        </lesson>
        <lesson>
            note lesson
        </lesson>
     <lessons1>
        <lesson>
            chapter unit 1 page
        </lesson>
        <lesson>
            description page
        </lesson>
        <lesson>
            chapter unit page
        </lesson>
    </lessons1>
    </lessons>
</tutorial>

produces the wanted, correct result:

<Geography>
   <historical>
      <social>
         <toc1>
            <toc>
               <chapter>chapter</chapter>
               <unit>unit 1</unit>
               <pages>page</pages>
            </toc>
            <toc>
               <chapter>chapter</chapter>
               <unit>unit 10</unit>
               <pages>page</pages>
            </toc>
            <toc>
               <chapter>chapter</chapter>
               <unit>unit 10</unit>
               <pages>page</pages>
            </toc>
            <sample>
               <original>note lesson</original>
            </sample>
         </toc1>
         <toc2>
            <toc>
               <chapter>chapter</chapter>
               <unit>unit 1</unit>
               <pages>page</pages>
            </toc>
            <sample>
               <original>description page</original>
            </sample>
            <toc>
               <chapter>chapter</chapter>
               <unit>unit 10</unit>
               <pages>page</pages>
            </toc>
         </toc2>
      </social>
   </historical>
</Geography>

If you just want to remove the "~", use translate(xxx, '~', '')

Beyond that, I'm afraid you're not making your requirements clear. For example, I don't understand this clause:

if there is no value in by default it has to displayed unit 10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!