Number hierarchically across heterogeneous elements

M

Mike

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.
 
J

Joris Gillis

Hi,

Tempore 18:00:23 said:
<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?
 
J

Joris Gillis

Please forgive the ignorance displayed in my previous reply;)

Tempore 18:00:23 said:
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,
 
M

Mike

Joris said:
Please forgive the ignorance displayed in my previous reply;)
Please forgive me for not including a sample of the original XML.
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top