[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
XSL list:
I'm developing a stylesheet that checks for and inserts missing nodes and/or branches into an existing XML file that I can make minimal assumptions about. The input XML may or may not have any portion of the XML to be added. The order of the inserted nodes is important. Here's an example of the multiple potential input XML files, the desired output, and my attempt at the XSL to do the insert. Goal: to insert "child_c" node under "parent" node.
Potential input #3 (node "child_c" doesn't exist, node "child_b" doesn't either):
===========================================
<parent>
<child_a></child_a>
<child_d></child_d>
<child_e></child_e>
</parent>
<xsl:template match="*[self::child_a or self::child_b or self::child_c or self::child_d or self::child_e][3]">
<!-- if it's not child_c, then add it -->
<xsl:if test="name(.)!='child_c'">
<xsl:element name="child_c">
</xsl:element>
</xsl:if>
<!-- copy the element no matter what it is -->
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy> </xsl:template>
</xsl:stylesheet>
This stylesheet only manages to handle inputs #1 and #2 and generates the wrong output on #3 and #4. How would I generize this XSL to handle this insert for all cases?
Thanks,
-Eric
[xsl] complex "insert" rules; generalized insert
Subject: [xsl] complex "insert" rules; generalized insert From: Eric Compas <edcompas@xxxxxxxx> Date: Wed, 13 Oct 2004 13:19:27 -0500 |
XSL list:
I'm developing a stylesheet that checks for and inserts missing nodes and/or branches into an existing XML file that I can make minimal assumptions about. The input XML may or may not have any portion of the XML to be added. The order of the inserted nodes is important. Here's an example of the multiple potential input XML files, the desired output, and my attempt at the XSL to do the insert. Goal: to insert "child_c" node under "parent" node.
Potential input #1 (node "child_c" already exists): =========================================== <parent> <child_a></child_a> <child_b></child_b> <child_c></child_c> <child_d></child_d> <child_e></child_e> </parent>
Potential input #2 (node "child_c" doesn't exist): =========================================== <parent> <child_a></child_a> <child_b></child_b> <child_d></child_d> <child_e></child_e> </parent>
Potential input #3 (node "child_c" doesn't exist, node "child_b" doesn't either):
===========================================
<parent>
<child_a></child_a>
<child_d></child_d>
<child_e></child_e>
</parent>
Potential input #4 (node "child_c" exists, but in wrong location): =========================================== <parent> <child_c></child_c> <child_a></child_a> <child_b></child_b> <child_d></child_d> <child_e></child_e> </parent>
Desired output: (exept "child_b" would be missing in input #3): =========================================== <parent> <child_a></child_a> <child_b></child_b> <child_c></child_c> <child_d></child_d> <child_e></child_e> </parent>
My attempt at the stylesheet: =========================================== <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"> <xsl:output method="xml" indent="yes"/>
<xsl:template match="/"> <xsl:apply-templates/> </xsl:template>
<xsl:template match="*[self::child_a or self::child_b or self::child_c or self::child_d or self::child_e][3]">
<!-- if it's not child_c, then add it -->
<xsl:if test="name(.)!='child_c'">
<xsl:element name="child_c">
</xsl:element>
</xsl:if>
<!-- copy the element no matter what it is -->
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy> </xsl:template>
<!-- TEMPLATE: default --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template>
</xsl:stylesheet>
This stylesheet only manages to handle inputs #1 and #2 and generates the wrong output on #3 and #4. How would I generize this XSL to handle this insert for all cases?
Thanks,
-Eric
-- __________________________________ Eric Compas PhD Student Geography Department 550 Science Hall University of Wisconsin-Madison 500 North Park Street Madison, WI 53706 Email: edcompas@xxxxxxxx Phone: 608-238-1654
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Processing xsl within an , Wendell Piez | Thread | Re: [xsl] complex "insert" rules; g, Wendell Piez |
RE: [xsl] xsl:number with level=any, Richard Tobin | Date | Re: [xsl] Processing xsl within an , Wendell Piez |
Month |