[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] WorldML Filteration


Subject: Re: [xsl] WorldML Filteration
From: Terry Badger <terry_badger@xxxxxxxxx>
Date: Mon, 18 Mar 2013 06:15:58 -0700 (PDT)

Joga,
I try again.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
version="2.0" xmlns:w="something" xmlns:tb="something-else">
    <!-- function
turns the existance or not of a w:b into something the group-adjacent can use
-->
    <xsl:strip-space elements="*"/>
    <xsl:function name="tb:switch-b">
        <xsl:param name="r"/>
        <xsl:choose>
            <xsl:when
test="boolean($r/w:rPr/w:b) = true()">1</xsl:when>
            <xsl:when
test="boolean($r/w:rPr/w:b) = false()">0</xsl:when>
        </xsl:choose>
   
</xsl:function>
   <!-- start here I put a root around the sample file called
w:p -->
    <xsl:template match="/w:p">
        <xsl:result-document
href="wordml-01-output.xml">
            <!-- with the given sample file there
will be only one group -->
            <xsl:for-each-group select="w:r"
group-adjacent="w:rPr/w:i">
                <xsl:element name="i">
                    <!-- group by bold and not bold -->
                   
<xsl:for-each-group select="current-group()/self::w:r"
group-adjacent="tb:switch-b(.)">
                        <xsl:choose>
                            <xsl:when test="current-group()//w:b">
                                <xsl:element name="b">
                                    <xsl:value-of 
select="current-group()//w:t"/>                            
                                </xsl:element>
                           
</xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of  select="current-group()//w:t"/>
                            </xsl:otherwise>
                       
</xsl:choose>
                    </xsl:for-each-group>
               
</xsl:element>
            </xsl:for-each-group>
       
</xsl:result-document>
    </xsl:template>
</xsl:stylesheet>
Terry




-----
Original Message -----
From: Joga Singh Rawat <jrawat@xxxxxxxxxxxxxx>
To:
xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Cc: 
Sent: Monday, March 18, 2013 2:14 AM
Subject: [xsl] WorldML Filteration

Hi, 
I have got a good clue about
formatting elements for worldML. In the similar
effort we need to filter these
elements. Comparison of "current output" and
"desired output" is sufficient to
understand the requirement.

Input
<w:r><w:rPr><w:i/><w:i-cs/></w:rPr><w:t>Azienda</w:t></w:r>
<w:r><w:rPr><w:i/><w:i-cs/></w:rPr><w:t> </w:t></w:r>
<w:r><w:rPr><w:i/><w:b/><w:i-cs/></w:rPr><w:t>Ospedaliera</w:t></w:r>
<w:r><w:rPr><w:i/><w:b/><w:i-cs/></w:rPr><w:t>, </w:t></w:r>
<w:r><w:rPr><w:i/><w:i-cs/></w:rPr><w:t>Ospedali</w:t></w:r>
<w:r><w:rPr><w:i/><w:i-cs/></w:rPr><w:t> </w:t></w:r>
<w:proofErr
w:type="spellStart"/>
<w:r><w:rPr><w:i/><w:i-cs/></w:rPr><w:t>Riuniti</w:t></w:r>

XSLT
<!--Text run
container-->
<xsl:template match="w:r">
  <xsl:choose>
   <xsl:when
test="w:rPr/w:vertAlign|w:rPr/w:u|w:rPr/w:b|w:rPr/w:i|w:rPr/w:smallCaps|w:rP
r/w:highlight">
    <xsl:apply-templates select="w:rPr" mode="styling"/>
 
</xsl:when>
   <xsl:otherwise>
    <xsl:apply-templates/>
   </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!--Text run properties container-->
<xsl:template match="w:rPr" mode="styling">
   <xsl:for-each
select="child::w:*[1]">
     <xsl:variable name="name">
       <xsl:choose>
 
      <xsl:when test="local-name() eq 'b'">
         <xsl:value-of
select="local-name()"/>
        </xsl:when>
        <xsl:when
test="local-name() eq 'i'">
         <xsl:value-of select="local-name()"/>
   
    </xsl:when>
      </xsl:choose>
     </xsl:variable>
     <xsl:if
test="$name!=''">
      <xsl:element name="{$name}">
       <xsl:call-template
name="nestedTemplate"/>
      </xsl:element>
     </xsl:if>
     <xsl:if
test="$name=''">
       <xsl:call-template name="nestedTemplate"/>
   
</xsl:if>
   </xsl:for-each>  
</xsl:template>

<xsl:template
name="nestedTemplate">
   <xsl:param name="inital" select="2"/>
 
<xsl:choose>
     <xsl:when test="following-sibling::w:*[$inital - 1]">
     
<xsl:variable name="name">
       <xsl:choose>
        <xsl:when
test="following-sibling::w:*[$inital - 1]/local-name() eq
'b'">
       
<xsl:value-of select="'b'"/>
        </xsl:when>
        <xsl:when
test="following-sibling::w:*[$inital - 1]/local-name() eq
'i'">
       
<xsl:value-of select="'i'"/>
        </xsl:when>
       </xsl:choose>
     
</xsl:variable>  
      <xsl:if test="$name!=''">  
       <xsl:element
name="{$name}">
        <xsl:if test="following-sibling::w:*[$inital - 1]">
 
        <xsl:call-template name="nestedTemplate">
            <xsl:with-param
name="inital" select="$inital + 1"/>
           </xsl:call-template>
       
</xsl:if>                
       </xsl:element>
      </xsl:if>
      <xsl:if
test="$name=''">  
       <xsl:if test="following-sibling::w:*[$inital - 1]">
         <xsl:call-template name="nestedTemplate">
           <xsl:with-param
name="inital" select="$inital + 1"/>
          </xsl:call-template>
       
</xsl:if>                
      </xsl:if>
     </xsl:when>
   
<xsl:otherwise>
       <xsl:apply-templates
select="parent::w:rPr/following-sibling::w:*[1][local-name()='t']"/>
   
</xsl:otherwise>
   </xsl:choose>
</xsl:template>

Current output
<i>Azienda</i><i> </i><i><b>Ospedaliera</b></i><i><b>,
</b></i><i>Ospedali</i><i> </i><i>Riuniti</i>

Desired output
<i>Azienda
<b>Ospedaliera, </b>Ospedali Riuniti</i>


Current Thread
Keywords