XSLT 递归重组 XML Children
XSLT to recursively reorganize XML Children
我正在尝试将 SurveyMonkey 中的一些数据解析为可用于 in-house 应用程序的 XML 格式。 SurveyMonkey 结果非常笨拙。问题按出现的 "Page" 研究排序。用于给定问题的答案的值嵌套在一些 child 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>
你明白了要点。嵌套在页面中的问题中嵌套的答案都是乱序的,这很丑陋。我只是现在不想从第 4 次 Survey Monky 页面元素中抓取并回答并将其一直放在前面。 XML 的 XSD 在元素顺序上非常严格,所以我必须在这里重新整理很多内容。我可以复制所有 children 出来然后处理吗?任何 help/thoughts/prayers?
-CJW
您可以使用以下模板实现您想要的大部分内容。只需用适当的 <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...}
,因为它太不具体了。
我正在尝试将 SurveyMonkey 中的一些数据解析为可用于 in-house 应用程序的 XML 格式。 SurveyMonkey 结果非常笨拙。问题按出现的 "Page" 研究排序。用于给定问题的答案的值嵌套在一些 child 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>
你明白了要点。嵌套在页面中的问题中嵌套的答案都是乱序的,这很丑陋。我只是现在不想从第 4 次 Survey Monky 页面元素中抓取并回答并将其一直放在前面。 XML 的 XSD 在元素顺序上非常严格,所以我必须在这里重新整理很多内容。我可以复制所有 children 出来然后处理吗?任何 help/thoughts/prayers?
-CJW
您可以使用以下模板实现您想要的大部分内容。只需用适当的 <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...}
,因为它太不具体了。