【问题标题】:XSLT transforming generic nodes to specific nodesXSLT 将通用节点转换为特定节点
【发布时间】:2014-12-27 13:41:06
【问题描述】:

我一直在努力对具有相同节点名称并且需要在特定节点中拆分的文档执行转换。从视觉上你可以说它正在将行转换为列。这是我的源文件:

<?xml version="1.0" encoding="UTF-8"?>
<Document>
<Car>
    <Element>
        <Question>Brand</Question>
        <Answer>Ford</Answer>
    </Element>
    <Element>
        <Question>Year</Question>
        <Answer>1995</Answer>
    </Element>
</Car>
<Car>
    <Element>
        <Question>Brand</Question>
        <Answer>Hummer</Answer>
    </Element>
    <Element>
        <Question>Year</Question>
        <Answer>1990</Answer>
    </Element>
</Car>
</Document>

我需要的是以下结果:

<Document>
    <Car>
        <Brand>Ford</Brand>
        <Year>1995</Year>
    </Car>
    <Car>
        <Brand>Hummer</Brand>
        <Year>1990</Year>
    </Car>
</Document>

有人可以帮我吗?

【问题讨论】:

  • 这是可能的,但很危险:如果任何问题包含的值不是有效的元素名称,您将收到致命错误。
  • 我知道你的意思,但这不是问题,在源系统中,节点“品牌”是一个不会改变的实际问题......这是因为出口可能性不佳我需要做这个转变。

标签: xml xslt transformation


【解决方案1】:

好吧,这很琐碎,我不知道您为什么要为此苦苦挣扎:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/Document">
    <Document>
        <xsl:for-each select="Car">
            <Car>
                <xsl:for-each select="Element">
                    <xsl:element name="{Question}">
                        <xsl:value-of select="Answer" />
                    </xsl:element>
                </xsl:for-each>
            </Car>
        </xsl:for-each>
    </Document>
</xsl:template>

</xsl:stylesheet>

【讨论】:

  • 这确实比我想象的要容易,非常感谢您的快速回复!
猜你喜欢
  • 2014-07-16
  • 1970-01-01
  • 2017-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
  • 1970-01-01
相关资源
最近更新 更多