XSLT以递归方式重组XML Children

问题描述 投票:0回答:1

我试图将SurveyMonkey中的一些数据解析为内部应用程序的可用XML格式。 SurveyMonkey的结果非常笨重。问题按照他们出现的研究的“页面”进行排序。用于将答案归因于给定问题的值嵌套在某个子ID类型中。这是一个简短的例子:

<root>
...
    <pages>
        <questions>
            <answers>
                <text>John</text>
                <row_id>123456789</row_id>
            </answers>
            <answers>
                <text>Smith</text>
                <row_id>123456790</row_id>
            </answers>
            <answers>
                <text>[email protected]</text>
                <row_id>123456791</row_id>
            </answers>
            <id>987654321</id>
        </questions>
        <questions>
            <answers>
                <choice_id>144018495</choice_id>
            </answers>
            <id>987654322</id>
        </questions>
        <questions>
            <answers>
                <choice_id>489456456</choice_id>
            </answers>
            <answers>
                <choice_id>159489464</choice_id>
            </answers>
            <id>987654323</id>
        </questions>
        <id>48946496849</id>
    </pages>
    <pages>
        <questions>
            <answers>
                <choice_id>144018495</choice_id>
            </answers>
            <id>987654324</id>
        </questions>
        <questions>
            <answers>
                <choice_id>489456456</choice_id>
            </answers>
            <answers>
                <choice_id>159489464</choice_id>
            </answers>
            <id>987654325</id>
        </questions>
        <id>1541561518</id>
    </pages>
....
    <id>8594816548</id>
</root>

而我正在尝试将其转换为具有大量结构和顺序依赖性。使用{...}表示来自上方的值

<DataExchange>
    <Source>XMLIMPORT</Source>
    <EffectData>
        <DataSet>
            <Source>XMLIMPORT</Source> 
            <IDContainer>
                <FlexiID>
                    <key>ID_KEY</key>
                    <value>{/root/id}</value>
                </FlexibleID>
            </IDContainer>
            <MasterData>
                <FirstName>{/root/pages/questions/text where row_id = 123456789}</FirstName>
                <LastName>{/root/pages/questions/text where row_id = 123456790}</LastName>
                <ContactDetails>
                    <Email>{/root/pages/questions/text where row_id = 123456791}</Email>
                    <Phone1>{...}</Phone1>
                    <Phone2>{...}</Phone2>
                </ContactDetails>
            </MasterData>
            <Organization>hardcoded</Organization>
            {if Choice_ID = x in Question y then...}
            <Flags>
                <FromDate>
                    <key>ID_KEY</key>
                    <value>{/root/id}</value>
                </FlexibleID>
            </IDContainer>
            ...
        </DataSet>
    </EffectData>
</DataExchange>

你得到了要点。将嵌套在页面中的问题嵌套在所有乱序中的问题很难看。我现在不想从第4次调查Monky页面元素中获取并回答,并将其一直放在前面。 XML的XSD在元素的顺序上非常严格,所以我必须在这里进行大量的注册。我可以复制所有孩子然后处理吗?任何帮助/想法/祈祷?

-CJW

xml xls
1个回答
1
投票

您可以使用以下模板实现所需的大部分功能。只需用适当的{...}表达式替换你的<xsl:value-of select="..." />

<xsl:template match="/root">
    <DataExchange>
        <Source>XMLIMPORT</Source>
        <EffectData>
            <DataSet>
                <Source>XMLIMPORT</Source> 
                <IDContainer>
                    <FlexibleID>
                        <key>ID_KEY</key>
                        <value><xsl:value-of select="/root/id" /></value>
                    </FlexibleID>
                </IDContainer>
                <MasterData>
                    <FirstName><xsl:value-of select="/root/pages/questions/answers[row_id='123456789']/text" /></FirstName>
                    <LastName><xsl:value-of select="/root/pages/questions/answers[row_id='123456790']/text" /></LastName>
                    <ContactDetails>
                        <Email><xsl:value-of select="/root/pages/questions/answers[row_id='123456791']/text" /></Email>
                        <Phone1><xsl:value-of select="'Phone1'" /></Phone1>
                        <Phone2><xsl:value-of select="'Phone2'" /></Phone2>
                    </ContactDetails>
                </MasterData>
                <Organization>hardcoded</Organization>
                {if Choice_ID = x in Question y then...}
                <Flags>
                    <FromDate>
                        <key>ID_KEY</key>
                        <value><xsl:value-of select="/root/id" /></value>
                    </FromDate>
                    <!-- </FlexibleID> -->
                </Flags>
                <!-- </IDContainer>  -->
                ...
            </DataSet>
        </EffectData>
    </DataExchange>
</xsl:template>

我无法实现你的{if Choice_ID = x in Question y then...}因为它太不明确了。

© www.soinside.com 2019 - 2024. All rights reserved.