【问题标题】:Create XSL for Xml to table transformation为 Xml 到表的转换创建 XSL
【发布时间】:2018-02-01 06:59:14
【问题描述】:

我有以下 XML 结构(文件 #1),我需要编写 XSL 文件以将其转换为不同的结构(文件 #2)。 目的:需要导入DB。

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

你能帮我了解一下 XSL 语法吗?

感谢您的帮助。

文件#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>

文件 #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>

【问题讨论】:

  • edit您的问题包含文件2信息。
  • 我无法发布这 2 个文件的代码。我收到一个错误 - 短文本长代码。

标签: xml xslt


【解决方案1】:

试试这个:

 <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 的转换

对于您的其他查询,试试这个

    <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>

【讨论】:

  • 感谢您的帮助。我收到以下错误:“xsl for-each 可能不包含分析字符串”。
  • xsl:analyze-string用在xslt-2.0中,你用的是哪个版本的xsl?
  • 我使用的是 xslt-1.0。它现在可以正常工作,没有错误。非常感谢你帮助我。
猜你喜欢
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
  • 2014-07-30
  • 1970-01-01
相关资源
最近更新 更多