Re: [xsl] Attribute Matching

Subject: Re: [xsl] Attribute Matching
From: Florent Georges <darkman_spam@xxxxxxxx>
Date: Sun, 7 Jan 2007 23:59:48 +0100 (CET)
Karl Stubsjoen wrote:

  Hi

> In a single source of XML I have a FIELDS/FIELD lookup, where there
> are many FIELD elements which may or may not have a flg attribute
> who's value maps to an attribute of the same name in a row element.
> The value of this attribute determines whether or not to include the
> FIELD element in the result.

> So, I'll expand a little with my XML:

> <root>
>   <fields>
>     <field name="Name" flg="flgName"/>
>     <field name="HomeTown" flg="flgHomeTown"/>
>     <field name="Country" flg="flgCountry"/>
>   </fields>
>   <result>
>     <row flgHomeTown="1" flgCountry="0" flgName="0"/>
>     <row flgHomeTown="1" flgCountry="0" flgName="1"/>
>   </result>
> </root>

  So you are using XSLT 1.0?  The following is what your are
looking for, if I correctly understood:

    (drkm)[31] ~/xslt/tests$ cat karl.xsl
    <xsl:stylesheet
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
        version="1.0">

      <xsl:output indent="yes"/>

      <xsl:template match="/">
        <res>
          <xsl:apply-templates select="root/result/row"/>
        </res>
      </xsl:template>

      <xsl:template match="row">
        <line>
          <xsl:apply-templates select="@*">
            <xsl:with-param name="fields" select="
                ../../fields/field"/>
          </xsl:apply-templates>
        </line>
      </xsl:template>

      <xsl:template match="row/@*[. = '0']">
        <xsl:param name="fields"/>
      </xsl:template>

      <xsl:template match="row/@*[. = '1']">
        <xsl:param name="fields"/>
        <xsl:variable name="name" select="name()"/>
        <xsl:copy-of select="$fields[@flg = $name]"/>
      </xsl:template>

    </xsl:stylesheet>

    (drkm)[32] ~/xslt/tests$ cat karl.xml
    <root>
      <fields>
        <field name="Name" flg="flgName"/>
        <field name="HomeTown" flg="flgHomeTown"/>
        <field name="Country" flg="flgCountry"/>
      </fields>
      <result>
        <row flgHomeTown="1" flgCountry="0" flgName="0"/>
        <row flgHomeTown="1" flgCountry="0" flgName="1"/>
      </result>
    </root>

    (drkm)[33] ~/xslt/tests$ saxon karl.xml karl.xsl
    Warning: at xsl:stylesheet on line 3 of karl.xsl:
      Running an XSLT 1.0 stylesheet with an XSLT 2.0 processor
    <?xml version="1.0" encoding="UTF-8"?>
    <res>
       <line>
          <field name="HomeTown" flg="flgHomeTown"/>
       </line>
       <line>
          <field name="HomeTown" flg="flgHomeTown"/>
          <field name="Name" flg="flgName"/>
       </line>
    </res>

    (drkm)[34] ~/xslt/tests$ xsltproc karl.xsl karl.xml
    <?xml version="1.0"?>
    <res>
      <line>
        <field name="HomeTown" flg="flgHomeTown"/>
      </line>
      <line>
        <field name="HomeTown" flg="flgHomeTown"/>
        <field name="Name" flg="flgName"/>
      </line>
    </res>

    (drkm)[35] ~/xslt/tests$

  Regards,

--drkm






















__________________________________________________
Do You Yahoo!?
En finir avec le spam? Yahoo! Mail vous offre la meilleure protection possible contre les messages non sollicitis 
http://mail.yahoo.fr Yahoo! Mail 

Current Thread