checking condition xslt

感情迁移 提交于 2019-12-13 19:13:00

问题


With the previous post.

here is the link

Previous Question

Again Small Update in input xml the other validation are all same. Here only the chapter (element) is changing instead of chapter i will have numbers

<tutorial>
<lessons>
    <lesson>
     12000 Bat 20 
    </lesson>
    <lesson>
        15000 Pen Ball 10~ 
    </lesson>
    <lesson>
        14000 Book 
    </lesson>
    <lesson>
        note lesson
    </lesson>
</lessons>
<lessons1>
    <lesson>
        24000 Pencil 10
    </lesson>
    <lesson>
        description page
    </lesson>
    <lesson>
        8000 Car Tank 25
    </lesson>
</lessons1>

In the previous question we have Chapter was the first node (chapter Bat 20) but here I have 12000 bat 20

Desire output for the above input is

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

@Dimitre & @Tomalak From next time i will write fully prepared question and definitely i will post it with the solution what i have, now i am started learning little faster(XSLT) with this below output and previous output.

Please guide me here

Thanks in advance Karthic


回答1:


This transformation:

<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[substring-before(normalize-space(), ' ')
                    castable as xs:integer
                    ]">
              <xsl:variable name="vNorm" select=
                             "translate(normalize-space(), '~', '')"/>
              <xsl:variable name="vAtUnit" select=
                             "substring-after($vNorm, ' ')"/>

              <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>
                 <xsl:value-of select="substring-before($vNorm, ' ')"/>
                </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>

when applied on the provided XML document:

    <tutorial>
    <lessons>
        <lesson>
         12000 Bat 20
        </lesson>
        <lesson>
            15000 Pen Ball 10~
        </lesson>
        <lesson>
            14000 Book
        </lesson>
        <lesson>
            note lesson
        </lesson>
    </lessons>
    <lessons1>
        <lesson>
            24000 Pencil 10
        </lesson>
        <lesson>
            description page
        </lesson>
        <lesson>
            8000 Car Tank 25
        </lesson>
    </lessons1>
</tutorial>

produces the wanted, correct result:

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


来源:https://stackoverflow.com/questions/11540422/checking-condition-xslt

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