How to create XSL... ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Terrater
    New Member
    • Mar 2008
    • 3

    #1

    How to create XSL... ?

    Hi,

    I'm wondering if anybody could help me with the following?

    I have this XML:

    [code=xml]
    <?xml version="1.0" encoding="utf-8"?>
    <dataset xmlns="http://developer.cogno s.com/schemas/xmldata/1/" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
    <!--
    <dataset
    xmlns="http://developer.cogno s.com/schemas/xmldata/1/"
    xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
    xs:schemaLocati on="http://developer.cogno s.com/schemas/xmldata/1/ xmldata.xsd"
    >
    -->
    <metadata>
    <item name="Product type" type="xs:string " length="102"/>
    <item name="Quantity" type="xs:int" precision="1"/>
    </metadata>
    <data>
    <row>
    <value>Binocula rs</value>
    <value>107508 7</value>
    </row>
    <row>
    <value>Climbi ng Accessories</value>
    <value>585025 1</value>
    </row>
    <row>
    <value>Cookin g Gear</value>
    <value>13400351 </value>
    </row>
    </data>
    </dataset>
    [/code]

    which I want to transform to the following using XSL (the number of row nodes is not fixed):

    [code=xml]
    <?xml version="1.0" encoding="utf-8"?>
    <data>
    <row
    ProductType="Bi noculars"
    Quantity="10750 87"
    />
    <row
    ProductType="Cl imbing Accessories"
    Quantity="58502 51"
    />
    <row
    ProductType="Co oking Gear"
    Quantity="13400 351"
    />
    </data>
    [/code]

    Please notice that the metadata node (in the source XML) contains the names to be used in the resulting XML file as attribute names and that the name can contain one or more spaces (as indicated below).


    <metadata>
    <item name="Product type" type="xs:string " length="102"/>
    <item name="Quantity" type="xs:int" precision="1"/>
    </metadata>

    Thank you very much in advance...
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    I suggest you look at the following tutorial at w3schools:
    Sorry! We can't seem to find the resource you're looking for

    Comment

    • Terrater
      New Member
      • Mar 2008
      • 3

      #3
      I did visit: http://www.w3schools.c om/xsl/default.asp

      But there are no hints I can use regarding:

      1. How to extract the <metadata> info and use it in a <xsl:for-each ...> on the <data> part of the XML
      2. How to handle 2 consecutive <row>..</row> (same name used twice)
      3. How to strip out spaces in data 'collected' in the <metadata> (as described in the initial post)

      Thanks,

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        <xsl:template match="/">
        <xsl:copy-of select="//data"/>
        </xsl:template>

        Comment

        • Terrater
          New Member
          • Mar 2008
          • 3

          #5
          Thank you for the hint, but I still fail to see how to transform the first <value> in each <row> to "Product_Ty pe" and the second <row> to "Quantity".

          Thanks.

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            [code=xml]
            <xsl:template match="data|row ">
            <xsl:copy>
            <xsl:apply-templates/>
            </xsl:copy>
            </xsl:template>
            <xsl:template match="data">
            <xsl:variable name="pos" select="positio n()"/>
            <xsl:attribut e name="{//metadata/item[position() = $pos]/@name"/>
            <xsl:value-of select="."/>
            </xsl:attribute>
            </xsl:template>
            [/code]

            Comment

            Working...