【问题标题】:XSLT to recursively reorganize XML ChildrenXSLT 递归重组 XML 子项
【发布时间】:2019-06-03 16:21:00
【问题描述】:

我正在尝试将来自 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>jsmith@example.com</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>

你明白了要点。嵌套在页面中的问题嵌套在所有乱序中,这很丑陋。我现在只是不从 4th Survey Monky 页面元素中抓取并回答并将其一直放在前面。 XML 的 XSD 对元素的顺序非常严格,所以我必须在这里进行很多调整。我可以复制所有的孩子然后处理吗?有什么帮助/想法/祈祷吗?

-CJW

【问题讨论】:

  • 您想要的输出 XML 现在是格式正确的&lt;IDContainer&gt;&lt;FlexibleID&gt; 需要修复。

标签: xml xls


【解决方案1】:

您可以使用以下模板实现您想要的大部分功能。只需将您的 {...} 替换为适当的 &lt;xsl:value-of select="..." /&gt; 表达式即可。

<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...},因为它太不具体了。

【讨论】:

  • 对您在选择中使用的语法不够熟悉。引用一个元素来选择另一个元素。很确定这就是我要找的。会试一试的。
猜你喜欢
  • 1970-01-01
  • 2016-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-23
  • 1970-01-01
  • 1970-01-01
  • 2022-12-07
相关资源
最近更新 更多