Question

I am exporting datebase from Access in that looking XML

<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2014-05-11T15:51:32">
    <Mnc_x0020_172>
        <ID>1</ID>
        <package_id>minecraft</package_id>
        <cat>lib</cat>
        <www>#http://minecraft.net/#</www>
        <nazwa>Minecraft</nazwa>
        <author>Mojang</author>
        <opis>Game - build your own world!</opis>
        <img>#/mc.png#</img>
    </Mnc_x0020_172>
    <Mnc_x0020_172>
        <ID>2</ID>
        <package_id>modloader</package_id>
        <cat>lib</cat>
        <www>#http://minecraftforum.net/topic/75440-x/#</www>
        <nazwa>ModLoader</nazwa>
        <author>Risugami</author>
        <opis>ModLoader - library to load mods</opis>
        <img>#/gen.png#</img>
    </Mnc_x0020_172>
    ...
</dataroot>

And so on.

I want to convert it to this kind:

<?xml version="1.0" encoding="UTF-8"?>
<xml>
    <package id="minecraft" cat="lib">
        <nazwa>Minecraft</nazwa>
        <author>Mojang</author>
        <www>http://minecraft.net/</www>
        <opis>Game - build your own world!</opis>
        <img>/mc.png</img>
    </package>
    <package id="modloader" cat="lib">
        <nazwa>ModLoader</nazwa>
        <author>Risugami</author>
        <www>http://minecraftforum.net/topic/75440-x/</www>
        <opis>ModLoader - library to load mods</opis>
        <img>/gen.png</img>
    </package>
...

And so on.

What XSLT code use to transform it?

I haven't use it before so I am blank about it. I wrote smth like that for now:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="ID">

        <xsd:element name="cat" type="catType"/>
          <xsd:complexType name="catType">
             <xsd:sequence>
                <xsd:element name="nazwa"      type="xsd:string"/>
                <xsd:element name="author"     type="xsd:string"/>
                <xsd:element name="opis"       type="xsd:string"/>
                <xsd:element name="www"        type="xsd:string"/>
                <xsd:element name="img"        type="xsd:string"/>
                <xsd:element name="depends"    type="xsd:string"/>
                <xsd:element name="conflicts"  type="xsd:string"/>
                <xsd:element name="after"      type="xsd:string"/>
                <xsd:element name="replaces"   type="xsd:string"/>
             </xsd:sequence>
          </xsd:complexType>
        </xs:element>

        <xsd:attribute name="package id" type="xsd:string"/>

        <xsl:for-each select="package_id/cd">
            <xsl:value-of select="cat"/>
            <xsl:value-of select="artist"/>
            <xsl:value-of select="country"/>
        </xsl:for-each>

    </xsl:template>
</xsl:stylesheet>
Was it helpful?

Solution

This XSL transform applied on the source XML you provided will generate the expected result:

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes"/>

    <xsl:template match="dataroot">
        <xml><xsl:apply-templates/></xml>
    </xsl:template>

    <xsl:template match="Mnc_x0020_172">
        <package id="{package_id}" cat="{cat}">
            <nazwa><xsl:value-of select="nazwa"/></nazwa>
            <author><xsl:value-of select="author"/></author>
            <www><xsl:value-of select="translate(www,'#','')"/></www>
            <opis><xsl:value-of select="opis"/></opis>
            <img><xsl:value-of select="translate(img,'#','')"/></img>
        </package>
    </xsl:template>

</xsl:stylesheet>

The match attribute in each xsl:template matches a node in your source document using XPath. The first template matches the dataroot node, adds a <xml>...</xml> root node to the output and calls for the processing of the other templates. The second template will match a nodeset formed by your two Mnc-x0020_172 nodes, will generate a <package> element for each one and will select the nodes from your source (in context) and place them in an element of the same name. translate is used to remove the #.

There are other ways to obtain the same results. If you didn't have a namespace declaration, you could have used <xsl:copy-of select="node"> to copy the nodes that are identical on both documents.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top