首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为Xml到表的转换创建XSL

为Xml到表的转换创建XSL
EN

Stack Overflow用户
提问于 2018-02-01 14:59:15
回答 1查看 69关注 0票数 0

我有以下XML结构(文件#1),我需要编写XSL文件,以便将其转换为不同的结构(文件#2)。用途:需要导入到数据库中。

在文件#1中可能有多个对象。file#1中的每个对象将根据XML文件#2在我的表中转换为4条记录。

您能帮助我了解XSL语法吗?

谢谢你的帮助。

文件#1:

代码语言:javascript
复制
<?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>

文件#2

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-01 16:26:57

试试这个:

代码语言:javascript
复制
 <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>

请参阅http://xsltransform.net/aiwQ3u上的转换

对于您的其他查询,请尝试如下所示

代码语言:javascript
复制
    <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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48557004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档