[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:10:02 -0700 (PDT)

Joga,
Try this - it much more fun with groups and functions and it does work
for the sample provided:
Terry<?xml version="1.0" encoding="UTF-8"?>



From:
Joga Singh Rawat <jrawat@xxxxxxxxxxxxxx>
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
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>
<xsl:stylesheetxmlns: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="somethi
ng-else">
<!-- function turns the existance or not of a w:b into something the
group-adjacent can use -->
<xsl:strip-spaceelements="*"/>
<xsl:functionname="tb:switch-b">
<xsl:paramname="r"/>
<xsl:choose>
<xsl:whentest="boolean($r/w:rPr/w:b) = true()">1</xsl:when>
<xsl:whentest="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:templatematch="/w:p">
<xsl:result-documenthref="wordml-01-output.xml">
<!-- with the given sample
file there will be only one group -->
<xsl:for-each-groupselect="w:r"group-adjacent="w:rPr/w:i">
<xsl:elementname="i">
<!-- group by bold and not bold -->
<xsl:for-each-groupselect="current-group()/self::w:r"group-adjacent="tb:switc
h-b(.)">
<xsl:choose>
<xsl:whentest="current-group()//w:b">
<xsl:elementname="b">
<xsl:value-ofselect="current-group()//w:t"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:value-ofselect="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>


Current Thread
Keywords