Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3940288
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:24:02+00:00 2026-05-20T00:24:02+00:00

On another thread about using XSLT to list every node in an XML file,

  • 0

On another thread about using XSLT to list every node in an XML file, Alejandro offered this useful piece of XPath 2.0 code:

EDIT: my stylesheet below now uses the modified version of the code that Alejandro kindly posted in a comment. It reports the @name attribute of elements.

I have heavily modified it to apply to a .xsd schema like the following:

Example Schema

(a simplified version of this source)

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:annotation>
    <xsd:documentation xml:lang="en">
 Purchase order schema for Example.com.
 Copyright 2000 Example.com. All rights reserved.
</xsd:documentation>
</xsd:annotation>
<xsd:element name="comment" type="xsd:string">
    <xsd:annotation>
        <xsd:documentation>doc for comment</xsd:documentation>
    </xsd:annotation>
</xsd:element>
<xsd:complexType name="USAddress">
    <xsd:annotation>
        <xsd:documentation>doc for USAddress</xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
        <xsd:element name="name" type="xsd:string"/>
        <xsd:element name="street" type="xsd:string"/>
        <xsd:element name="city" type="xsd:string"/>
        <xsd:element name="state" type="xsd:string"/>
        <xsd:element name="zip" type="xsd:decimal"/>
    </xsd:sequence>
    <xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>
</xsd:complexType>
<!-- Stock Keeping Unit, a code for identifying products -->
<xsd:simpleType name="SKU">
    <xsd:annotation>
        <xsd:documentation>doc for SKU</xsd:documentation>
    </xsd:annotation>
    <xsd:restriction base="xsd:string">
        <xsd:pattern value="\d{3}-[A-Z]{2}"/>
    </xsd:restriction>
</xsd:simpleType>
</xsd:schema>

My XSLT Stylesheet:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsl:template match="*|@*">
<xsl:value-of select="
string-join(
distinct-values(
(//*|//@*)
/string-join(
(ancestor::node()/name(),
if (self::attribute())
    then concat('@',name())
    else if (self::*[@name]) 
        then concat(name(),'[@name=&quot;',@name,'&quot;]')
        else name()),
'/')),
'&#xA;')
"/>
    </xsl:template>
    </xsl:stylesheet>

Results:

/xsd:schema
/xsd:schema/xsd:annotation
/xsd:schema/xsd:annotation/xsd:documentation
/xsd:schema/xsd:annotation/xsd:documentation/@xml:lang
/xsd:schema/xsd:element[@name="comment"]
/xsd:schema/xsd:element/@name
/xsd:schema/xsd:element/@type
/xsd:schema/xsd:element/xsd:annotation
/xsd:schema/xsd:element/xsd:annotation/xsd:documentation
/xsd:schema/xsd:complexType[@name="USAddress"]
/xsd:schema/xsd:complexType/@name
/xsd:schema/xsd:complexType/xsd:annotation
/xsd:schema/xsd:complexType/xsd:annotation/xsd:documentation
/xsd:schema/xsd:complexType/xsd:sequence
/xsd:schema/xsd:complexType/xsd:sequence/xsd:element[@name="name"]
/xsd:schema/xsd:complexType/xsd:sequence/xsd:element/@name
/xsd:schema/xsd:complexType/xsd:sequence/xsd:element/@type
/xsd:schema/xsd:complexType/xsd:sequence/xsd:element[@name="street"]
/xsd:schema/xsd:complexType/xsd:sequence/xsd:element[@name="city"]
/xsd:schema/xsd:complexType/xsd:sequence/xsd:element[@name="state"]
/xsd:schema/xsd:complexType/xsd:sequence/xsd:element[@name="zip"]
/xsd:schema/xsd:complexType/xsd:attribute[@name="country"]
/xsd:schema/xsd:complexType/xsd:attribute/@name
/xsd:schema/xsd:complexType/xsd:attribute/@type
/xsd:schema/xsd:complexType/xsd:attribute/@fixed
/xsd:schema/xsd:simpleType[@name="SKU"]
/xsd:schema/xsd:simpleType/@name
/xsd:schema/xsd:simpleType/xsd:annotation
/xsd:schema/xsd:simpleType/xsd:annotation/xsd:documentation
/xsd:schema/xsd:simpleType/xsd:restriction
/xsd:schema/xsd:simpleType/xsd:restriction/@base
/xsd:schema/xsd:simpleType/xsd:restriction/xsd:pattern
/xsd:schema/xsd:simpleType/xsd:restriction/xsd:pattern/@value

This output is fine, for the purposes of this question.

Now, rather than asking “what’s wrong with my fugly code?” I’ll just ask the best way to proceed from a base state that actually works.

I need to add a clause that, for every line with a /xsd:documentation grandchild, appends the text from <xsd:documentation>foo</xsd:documentation>. So this line:

/xsd:schema/xsd:element[@name="comment"]

becomes:

/xsd:schema/xsd:element[@name="comment"] | doc for comment

Thanks,
Matt

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:24:03+00:00Added an answer on May 20, 2026 at 12:24 am

    The solution is simple:

    .1. Define the xsd namespace in your stylesheet:

    <xsl:stylesheet version="2.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    >
    

    .2. Change:

    .//documentation
    

    to

    .//xsd:documentation
    

    BTW, this is wrong:

    //xsd|//@*
    

    There is no element named xsd in the document. Probably you want:

    //xsd:*|//@* 
    

    Also, there is no need to use local-name() on attributes, just name() is OK.

    Second observation:

    There is nothing wrong with Alejandro’s solution. Applied to the provided XML document it produces the following correct output:

    /xsd:schema
    /xsd:schema/xsd:annotation
    /xsd:schema/xsd:annotation/xsd:documentation
    /xsd:schema/xsd:annotation/xsd:documentation/@xml:lang
    /xsd:schema/xsd:element
    /xsd:schema/xsd:element/@name
    /xsd:schema/xsd:element/@type
    /xsd:schema/xsd:element/xsd:annotation
    /xsd:schema/xsd:element/xsd:annotation/xsd:documentation
    /xsd:schema/xsd:complexType
    /xsd:schema/xsd:complexType/@name
    /xsd:schema/xsd:complexType/xsd:sequence
    /xsd:schema/xsd:complexType/xsd:sequence/xsd:element
    /xsd:schema/xsd:complexType/xsd:sequence/xsd:element/@name
    /xsd:schema/xsd:complexType/xsd:sequence/xsd:element/@type
    /xsd:schema/xsd:complexType/xsd:attribute
    /xsd:schema/xsd:complexType/xsd:attribute/@name
    /xsd:schema/xsd:complexType/xsd:attribute/@type
    /xsd:schema/xsd:complexType/xsd:attribute/@fixed
    /xsd:schema/xsd:complexType/xsd:annotation
    /xsd:schema/xsd:complexType/xsd:annotation/xsd:documentation
    /xsd:schema/xsd:simpleType
    /xsd:schema/xsd:simpleType/@name
    /xsd:schema/xsd:simpleType/xsd:restriction
    /xsd:schema/xsd:simpleType/xsd:restriction/@base
    /xsd:schema/xsd:simpleType/xsd:restriction/xsd:pattern
    /xsd:schema/xsd:simpleType/xsd:restriction/xsd:pattern/@value
    /xsd:schema/xsd:simpleType/xsd:annotation
    /xsd:schema/xsd:simpleType/xsd:annotation/xsd:documentation
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On another thread I saw a answer to a sql problem using Oracle's analytics.
I have a thread push-backing to STL list and another thread pop-fronting from the
Another easy one hopefully. Let's say I have a collection like this: List<DateTime> allDates;
I have one thread that writes results into a Queue. In another thread (GUI),
An answer and subsequent debate in the comments in another thread prompted me to
Is there an easy way to cancel a curl_easy_perform from another thread?
I need to have a thread signal another if the user wishes to interrupt
I have a Form being launched from another form on a different thread. Most
Given: Constructing an ADO Connection object from one thread and giving it to another
My form receives asynchronous callbacks from another object on random worker threads. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.