Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Issue creating paths from XML using XSL sheet

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
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="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL]
    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
 
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="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL]
    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="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL]
    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
 
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="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL]
    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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top