Number hierarchically across heterogeneous elements

Discussion in 'XML' started by Mike, Jul 7, 2005.

  1. Mike

    Mike Guest

    Newbie question. I want to traverse an almost arbitrary XML formatted
    file and punch its attributes into a list. The list will contain the
    following information: Row-index, indent-level, element-name,
    attribute-name, attribute value.

    I was almost able to do this in two stages. The first XSLT script
    punched an intermediate XML file containing row elements that looked
    like this:
    <Row elementName="Loan" attributeName="Loan_ID"
    attributeValue="0123456789"/>
    <Row elementName="Borrower" attributeName="Borrower_ID"
    attributeValue="555"/>

    where Borrower is a sub-element of Loan. The problem is that I can't
    figure out how to make XSLT keep an indent count for arbitrary
    elements. What I want is an indent count for Loan = "1.0.0" and an
    indent count for Borrower = "1.1.0" to indicate that Borrower is (the
    1st) sub-element of Loan and that Loan is (the 1st) high level element.

    Any clue would be appreciated. BTW, here is my first stage script:


    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="xml" version="1.0" encoding="UTF-8"/>
    <!-- This script takes a Loan Setup XML file as input and generates an
    -->
    <!-- intermediate XML file as output. The reason for this step is to
    -->
    <!-- simplify the algorithm needed to produce the final SYLK formatted
    -->
    <!-- ASCII files that in turn will be used to populate Excel
    -->
    <!-- spreadsheets.
    -->
    <xsl:template match="/schema">
    <xsl:element name="schema">
    <xsl:apply-templates select="LoanFile"/>
    </xsl:element>
    </xsl:template>
    <xsl:template match="LoanFile">
    <xsl:element name="LoanFile">
    <xsl:apply-templates select="*">
    </xsl:apply-templates>
    </xsl:element>
    </xsl:template>

    <!-- For each attribute in the current element generate a Row element
    -->
    <!-- with new attributes
    -->
    <xsl:template match="@*">
    <xsl:param name="elementName"/>
    <xsl:variable name="namo"><xsl:value-of
    select="name()"/></xsl:variable>
    <xsl:element name="Row">
    <xsl:attribute name="elementName">
    <xsl:value-of select="$elementName"/>
    </xsl:attribute>
    <xsl:attribute name="attributeName">
    <xsl:value-of select="name()"/>
    </xsl:attribute>
    <xsl:attribute name="attributeValue">
    <xsl:value-of select="."/>
    </xsl:attribute>
    </xsl:element>
    </xsl:template>

    <!-- For each input element invoke the elementName template -->
    <xsl:template match="*">
    <!--Process attributes-->
    <xsl:apply-templates select="@*">
    <xsl:with-param name="elementName" select="name()"/>
    </xsl:apply-templates>
    <!--Process child elements-->
    <xsl:apply-templates select="*">
    </xsl:apply-templates>
    </xsl:template>

    </xsl:stylesheet>


    --Thank you,
    --Mike Jr.
     
    Mike, Jul 7, 2005
    #1
    1. Advertising

  2. Mike

    Joris Gillis Guest

    Hi,

    Tempore 18:00:23, die Thursday 07 July 2005 AD, hinc in foro {comp.text.xml} scripsit Mike <>:

    > <Row elementName="Loan" attributeName="Loan_ID"
    > attributeValue="0123456789"/>
    > <Row elementName="Borrower" attributeName="Borrower_ID"
    > attributeValue="555"/>
    >
    > where Borrower is a sub-element of Loan.

    In XML terms Borrower is not a sub-element of Loan; they are siblings. Is this a typo or am I not understanding this?

    --
    Joris Gillis (http://users.telenet.be/root-jg/me.html)
    Spread the wiki (http://www.wikipedia.org)
     
    Joris Gillis, Jul 7, 2005
    #2
    1. Advertising

  3. Mike

    Joris Gillis Guest

    Please forgive the ignorance displayed in my previous reply;)

    Tempore 18:00:23, die Thursday 07 July 2005 AD, hinc in foro {comp.text.xml} scripsit Mike <>:

    > What I want is an indent count for Loan = "1.0.0" and an
    > indent count for Borrower = "1.1.0" to indicate that Borrower is (the
    > 1st) sub-element of Loan and that Loan is (the 1st) high level element.


    Try something like:
    <xsl:attribute name="indent">
    <xsl:number count="*" level="multiple" from="LoanFile" format="1.1"/>
    </xsl:attribute>

    regards,
    --
    Joris Gillis (http://users.telenet.be/root-jg/me.html)
    Spread the wiki (http://www.wikipedia.org)
     
    Joris Gillis, Jul 7, 2005
    #3
  4. Mike

    Mike Guest

    Joris Gillis wrote:
    > Please forgive the ignorance displayed in my previous reply;)

    Please forgive me for not including a sample of the original XML.

    >
    > Tempore 18:00:23, die Thursday 07 July 2005 AD, hinc in foro {comp.text.xml} scripsit Mike <>:
    >
    > > What I want is an indent count for Loan = "1.0.0" and an
    > > indent count for Borrower = "1.1.0" to indicate that Borrower is (the
    > > 1st) sub-element of Loan and that Loan is (the 1st) high level element.

    >
    > Try something like:
    > <xsl:attribute name="indent">
    > <xsl:number count="*" level="multiple" from="LoanFile" format="1.1"/>
    > </xsl:attribute>


    Worked like a charm. I had tried something similar but sans the from
    attribute. I had incorrectly assumed that the problem was with
    count="*". That is, I incorrectly concluded that I had to specify an
    explicit node. Thank you!

    >
    > regards,
    > --
    > Joris Gillis (http://users.telenet.be/root-jg/me.html)
    > Spread the wiki (http://www.wikipedia.org)
     
    Mike, Jul 7, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kj
    Replies:
    1
    Views:
    366
    Ashmodai
    Apr 15, 2004
  2. Jason
    Replies:
    0
    Views:
    401
    Jason
    Aug 21, 2003
  3. Markus Dehmann
    Replies:
    8
    Views:
    2,099
    bartek
    Jun 2, 2004
  4. Replies:
    2
    Views:
    303
    Diez B. Roggisch
    Jun 9, 2005
  5. Arash Partow

    Heterogeneous containers with CRTP

    Arash Partow, May 15, 2007, in forum: C++
    Replies:
    2
    Views:
    769
    Axter
    May 15, 2007
Loading...

Share This Page