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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inseting arbitrary number of tags 1

Status
Not open for further replies.

Naug

Technical User
Sep 24, 2004
85
RU
I have a problem with transforming xml - what I need to do is to insert a group of tags number of which depends on some other tags. For instance I need to add table-column tags to a table which equal to the number of cells in the first row. So the starting code would look something like

<table><tr><td/><td/></tr></table>


and the ending like

<table><tc/><tc/><tr><td/><td/></tr></table>


How do I do that?
 
Try this:
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]
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="table">
        <table>
            <xsl:for-each select="tr/td">
                <tc/>
            </xsl:for-each>
            <xsl:apply-templates/>
        </table>
    </xsl:template>
</xsl:stylesheet>
What's the tc tag for?
 
tc isn't an HTML tag. You should use col and colgroup. Was the transform I suggested ok?
 
Im not making an HTML and besides that was just an example as <table:table-column table:style-name="{@style}"> might have been a bit lengthy.

What is
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

for?

The method you suggested works except that I have modified it to tr[1]/td
 
xsl:copy-of automatically copies all child nodes and attributes of the selected node. xsl:copy doesn't. This code says copy all attributes and nodes, but allows you to specify a further template for child nodes/attributes. Thus the code copies the XML structure, but has a new template for the table node.

No need to change it to tr[1]/td. By default, the XSL processor will pick up only the first tr.

Jon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top