[英]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.