![](/img/trans.png)
[英]XSLT: How to transform the xml data into a hierarchical structure using XSLT
[英]XSLT: Processing hierarchical XML data using XSLT
我有以下 XML 文档。 每个节点下有许多子节点,但我只对其中的几个感兴趣。 我的目标是在其上运行 xslt 以生成仅包含我对它们感兴趣的元素的 Json 输出。
我采用的方法是首先将其作为 XML 处理(应用所有数据转换、重命名、仅输出我感兴趣的元素等)然后应用另一个 xslt 将其转换为 Json(我在网上找到了一些现成的 xslt,可以去做)。
我现在的问题主要在递归部分,我无法正确完成,我尝试了 for-each/apply-templates/call-template 但我仍然很难解决它。
<MainDoc>
<MetaData>
<Name>MainDoc Name</Name>
<Date>MainDoc Date</Date> <!--not interested in this element-->
</MetaData>
<ContentList>
<Content/>
<Content/>
<Content><!--Want to process last content only-->
<BuildList>
<Build>
<Steps>
<Step><!--want to process all of them-->
<StepContentParent>
<StepContent>
<Title>myTitle1</Title>
<Details>myDetails1</Details>
<Date>Step Date</Date> <!--not interested in this element-->
</StepContent>
</StepContentParent>
<Steps><!--Could be empty or could be the same as the previous Steps (recursion ) -->
<Step><!--want to process all of them-->
<StepContentParent>
<StepContent>
<Title>myTitle1.1</Title>
<Details>myDetails1.1</Details>
</StepContent>
</StepContentParent>
<Steps/><!--Could be empty or could be the same as the previous Steps (recursion ) -->
<SubDoc><!-- could be empty -->
<SubDocInstance>
<DocInstance>
<MainDoc><!-- Same as Root (recursion ) -->
<MetaData>
<Name>Sub Doc Name</Name>
</MetaData>
<ContentList>
<Content/>
<Content/>
<Content><!--Want to process last content only-->
<BuildList>
<Build>
<Steps>
<Step><!--want to process all of them-->
<StepContentParent>
<StepContent>
<Title>Sub Doc myTitle1</Title>
<Details>Sub Doc myDetails1</Details>
</StepContent>
</StepContentParent>
<Steps><!--Could be empty or could be the same as the previous Steps (recursion ) -->
<Step><!--want to process all of them-->
<StepContentParent>
<StepContent>
<Title>Sub Doc myTitle1.1</Title>
<Details>Sub Doc myDetails1.1</Details>
</StepContent>
</StepContentParent>
<Steps/><!--Could be empty or could be the same as the previous Steps (recursion ) -->
<SubDoc><!-- could be empty -->
<SubDocInstance>
<DocInstance>
<MainDoc/><!-- Same as Root (recursion ) -->
</DocInstance>
</SubDocInstance>
</SubDoc>
</Step>
<step/>
<step/>
</Steps>
<SubDoc><!-- could be empty -->
<SubDocInstance>
<DocInstance>
<MainDoc/><!-- Same as Root (recursion ) -->
</DocInstance>
</SubDocInstance>
</SubDoc>
</Step>
</Steps>
</Build>
</BuildList>
</Content>
</ContentList>
</MainDoc>
</DocInstance>
</SubDocInstance>
</SubDoc>
</Step>
<step/>
<step/>
</Steps>
<SubDoc><!-- could be empty -->
<SubDocInstance>
<DocInstance>
<MainDoc/><!-- Same as Root (recursion ) -->
</DocInstance>
</SubDocInstance>
</SubDoc>
</Step>
<Step>
<StepContentParent>
<StepContent>
<Title>myTitle2</Title>
<Details>myDetails2</Details>
</StepContent>
</StepContentParent>
<Steps><!--Could be empty or could be the same as the previous Steps (recursion ) -->
<Step><!--want to process all of them-->
<StepContentParent>
<StepContent>
<Title>myTitle2.1</Title>
<Details>myDetails2.1</Details>
</StepContent>
</StepContentParent>
<Steps/><!--Could be empty or could be the same as the previous Steps (recursion ) -->
<SubDoc><!-- could be empty -->
<SubDocInstance>
<DocInstance>
<MainDoc/><!-- Same as Root (recursion ) -->
</DocInstance>
</SubDocInstance>
</SubDoc>
</Step>
<step/>
<step/>
</Steps>
</Step>
<step/>
<step/>
</Steps>
</Build>
</BuildList>
</Content>
</ContentList>
</MainDoc>
由于使用带有apply-templates
的身份转换具有“内置递归”,我不确定问题是什么,您只需将其用作起点(例如,在 XSLT 3 中使用<xsl:mode on-no-match="shallow-copy"/>
),然后添加与要删除的元素匹配的空模板和/或与要重命名或转换的元素匹配的模板:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="MainDoc/MetaData/Date"/>
<xsl:template match="StepContentParent/StepContent/Date"/>
<xsl:template match="ContentList/Content[not(position() = last())]"/>
</xsl:stylesheet>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.