check condition in xslt

大憨熊 提交于 2019-12-02 08:35:31

问题


Below is the input XML (Little Big) sorry for the bigger input XML and as well as output xml

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

Below is my Output Xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<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>
        <toc>
          <sample>
           <original>Note Lesson</orginal>
          </sample>
        </toc>
     </toc1>
     <toc2>
        <toc>
           <chapter>chapter</chapter>
           <unit>unit 1</unit>
           <pages>page</pages>
        </toc>
        <toc>
          <sample>
           <original>description page</orginal>
          </sample>
        </toc>
         <toc>
           <chapter>chapter</chapter>
           <unit>unit 10</unit>
           <pages>page</pages>
        </toc>
     </toc2>
  </social>

It's quiet big output XML file sorry for that.

In unit if i have unit 1 in output it will displayed as unit 1 but for example if i have unit10~ it will displayed as unit 10 the ~ has to removed if there is no value in by default it has to displayed unit 10.

Little Brief Explanation

My Output XML has to differentiate in three categories

1) Chapter

2) unit

3) pages

The input will be in three different types of formats

1) The XML have Chapter,unit(number with tilda symbol) & pages

2) The XML have Chapter,unit(number without tilda symbol) & pages

3) The XML have only pages ex..(note & description) so here if for example i have 10~(unit) the output will show 10, if the input xml doesn't have the value (for unit) in output xml it will show 10 as default number – karthic yesterday

Please help me and guide me with the help of XSLT.

Regards Karthic


回答1:


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>



回答2:


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



来源:https://stackoverflow.com/questions/11499010/check-condition-in-xslt

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