xsl-list
[Top] [All Lists]

Re: [xsl] Some attribute not displayed in output

2009-08-30 00:20:19
Hi lee qm,

I stopped using v1 years ago and do not miss it so far, so I will let someone else pick up from here.

For the same input, in v2, changing your root template to

   <xsl:template match="/">
       <o t="{name(.)}">
           <xsl:for-each-group select="*" group-by="name()">
               <a n="{name(.)}">
                   <xsl:apply-templates select="current-group()"/>
               </a>
           </xsl:for-each-group>
       </o>
   </xsl:template>

produces the output that you specified.

On the other hand, your stylesheet seems to produce the following output, rather than what you mentioned:
<a n="info">
  <o t="info">
     <a n="user">
        <o t="user">
           <a n="id">
              <v s="123"/>
           </a>
        </o>
     </a>
     <a n="Product">
        <o t="Product">
           <a n="Category">
              <v s="A"/>
           </a>
        </o>
     </a>
     <a n="Product">
        <o t="Product">
           <a n="Category">
              <v s="B"/>
           </a>
        </o>
     </a>
  </o>
</a>

Cheers,
ac


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(_at_)hyperbase(_dot_)com> 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(_at_)hyperbase(_dot_)com> wrote:

Hi,

You could possibly simply add a template like

 <xsl:template match="o[(_at_)t='Info']">
     <xsl:element name="{(_at_)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="{(_at_)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="{(_at_)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>

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--