Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

Join Tek-Tips
*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 from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Issue creating paths from XML using XSL sheet

tshad (Programmer) (OP)
2 Nov 11 13:29
I am trying to go through some xml files and create paths and attributes from them.

I have the following XSL that almost works:

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="node()"/>
        </xsl:copy>
    </xsl:template>

  <xsl:template match="node()">
    <path>
      <xsl:for-each select="ancestor-or-self::*">
        <xsl:text>/</xsl:text>
        <xsl:value-of select="name()" />
      </xsl:for-each>
    </path>
    <xsl:apply-templates>
    </xsl:apply-templates>
  </xsl:template>

</xsl:stylesheet>

using the xml file:

CODE

<RESPONSE MISMOVersionID="2.6">
  <REPORT _ID="Major" MajorFormType="Form102">
    <FORM _ID="Minor" MinorFormType="Form240">
      <IMAGE _ID="Im2"/>
      <IMAGE _ID="Im5"/>
    </FORM>
  </REPORT>
  <METHODS Description="THIS IS SUMMARY">
    <COMPARISON ID="241" ComparisonAmount="352,000">
      <LOCATION ID="HOME"/>
      <LOCATION ID="WORK"/>
    </COMPARISON>
  </METHODS>
</RESPONSE>

And the results are:

CODE

<?xml version="1.0" encoding="utf-8"?>
<path>/RESPONSE</path>
<path>/RESPONSE</path>
<path>/RESPONSE/REPORT</path>
<path>/RESPONSE/REPORT</path>
<path>/RESPONSE/REPORT/FORM</path>
<path>/RESPONSE/REPORT/FORM</path>
<path>/RESPONSE/REPORT/FORM/IMAGE</path>
<path>/RESPONSE/REPORT/FORM</path>
<path>/RESPONSE/REPORT/FORM/IMAGE</path>
<path>/RESPONSE/REPORT/FORM</path>
<path>/RESPONSE/REPORT</path>
<path>/RESPONSE</path>
<path>/RESPONSE/METHODS</path>
<path>/RESPONSE/METHODS</path>
<path>/RESPONSE/METHODS/COMPARISON</path>
<path>/RESPONSE/METHODS/COMPARISON</path>
<path>/RESPONSE/METHODS/COMPARISON/LOCATION</path>
<path>/RESPONSE/METHODS/COMPARISON</path>
<path>/RESPONSE/METHODS/COMPARISON/LOCATION</path>
<path>/RESPONSE/METHODS/COMPARISON</path>
<path>/RESPONSE/METHODS</path>
<path>/RESPONSE</path>

It seems I am getting the closing nodes as well.

Is there a way to change this so I get something like:

CODE

<?xml version="1.0" encoding="utf-8"?>
<path>/RESPONSE</path>
<path>/RESPONSE/REPORT</path>
<path>/RESPONSE/REPORT/FORM</path>
<path>/RESPONSE/REPORT/FORM/IMAGE</path>
<path>/RESPONSE/REPORT/FORM/IMAGE</path>
<path>/RESPONSE/METHODS</path>
<path>/RESPONSE/METHODS/COMPARISON</path>
<path>/RESPONSE/METHODS/COMPARISON/LOCATION</path>
<path>/RESPONSE/METHODS/COMPARISON/LOCATION</path>

I am not trying to create an XML file but am trying to create a list of paths from an xml file that I want to put in an excel sheet.

I will also be making changes to show the attributes as well.

But I need to not show the path for the closing nodes.

Thanks,

Tom
 
tshad (Programmer) (OP)
2 Nov 11 16:45
I tried to resolve this by testing for blank nodes (which I thought may be the closing nodes).

I tried:

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="node()">
        <xsl:copy>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>

  <xsl:template match="node()">
    <xsl:if test="node() != '">
      <path>
        <xsl:for-each select="ancestor-or-self::*">
          <xsl:text>/</xsl:text>
          <xsl:value-of select="name()" />
        </xsl:for-each>
      </path>
    </xsl:if>
    <xsl:apply-templates>
    </xsl:apply-templates>
  </xsl:template>

</xsl:stylesheet>

And I got the following:

CODE

<?xml version="1.0" encoding="utf-8"?>
<path>/RESPONSE</path>
<path>/RESPONSE/REPORT</path>
<path>/RESPONSE/REPORT/FORM</path>
<path>/RESPONSE/METHODS</path>
<path>/RESPONSE/METHODS/COMPARISON</path>

But where is the IMAGE and LOCATION nodes?

I tried to print out the node name to see what it was:

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="node()">
        <xsl:copy>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>

  <xsl:template match="node()">
    <node>
      <xsl:value-of select="node()"/>
    </node>
    <xsl:if test="node() != '">
      <path>
        <xsl:for-each select="ancestor-or-self::*">
          <xsl:text>/</xsl:text>
          <xsl:value-of select="name()" />
        </xsl:for-each>
      </path>
    </xsl:if>
    <xsl:apply-templates>
    </xsl:apply-templates>
  </xsl:template>

</xsl:stylesheet>

Here I got the following:

CODE

<?xml version="1.0" encoding="utf-8"?>
<node>
  </node>
<path>/RESPONSE</path>
<node></node>
<node>
    </node>
<path>/RESPONSE/REPORT</path>
<node></node>
<node>
      </node>
<path>/RESPONSE/REPORT/FORM</path>
<node></node>
<node></node>
<node></node>
<node></node>
<node></node>
<node></node>
<node></node>
<node>
    </node>
<path>/RESPONSE/METHODS</path>
<node></node>
<node>
      </node>
<path>/RESPONSE/METHODS/COMPARISON</path>
<node></node>
<node></node>
<node></node>
<node></node>
<node></node>
<node></node>
<node></node>

There is never a name for the nodes???

Why is that?

I assume that the missing nodes has something to do with the fact that there is no closing node.

Thanks,

Tom
 
tshad (Programmer) (OP)
2 Nov 11 16:50
Oops, my mistake on the last post.  I had used node() instead of name() for my test and display.

I changed the code to:

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="node()">
        <xsl:copy>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>

  <xsl:template match="node()">
    <xsl:if test="name() != '">
      <path>
        <xsl:for-each select="ancestor-or-self::*">
          <xsl:text>/</xsl:text>
          <xsl:value-of select="name()" />
        </xsl:for-each>
      </path>
    </xsl:if>
    <xsl:apply-templates>
    </xsl:apply-templates>
  </xsl:template>

</xsl:stylesheet>

And I got the following:

CODE

<?xml version="1.0" encoding="utf-8"?>
<path>/RESPONSE</path>
<path>/RESPONSE/REPORT</path>
<path>/RESPONSE/REPORT/FORM</path>
<path>/RESPONSE/REPORT/FORM/IMAGE</path>
<path>/RESPONSE/REPORT/FORM/IMAGE</path>
<path>/RESPONSE/METHODS</path>
<path>/RESPONSE/METHODS/COMPARISON</path>
<path>/RESPONSE/METHODS/COMPARISON/LOCATION</path>
<path>/RESPONSE/METHODS/COMPARISON/LOCATION</path>

Thanks,

Tom

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!

Back To Forum

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