Subject: Re: [xsl] Some attribute not displayed in output From: lee qm <akimilee@xxxxxxxxx> Date: Sun, 30 Aug 2009 00:16:45 +0800 |
Hi ac, This is my stylesheet, <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template> <xsl:template match="*"> <a n="{name(.)}"> <o t="{name(.)}"> <xsl:for-each select="@*"> <a n="{name(.)}"> <xsl:element name="v"> <xsl:attribute name="s"> <xsl:value-of select="normalize-space(.)"/> </xsl:attribute> </xsl:element> </a> </xsl:for-each> <xsl:apply-templates/> </o> </a> </xsl:template> </xsl:stylesheet> On Fri, Aug 28, 2009 at 10:43 PM, ac<ac@xxxxxxxxxxxxx> wrote: > Hi Lee, > > You may require some grouping, so are you using XSLT 1 or 2? > > Also, your "stylesheet" is only a template but the grouping may be required > a level above that template so providing that level also may be useful. > > The solution may be as simple as grouping similar child nodes, when at the > <info> level, to process them together, whether multiple <Users> or multiple > <Products> or any other. > > Cheers, > ac > > >> Thanks ac. >> The </header> is a copy and paste error; it should be </Info> instead. >> >> I had another problem which need help. >> >> src.xml >> ----------- >> <info> >> <user id="123" /> >> <Product Category="A" /> >> <Product Category="B" /> >> <!-- a lot of other element .......--> >> </info> >> >> Expected output >> ------------------------ >> <o t="info"> >> <a n="user"> >> <o t="user"> >> <a n="id"> >> <v s="123"> >> </a> >> </o> >> </a> >> <a n="Product"> >> <o t="Prodcut"> >> <a n="Category"> >> <v s="A"> >> </a> >> </o> >> <o t="Product"> >> <a n="Category"> >> <v s="B"> >> </a> >> </o> >> </a> >> </o> >> >> Stylesheet >> ---------------- >> So basically for each element that found in src file, we should write >> it in following format >> <a n="element name"> >> <o t="element name"> >> </o> >> </a> >> >> But when two similar element found, such as "Product", we should write >> it as below: >> <a n="element name"> >> <o t="element name"> >> </o> >> <o t="element name"> >> </o> >> </a> >> >> My stylesheet below only works for single element: >> >> <xsl:template match="*"> >> <a n="{name(.)}"> >> <o t="{name(.)}"> >> <xsl:for-each select="@*"> >> <a n="{name(.)}"> >> <xsl:element name="v"> >> <xsl:attribute name="s"> >> <xsl:value-of select="normalize-space(.)"/> >> </xsl:attribute> >> </xsl:element> >> </a> >> </xsl:for-each> >> <xsl:apply-templates/> >> </o> >> </a> >> </xsl:template> >> >> How do I modify the stylesheet so that it can handle multiple element >> with the same name to match the expected output? >> >> >> On Thu, Aug 27, 2009 at 11:45 AM, ac<ac@xxxxxxxxxxxxx> wrote: >> >>> >>> Hi, >>> >>> You could possibly simply add a template like >>> >>> <xsl:template match="o[@t='Info']"> >>> <xsl:element name="{@t}"> >>> <xsl:apply-templates select="a[(@n!='Info') and >>> (@n!='Sender')]"/> >>> <xsl:apply-templates select="a[(@n='Info') or (@n='Sender')]"/> >>> </xsl:element> >>> </xsl:template> >>> >>> which would ensure that attributes are processed first. >>> >>> But I can not see where the </Header> in your output comes from. >>> >>> Cheers, >>> ac >>> >>> >>> >>> >>>> >>>> I had following src xml file which needs to convert to the output.xml >>>> given below. >>>> I found some of the attributes ("MissingAttribute" in example given >>>> below) is not written to the output.xml with the stylesheet given. >>>> I check the spec and found that adding attribute after child node is >>>> added is illegal. >>>> >>>> Can someone help me on how to modify the stylesheet to get the >>>> expected output (where "MissingAttribute" appeared as a attribute of >>>> Header element)? >>>> >>>> src.xml (simplied version, the actual src files had many 'a' element) >>>> ----------- >>>> <?xml version="1.0" encoding="UTF-8"?> >>>> <data> >>>> <o t="request"> >>>> <a n="Info"> >>>> <o t="Info"> >>>> <a n="TimeStamp"> >>>> <v s="2009-08-81 12:59:59"/> >>>> </a> >>>> <a n="Sender"> >>>> <o t="Sender"> >>>> <a n="Login"> >>>> <v s="username"/> >>>> </a> >>>> </o> >>>> </a> >>>> <a n="MissingAttribute"> >>>> <v s="xxx"/> >>>> </a> >>>> </o> >>>> </a> >>>> </o> >>>> </data> >>>> >>>> output.xml >>>> ---------------- >>>> <?xml version="1.0" encoding="utf-8"?> >>>> <request> >>>> <Info TimeStamp="2009-08-81 12:59:59" MissingAttribute="xxx"> >>>> <Sender Login="username"/> >>>> </Header> >>>> </request> >>>> >>>> Stylesheet >>>> ---------------- >>>> <xsl:template match="o"> >>>> <xsl:element name="{@t}"> >>>> <xsl:apply-templates select="a"/> >>>> </xsl:element> >>>> </xsl:template> >>>> >>>> <xsl:template match="a"> >>>> <xsl:if test="(@n!='Info') and (@n!='Sender') "> >>>> <xsl:attribute name="{@n}"> >>>> <xsl:for-each select="v"> >>>> <xsl:value-of select="@s"/> >>>> </xsl:for-each> >>>> </xsl:attribute> >>>> </xsl:if> >>>> <xsl:if test="(@n='Info') or (@n='Sender') "> >>>> <xsl:apply-templates select="o"/> >>>> </xsl:if> >>>> </xsl:template>
Current Thread |
---|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Some attribute not displa, ac | Thread | Re: [xsl] Some attribute not displa, ac |
Re: [xsl] How to "flatten" a XML fi, Martin Honnen | Date | Re: [xsl] Some attribute not displa, ac |
Month |