Relative XPath position Opties

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thomasv
    New Member
    • Feb 2009
    • 1

    #1

    Relative XPath position Opties

    Hi,

    I want to convert the following XML (OpenOffice Calc file):


    Code:
    <?xml version="1.0" encoding="UTF-8"?> 
    <office:document-content 
    xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
    xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" 
    xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" 
    xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" 
    xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" 
    xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" 
    xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/ 
    dc/elements/1.1/" 
    xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" 
    xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" 
    xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation: 
    1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible: 
    1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" 
    xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" 
    xmlns:math="http://www.w3.org/1998/Math/MathML" 
    xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" 
    xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" 
    xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http:// 
    openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/ 
    calc" xmlns:dom="http://www.w3.org/2001/xml-events" 
    xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http:// 
    www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema- 
    instance" xmlns:rpt="http://openoffice.org/2005/report" 
    xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" 
    xmlns:rdfa="http://docs.oasis-open.org/opendocument/meta/rdfa#" 
    office:version="1.2"> 
            <office:body> 
                    <office:spreadsheet> 
                            <table:table table:name="Sheet1" table:style-name="ta1" 
    table:print="false"> 
                                    <table:table-column table:style-name="co1" table:number-columns- 
    repeated="2" table:default-cell-style-name="Default"/> 
                                    <table:table-column table:style-name="co2" table:default-cell- 
    style-name="Default"/> 
                                    <table:table-row table:style-name="ro1"> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>EN-US</text:p> 
                                            </table:table-cell> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>NL-NL</text:p> 
                                            </table:table-cell> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>ES-ES</text:p> 
                                            </table:table-cell> 
                                    </table:table-row> 
                                    <table:table-row table:style-name="ro2"> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>Dog</text:p> 
                                            </table:table-cell> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>Hond</text:p> 
                                            </table:table-cell> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>Perro</text:p> 
                                            </table:table-cell> 
                                    </table:table-row> 
                                    <table:table-row table:style-name="ro2"> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>Cat</text:p> 
                                            </table:table-cell> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>Kat</text:p> 
                                            </table:table-cell> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>Gato</text:p> 
                                            </table:table-cell> 
                                    </table:table-row> 
                                    <table:table-row table:style-name="ro2"> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>House</text:p> 
                                            </table:table-cell> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>Huis</text:p> 
                                            </table:table-cell> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>Casa</text:p> 
                                            </table:table-cell> 
                                    </table:table-row> 
                                    <table:table-row table:style-name="ro2"> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>Tree</text:p> 
                                            </table:table-cell> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>Boom</text:p> 
                                            </table:table-cell> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>Árbol</text:p> 
                                            </table:table-cell> 
                                    </table:table-row> 
                                    <table:table-row table:style-name="ro2"> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>Cat &amp; Dog</text:p> 
                                            </table:table-cell> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>Kat &amp; Hond</text:p> 
                                            </table:table-cell> 
                                            <table:table-cell office:value-type="string"> 
                                                    <text:p>Gato &amp; Perro</text:p> 
                                            </table:table-cell> 
                                    </table:table-row> 
                            </table:table> 
                    </office:spreadsheet> 
            </office:body> 
    </office:document-content>

    Using this style sheet:


    Code:
    <?xml version="1.0" encoding="UTF-8"?> 
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ 
    Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xlink="http://www.w3.org/1999/xlink" 
    xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
    xmlns:ooo="http://openoffice.org/2004/office" 
    xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" 
    xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"> 
            <xsl:output indent="yes"/> 
            <xsl:template match="/"> 
                    <martif type="TBX"> 
                            <martifHeader> 
                                    <fileDesc> 
                                            <sourceDesc> 
                                                    <p>This TBX file was converted from an OpenOffice Calc file</p> 
                                            </sourceDesc> 
                                    </fileDesc> 
                            </martifHeader> 
                            <text> 
                                    <body> 
                                            <xsl:for-each select="//table:table-row[position()>1]"> 
                                                    <termEntry> 
                                                            <xsl:for-each select="table:table-cell"> 
                                                                    <langSet xml:lang="{//table:table-row[@table:style-name='ro1']/ 
    table:table-cell[count(ancestor::table:table-cell/preceding-sibling::*) 
    +1]/text:p}"> 
                                                                            <tig> 
                                                                                    <term> 
                                                                                            <xsl:value-of select="text:p"/> 
                                                                                    </term> 
                                                                            </tig> 
                                                                    </langSet> 
                                                            </xsl:for-each> 
                                                    </termEntry> 
                                            </xsl:for-each> 
                                    </body> 
                            </text> 
                    </martif> 
            </xsl:template> 
    </xsl:stylesheet>

    The desired output is (TermBase eXchange):


    Code:
    <?xml version="1.0" encoding="UTF-8"?> 
    <martif xmlns:fo="http://www.w3.org/1999/XSL/Format" 
    xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
    xmlns:ooo="http://openoffice.org/2004/office" 
    xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" 
    xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" 
    xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http:// 
    www.w3.org/2001/XMLSchema-instance" type="TBX"> 
            <martifHeader> 
                    <fileDesc> 
                            <sourceDesc> 
                                    <p>This TBX file was converted from an OpenOffice Calc file</p> 
                            </sourceDesc> 
                    </fileDesc> 
            </martifHeader> 
            <text> 
                    <body> 
                            <termEntry> 
                                    <langSet xml:lang="EN-US"> 
                                            <tig> 
                                                    <term>Dog</term> 
                                            </tig> 
                                    </langSet> 
                                    <langSet xml:lang="NL-NL"> 
                                            <tig> 
                                                    <term>Hond</term> 
                                            </tig> 
                                    </langSet> 
                                    <langSet xml:lang="ES-ES"> 
                                            <tig> 
                                                    <term>Perro</term> 
                                            </tig> 
                                    </langSet> 
                            </termEntry> 
                            <termEntry> 
                                    <langSet xml:lang="EN-US"> 
                                            <tig> 
                                                    <term>Cat</term> 
                                            </tig> 
                                    </langSet> 
                                    <langSet xml:lang="NL-NL"> 
                                            <tig> 
                                                    <term>Kat</term> 
                                            </tig> 
                                    </langSet> 
                                    <langSet xml:lang="ES-ES"> 
                                            <tig> 
                                                    <term>Gato</term> 
                                            </tig> 
                                    </langSet> 
                            </termEntry> 
                            <termEntry> 
                                    <langSet xml:lang="EN-US"> 
                                            <tig> 
                                                    <term>House</term> 
                                            </tig> 
                                    </langSet> 
                                    <langSet xml:lang="NL-NL"> 
                                            <tig> 
                                                    <term>Huis</term> 
                                            </tig> 
                                    </langSet> 
                                    <langSet xml:lang="ES-ES"> 
                                            <tig> 
                                                    <term>Casa</term> 
                                            </tig> 
                                    </langSet> 
                            </termEntry> 
                            <termEntry> 
                                    <langSet xml:lang="EN-US"> 
                                            <tig> 
                                                    <term>Tree</term> 
                                            </tig> 
                                    </langSet> 
                                    <langSet xml:lang="NL-NL"> 
                                            <tig> 
                                                    <term>Boom</term> 
                                            </tig> 
                                    </langSet> 
                                    <langSet xml:lang="ES-ES"> 
                                            <tig> 
                                                    <term>Árbol</term> 
                                            </tig> 
                                    </langSet> 
                            </termEntry> 
                            <termEntry> 
                                    <langSet xml:lang="EN-US"> 
                                            <tig> 
                                                    <term>Cat &amp; Dog</term> 
                                            </tig> 
                                    </langSet> 
                                    <langSet xml:lang="NL-NL"> 
                                            <tig> 
                                                    <term>Kat &amp; Hond</term> 
                                            </tig> 
                                    </langSet> 
                                    <langSet xml:lang="ES-ES"> 
                                            <tig> 
                                                    <term>Gato &amp; Perro</term> 
                                            </tig> 
                                    </langSet> 
                            </termEntry> 
                    </body> 
            </text> 
    </martif>

    I'm stuck with the Xpath expression to get the desired output for the
    'xml:lang' attribute. It has to take on the value of the corresponding
    (i.e. same position) 'table:table-cell' element of row 1 (i.e.
    <table:table-row table:style-name="ro1">). Now it always takes the
    first value (i.e. EN-US).


    Thanks in advance.
    Thomas
    Last edited by Dormilich; Feb 18 '09, 12:32 PM. Reason: added [code] tags
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Code:
    <xsl:variable name="pos" select="position()"/>
    ...
    xml:lang="{//table:table-row[@table:style-name='ro1']/  
    table:table-cell[position() = $pos]}"
    You were probably missing a current() somewhere.

    Comment

    Working...