0

i'm using saxon: i have xhtml,practically is a table with a token like this:

    <?xml version="1.0" encoding="windows-1252"?>

<!DOCTYPE html
   PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title></title>
    <meta name="GENERATOR" content="OpenOffice.org 3.3  (Win32)" />
    <meta name="CREATED" content="0;0" />
    <meta name="CHANGED" content="20150212;12040469" />
    <style type="text/css" xml:space="preserve">

        BODY,DIV,TABLE,THEAD,TBODY,TFOOT,TR,TH,TD,P { font-family:"Arial"; font-size:x-small }

    </style>
  </head>
  <body text="#000000">
    <table frame="void" cellspacing="0" rules="none" border="0">
      <colgroup>
        <col width="78" />
        <col width="379" />
        <col width="370" />
        <col width="165" />
        <col width="165" />
        <col width="120" />
        <col width="135" />
        <col width="135" />
      </colgroup><tbody>
        <tr>
          <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" width="78" height="82" align="center">
            <b>CODE </b>
          </td>
          <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" width="379" align="center">
            <b>DISPLAY NAME </b>
          </td>
          <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" width="370" align="center">
            <b><font color="#000000">
            2013 </font></b>
          </td>
          <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" width="165" align="center">
            <b>TYPE</b>
          </td>
          <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" width="165" align="center">
            <b>2013 FORECAST</b>
          </td>
          <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" width="120" align="center">
            <b>2014 FORECAST<br /></b>
          </td>
          <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" width="135" align="center">
            <b>2015 FORECAST</b>
          </td>
          <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" width="135" align="center">
            <b>2016 FORECAST</b>
          </td>
        </tr>
        <tr>
          <td style="border-top: 1px solid #000000; border-left: 1px solid #000000" height="17" align="left">
            <br />
          </td>
          <td style="border-top: 1px solid #000000" align="left">
            <br />
          </td>
          <td style="border-top: 1px solid #000000" align="left">
            <br />
          </td>
          <td style="border-top: 1px solid #000000" align="left">
            <br />
          </td>
          <td style="border-top: 1px solid #000000" align="left">
            <br />
          </td>
          <td style="border-top: 1px solid #000000" align="left">
            <br />
          </td>
          <td style="border-top: 1px solid #000000" align="left">
          </td>
          <td style="border-top: 1px solid #000000; border-right: 1px solid #000000" align="left">
            <br />
          </td>
        </tr>
        <tr>
          <td style="border-left: 1px solid #000000" height="17" align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td style="border-right: 1px solid #000000" align="left">
            <br />
          </td>
        </tr>
        <tr>
          <td style="border-left: 1px solid #000000" height="17" align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td style="border-right: 1px solid #000000" align="left">
            <br />
          </td>
        </tr>
        <tr>
          <td style="border-left: 1px solid #000000" height="17" align="left">
            2 001
          </td>
          <td align="left">
            SOME TEXT TO DISPLAY 1</td>
          <td align="left">

            <br />
          </td>
          <td align="left">
            some type 1</td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td style="border-right: 1px solid #000000" align="left">
            <br />
          </td>
        </tr>
        <tr>
          <td style="border-left: 1px solid #000000" height="17" align="left">
            <br />
          </td>
          <td align="left">
            SOME TEXT TO DISPLAY 2</td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td style="border-right: 1px solid #000000" align="left">
            <br />
          </td>
        </tr>
        <tr>
          <td style="border-left: 1px solid #000000" height="17" align="left">
            2 002
          </td>
          <td align="left">
            SOME TEXT TO DISPLAY 3</td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            some type 3</td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td style="border-right: 1px solid #000000" align="left">
            <br />
          </td>
        </tr>
      </tbody>
    </table>
    <!-- 
      ************************************************************************** -->
  </body>
</html>

In this xhtml i have a code in a td element like : 2 001, 2 002.

Now I have an xml file that allows you to map data like this:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<certificati>
    <certificato anno="2014" codiceEnte="#CODICE_ENTE#"
        tipoCertificato="P" tipoValuta="E">

        <elemento>
            <colonne quadro="02" voce="001">
                <colonna num="1">123</colonna>
                <colonna num="2">456</colonna>
                <colonna num="3">789</colonna>
                <colonna num="4">10112</colonna>
                <colonna num="5">1312</colonna>
            </colonne>
            <colonne quadro="02" voce="002">
                <colonna num="1">2123</colonna>
                <colonna num="2">3456</colonna>
                <colonna num="3">567</colonna>
                <colonna num="4">876</colonna>
                <colonna num="5">8765</colonna>
            </colonne>

        </elemento> 
    </certificato>
</certificati>

The out file must be an xhtml with value matched in the value xml file: for matching value u have to use this key for example : <colonne quadro="02" voce="001"> for 2 001 in the TD element in xhtml and fill the other value.

The out put file must be:

    <?xml version="1.0" encoding="windows-1252"?>

<!DOCTYPE html
   PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title></title>
    <meta name="GENERATOR" content="OpenOffice.org 3.3  (Win32)" />
    <meta name="CREATED" content="0;0" />
    <meta name="CHANGED" content="20150212;12040469" />
    <style type="text/css" xml:space="preserve">

        BODY,DIV,TABLE,THEAD,TBODY,TFOOT,TR,TH,TD,P { font-family:"Arial"; font-size:x-small }

    </style>
  </head>
  <body text="#000000">
    <table frame="void" cellspacing="0" rules="none" border="0">
      <colgroup>
        <col width="78" />
        <col width="379" />
        <col width="370" />
        <col width="165" />
        <col width="165" />
        <col width="120" />
        <col width="135" />
        <col width="135" />
      </colgroup><tbody>
        <tr>
          <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" width="78" height="82" align="center">
            <b>CODE </b>
          </td>
          <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" width="379" align="center">
            <b>DISPLAY NAME </b>
          </td>
          <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" width="370" align="center">
            <b><font color="#000000">
            2013 </font></b>
          </td>
          <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" width="165" align="center">
            <b>TYPE</b>
          </td>
          <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" width="165" align="center">
            <b>2013 FORECAST</b>
          </td>
          <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" width="120" align="center">
            <b>2014 FORECAST<br /></b>
          </td>
          <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" width="135" align="center">
            <b>2015 FORECAST</b>
          </td>
          <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" width="135" align="center">
            <b>2016 FORECAST</b>
          </td>
        </tr>
        <tr>
          <td style="border-top: 1px solid #000000; border-left: 1px solid #000000" height="17" align="left">
            <br />
          </td>
          <td style="border-top: 1px solid #000000" align="left">
            <br />
          </td>
          <td style="border-top: 1px solid #000000" align="left">
            <br />
          </td>
          <td style="border-top: 1px solid #000000" align="left">
            <br />
          </td>
          <td style="border-top: 1px solid #000000" align="left">
            <br />
          </td>
          <td style="border-top: 1px solid #000000" align="left">
            <br />
          </td>
          <td style="border-top: 1px solid #000000" align="left">
          </td>
          <td style="border-top: 1px solid #000000; border-right: 1px solid #000000" align="left">
            <br />
          </td>
        </tr>
        <tr>
          <td style="border-left: 1px solid #000000" height="17" align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td style="border-right: 1px solid #000000" align="left">
            <br />
          </td>
        </tr>
        <tr>
          <td style="border-left: 1px solid #000000" height="17" align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td style="border-right: 1px solid #000000" align="left">
            <br />
          </td>
        </tr>
        <tr>
          <td style="border-left: 1px solid #000000" height="17" align="left">2 001</td>
          <td align="left">
            SOME TEXT TO DISPLAY 1</td>
          <td align="left">

            <br />
            123
          </td>
          <td align="left">
            some type 1</td>
          <td align="left">
            <br />
            456
          </td>
          <td align="left">
            <br />
            789
          </td>
          <td align="left">
            <br />
            10112
          </td>
          <td style="border-right: 1px solid #000000" align="left">

            <br />
            1312
          </td>
        </tr>
        <tr>
          <td style="border-left: 1px solid #000000" height="17" align="left">
            <br />
          </td>
          <td align="left">
            SOME TEXT TO DISPLAY 2</td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td align="left">
            <br />
          </td>
          <td style="border-right: 1px solid #000000" align="left">
            <br />
          </td>
        </tr>
        <tr>
          <td style="border-left: 1px solid #000000" height="17" align="left">
            2 002
          </td>
          <td align="left">
            SOME TEXT TO DISPLAY 3</td>
          <td align="left">
            <br />
            2123
          </td>
          <td align="left">
            some type 3</td>
          <td align="left">
            <br />
            3456
          </td>
          <td align="left">
            <br />
            567
          </td>
          <td align="left">
            <br />
            876
          </td>
          <td style="border-right: 1px solid #000000" align="left">
            <br />
            8765
          </td>
        </tr>
      </tbody>
    </table>
    <!-- 
      ************************************************************************** -->
  </body>
</html>

The question is: how to do this output with a single stylesheets?

2
  • 2
    Could you please compress your examples to the actual problem? Would be easier to understand. Commented Feb 13, 2015 at 17:14
  • Minimizing the examples would be very helpful. You also need to explain (1) which elements in the source document need to have their values replaced by the lookup; and (2) how do you know which of the 5 matching values to apply. -- In general, the answer to your question is to use a key: w3.org/TR/xslt20/#key Commented Feb 13, 2015 at 17:20

1 Answer 1

0

It is a bit tricky with one document having elements in the XHTML namespace and the other document having elements in no namespace. Furthermore the number format in the XHTML document is different from the one in the other document. To cover that I had to use a string(xs:integer(...)) conversion.

Here is my suggestion:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="xs xhtml"
  xpath-default-namespace="http://www.w3.org/1999/xhtml">

<xsl:param name="doc2-url" as="xs:string" select="'test2015021302.xml'"/>
<xsl:param name="doc2" as="document-node()" select="doc($doc2-url)"/>

<xsl:key name="col"
         xpath-default-namespace=""
         match="elemento/colonne"
         use="string-join(for $att in (@quadro, @voce) return string(xs:integer($att)), '|')"/>

<xsl:output
  method="xhtml"
  doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
  doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="tr[key('col', string-join(for $s in tokenize(td[1], '\s+')[normalize-space()] return string(xs:integer($s)), '|'), $doc2)]/td[not(text()[normalize-space()])]">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
    <xsl:copy-of xpath-default-namespace="" select="key('col', string-join(for $s in tokenize(../xhtml:td[1], '\s+')[normalize-space()] return string(xs:integer($s)), '|'), $doc2)/colonna[position() = count(current()/preceding-sibling::xhtml:td[not(text()[normalize-space()])]) + 1]/node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

1 Comment

Sounds good, work perfectly for me, i have made some change because the data in the xhtml table have a fixed fields. Thanks!!! <xsl:template match="tr[key('col', string-join(for $s in tokenize(td[1], '\s+')[normalize-space()] return string(xs:integer($s)), '|'), $doc2)]/td[3]"> <xsl:copy> <xsl:apply-templates select="@* | node()" /> <xsl:copy-of xpath-default-namespace="" select="key('col', string-join(for $s in tokenize(../xhtml:td[1], '\s+')[normalize-space()] return string(xs:integer($s)), '|'), $doc2)/colonna[@num=1]" /> </xsl:copy> </xsl:template>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.