简体   繁体   中英

Create XSL for Xml to table transformation

I have the following XML structure (file #1) and I need to write XSL file in order to transform it to the different structure (file #2). Purpose: need to import to DB.

In file #1 there may be multiple objects. Each object in file#1 will be transformed 4 records in my table based on XML file #2.

Can you help me with the XSL syntax?

Thank you for your help.

File #1:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Root>
    <Object>
        <location>[X12][Y20]</location>
        <serial>1224719</serial>
        <side_left>
            <color>black</color>
            <point>
                <name>1</name>
                <value>2</value>
            </point>
            <point>
                <name>2</name>
                <value>3</value>
            </point>
            <total>5</total>
        </side_left>
        <side_right>
            <color>yellow</color>
            <point>
                <name>1</name>
                <value>5</value>
            </point>
            <point>
                <name>2</name>
                <value>6</value>
            </point>
            <total>11</total>
        </side_right>
    </Object>
</Root>

File #2

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <Root>
        <MyTable>
            <serial>1224719</serial>
            <location>[X12][Y20]</location>
            <color>black</color>
            <name>1</name>
            <value>2</value>
        </MyTable>
        <MyTable>
            <serial>1224719</serial>
            <location>[X12][Y20]</location>
            <color>black</color>
            <name>2</name>
            <value>3</value>
        </MyTable>
        <MyTable>
            <serial>1224719</serial>
            <location>[X12][Y20]</location>
            <color>yellow</color>
            <name>1</name>
            <value>5</value>
        </MyTable>
        <MyTable>
            <serial>1224719</serial>
            <location>[X12][Y20]</location>
            <color>yellow</color>
            <name>2</name>
            <value>6</value>
        </MyTable>
    </Root>

Try this:

 <xsl:template match="Root">
    <xsl:copy>
        <xsl:for-each select="//point">
            <MyTable>
                <xsl:copy-of select="ancestor::Object/serial"/>
                <xsl:copy-of select="ancestor::Object/location"/>
                <xsl:copy-of select="../color"/>
                <xsl:copy-of select="*"/>
            </MyTable>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

See Transformation at http://xsltransform.net/aiwQ3u

For your other query try this

    <xsl:template match="Root">
    <xsl:copy>
        <xsl:for-each select="//point">
            <MyTable>
                <xsl:copy-of select="ancestor::Object/serial"/>
                <locationX><xsl:value-of select="substring-before(substring-after(ancestor::Object/location, 'X'), ']')"/></locationX>
                <locationY><xsl:value-of select="substring-before(substring-after(ancestor::Object/location, 'Y'), ']')"/></locationY>
                <xsl:copy-of select="../color"/>
                <xsl:copy-of select="*"/>
            </MyTable>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM