简体   繁体   English

XSLT:使用 XSLT 处理分层 XML 数据

[英]XSLT: Processing hierarchical XML data using XSLT

I have the below XML document.我有以下 XML 文档。 Under each node there are many sub nodes, but I am interested only in few of them.每个节点下有许多子节点,但我只对其中的几个感兴趣。 My target is to run xslt on it to generate a Json output contains only the elements I am interested in them.我的目标是在其上运行 xslt 以生成仅包含我对它们感兴趣的元素的 Json 输出。

The approach that I took is to process it as XML first (apply all data transformation, renaming, output only elements I am interested in ...etc) then apply another xslt to convert it to Json (I found some ready xslt online that can do that).我采用的方法是首先将其作为 XML 处理(应用所有数据转换、重命名、仅输出我感兴趣的元素等)然后应用另一个 xslt 将其转换为 Json(我在网上找到了一些现成的 xslt,可以去做)。

My issue right now mainly in the recursive part, I can't do it correct, I tried for-each/apply-templates/call-template but I still have hard time with it.我现在的问题主要在递归部分,我无法正确完成,我尝试了 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>

As using the identity transformation with apply-templates has "recursion built-in" I am not sure what the problem is, you just use it as the starting point (eg in XSLT 3 with <xsl:mode on-no-match="shallow-copy"/> ) and then add empty templates matching the elements you want to strip and/or templates matching for those elements you want to rename or transform otherwise:由于使用带有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>

https://xsltfiddle.liberty-development.net/6pS26my https://xsltfiddle.liberty-development.net/6pS26my

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM