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

Re: [xsl] accessing attributes


Subject: Re: [xsl] accessing attributes
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 09 Sep 2008 15:08:19 -0400

Tom,

As it happens, I just helped someone solve this exact problem last week. So you are not alone.

The simple answer to your question:
<xsl:value-of select="fmp:METADATA/fmp:FIELD[3]@NAME"/>

<xsl:value-of select="/*/fmp:METADATA/fmp:FIELD[3]/@NAME"/>


would write the value of the third fmp:FIELD element of the fmp:METADATA element inside the FMPXMLRESULT document element. Note:

* An extra step in front of the path, so it traverses from the root of the document, not the processing context of the XPath, and finds the fmp:METADATA element as a child of the document element (which it is)
* A slash before @NAME, as that has to be a discrete step in the XPath.
* Also, in the sample data you have, of course this won't get anything, as there are only two FIELD elements and you are asking for fmp:FIELD[3].


I hope that gets you moving.

About the general problem, experience shows that:

* The necessary operations are much easier in XSLT 2.0 than in XSLT 1.0, since XPath 2.0 offers a function, index-of(), which is lacking in XSLT 1.0 but which provides a bit of necessary information that is cumbersome to calculate without it, and
* If you are stuck using XSLT 1.0, you will probably want to optimize your logic using variables and possibly a key. Otherwise you will have almost illegible code and probably n^2 performance or worse (depending on whether your processor optimizes internally for you).


Good luck,
Wendell

At 02:26 PM 9/9/2008, you wrote:
Hi everyone,
I'm new to this so please forgive any missing information etc.
I'm trying to write an xslt style sheet to transform the xml output from a
filemaker database and I'm running into a few issues.
My processor allows XSL v1.0 with library libxslt (running using PHP so
looks like this is all I can get).

The filemaker output gives the field names in a metadata node, with the
data coming later:

<?xml version="1.0" encoding="UTF-8" ?>
<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
<METADATA>
        <FIELD NAME="F1"/>
        <FIELD NAME="F2"/>
</METADATA>
<DATA>
        <ROW>
          <COL><DATA>V1</DATA></COL>
          <COL><DATA>V2</DATA></COL>
        </ROW>
        <ROW>
          <COL><DATA>V3</DATA></COL>
          <COL><DATA>V4</DATA></COL>
        </ROW>
</DATA>



Basically I want to be able to relate each column value to it's name in
the METADATA. However I'm not that far yet. To start with can someone
please tell me how I should be writing this xsl:value-of element?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:fmp="http://www.filemaker.com/fmpxmlresult"
                              exclude-result-prefixes="fmp">

<xsl:template match="fmp:FMPXMLRESULT">
<xsl:value-of select="fmp:METADATA/fmp:FIELD[3]@NAME"/>
</xsl:template>
</xsl:stylesheet>


Many thanks tom


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


Current Thread
Keywords