简体   繁体   English

XSLT将通用节点转换为特定节点

[英]XSLT transforming generic nodes to specific nodes

I've been struggeling to perform a transformation on a document which has identical node names and needs to be split up in specific nodes. 我一直在努力对具有相同节点名称并且需要在特定节点中拆分的文档执行转换。 Visually you could say it's transforming rows to columns. 在视觉上,您可以说这是将行转换为列。 This is my source file: 这是我的源文件:

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

What I need is the following result: 我需要的是以下结果:

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

Can anybody help me? 有谁能够帮助我?

Well, it is rather trivial, I don't know why you're struggling with it: 好吧,这相当琐碎,我不知道您为什么要为此苦苦挣扎:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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