Re: [xsl] Attribute Matching

Subject: Re: [xsl] Attribute Matching
From: "Karl Stubsjoen" <kstubs@xxxxxxxxx>
Date: Sun, 7 Jan 2007 16:26:29 -0700
Beautiful!
I may be forced to convert to a node result from this:


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


Where this becomes:

<xsl:variable name="drvr">
         <xsl:apply-templates select="@*">
           <xsl:with-param name="fields" select="
               ../../fields/field"/>
         </xsl:apply-templates>
</xsl:variable>


And then to use the variable drvr in the current row node, I'll need to reference it with a node-set conversion.

I'll give this a try.  Was hoping to avoid the node-set conversion,
but I'm not unfamiliar with this approach.

Karl..


On 1/7/07, Florent Georges <darkman_spam@xxxxxxxx> wrote:
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