INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Process list of attributes in a node

Process list of attributes in a node

(OP)
I need to process a list of unknown attributes in a node.  Normally I would test if the attributes exist and process each one.

Now I have an instance where I know the node but not the attributes and I need to process them.  They would be processed in exactly the same way except for the name and value of the attribute.

For example:

With this xml file:

CODE

<RESPONSE MISMOVersionID="2.6">
  <REPORT _ID="Major" MajorFormType="Form102">
    <FORM _ID="Minor" MinorFormType="Form240">
      <IMAGE _ID="Im2"/>
    </FORM>
  </REPORT>
  <METHODS Description="THIS IS SUMMARY">
    <COMPARISON ID="241" ComparisonAmount="352,000">
      <SALE SequenceId="0" state="true" saleDate="05/15/2011">
        <LOCATION Address="9107 Forest Ct" Address2="Franklin, CT"/>
        <ADJUSTMENT Type="Concessions" _Description="N/A"/>
      </SALE>
    </COMPARISON>
  </METHODS>
</RESPONSE>

I have the following xslt file:

CODE

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/*">
    <xsl:copy>
      <xsl:apply-templates select="METHODS/COMPARISON/SALE">
        <xsl:with-param name="reportName" select="REPORT/@MajorFormType"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <!-- Now we handle the other sections after the DATA section -->

  <xsl:template match="SALE">
    <xsl:param name="reportName"/>
    <xsl:if test="@state">
      <form>
        <section>
          <xsl:value-of select="../@SequenceId"/>
        </section>
        <formName>
          <xsl:value-of select="$reportName"/>
        </formName>
        <tagName>
          <xsl:text>state</xsl:text>
        </tagName>
        <value>
          <xsl:value-of select="@state"/>
        </value>
      </form>
    </xsl:if>
    <xsl:if test="@saleDate">
      <form>
        <section>
          <xsl:value-of select="../@SequenceId"/>
        </section>
        <formName>
          <xsl:value-of select="$reportName"/>
        </formName>
        <tagName>
          <xsl:text>saleDate</xsl:text>
        </tagName>
        <value>
          <xsl:value-of select="@saleDate"/>
        </value>
      </form>
    </xsl:if>
    <xsl:apply-templates>
      <xsl:with-param name="reportName" select="$reportName"/>
    </xsl:apply-templates>
  </xsl:template>
</xsl:stylesheet>

Here I know what the attributes are and get the following results:

CODE

<?xml version="1.0" encoding="utf-8"?>
<RESPONSE>
  <form>
    <section></section>
    <formName>Form102</formName>
    <tagName>state</tagName>
    <value>true</value>
  </form>
  <form>
    <section></section>
    <formName>Form102</formName>
    <tagName>saleDate</tagName>
    <value>05/15/2011</value>
  </form>
</RESPONSE>

Is there a way to process the attributes the same way without knowing what the attribute names are such that I would have the same resultes for each attribute with only the <tagName> having the name of the attribute and <value> having the value of the attribute?

Thanks,

Tom
 

 

RE: Process list of attributes in a node

You can use the attribute axis for that.  (You have been using the child axis.)

Example

Tom Morrison
Micro Focus

RE: Process list of attributes in a node

(OP)
How would I do that here?

I tried:

CODE

  <xsl:template match="SALE/@*">
    <xsl:value-of select ="."/>
  </xsl:template>

But that didn't work.

Thanks,

Tom

RE: Process list of attributes in a node

Hi Tom,

Not knowing (1) what context that template was used in and (2) how it did not work make it a bit difficult to diagnose what might be happening.  You may not be doing anything to cause the template to match, for example.

In general, from a template matching SALE you would want to <xsl:apply-templates select="@*"/>.

The have a template that matches attributes:

CODE

<xsl:template match="@*">
  <form>
    <section>
    etc etc
    <tagName><xsl:value-of select="local-name()"/></tagName>
    <value><xsl:value-of select="."/></value>
  </form>
</xsl:template>

Still using Visual Studio 2010 for debugging?  There are better XSLT debugging tools on the market...

Tom Morrison
Micro Focus

RE: Process list of attributes in a node

(OP)
That might work for me.

I had actually tried a couple of things and found one that worked - not sure which is better:

CODE

  <xsl:template match="SALE">
    <xsl:param name="reportName"/>
    <xsl:for-each select="@*">
        <form>
            <section>
                <xsl:value-of select="../@SequenceId"/>
            </section>        
            <formName>
                <xsl:value-of select="$reportName"/>
            </formName>
            <tagName>
                <xsl:value-of select="name()"/>
            </tagName>
            <value>
                <xsl:value-of select="."/>
            </value>
        </form>
    </xsl:for-each>
    <xsl:if test="@state">

Yes, I am still using VS 2010 for debugging as it works for what I need.  I was looking for a better one years ago and they all seemed to be pretty expensive.

Tom

RE: Process list of attributes in a node

This is fine.  Iterating in a for-each is not approved by some purists, who prefer the template match when it works, but I think a for-each does fine here.

Tom Morrison
Micro Focus

RE: Process list of attributes in a node

(OP)
I didn't know how to use a match for attributes here so that was why I used it.

Thanks,

Tom

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close