【问题标题】:How to Read all inner recursive values from XML using XSLT如何使用 XSLT 从 XML 中读取所有内部递归值
【发布时间】:2019-04-04 09:17:12
【问题描述】:

需要从递归XML结构的各级中提取值。所有级别的结构都相同

<regPackagingHierarchyList>
  <RegistrationPackagingHierarchy>
    <recordId>Level0</recordId>
    <regParentPackagingHierarchy>
      <RegistrationPackagingHierarchy>
        <recordId>level5</recordId>
        <regParentPackagingHierarchy>
          <RegistrationPackagingHierarchy>
            <recordId>level4</recordId>
            <regParentPackagingHierarchy>
              <RegistrationPackagingHierarchy>
                <recordId>level3</recordId>
                <regParentPackagingHierarchy>
                  <RegistrationPackagingHierarchy>
                    <recordId>level2</recordId>
                    <regParentPackagingHierarchy>
                      <RegistrationPackagingHierarchy>
                        <recordId>level1</recordId>
                      </RegistrationPackagingHierarchy>
                    </regParentPackagingHierarchy>
                  </RegistrationPackagingHierarchy>
                </regParentPackagingHierarchy>
              </RegistrationPackagingHierarchy>
            </regParentPackagingHierarchy>
          </RegistrationPackagingHierarchy>
        </regParentPackagingHierarchy>
      </RegistrationPackagingHierarchy>
    </regParentPackagingHierarchy>
  </RegistrationPackagingHierarchy>
</regPackagingHierarchyList>

预期结果应采用以下格式 Level1,Level2,Level3,level4,Level5

【问题讨论】:

  • 您是否可以有两个 RegistrationPackagingHierarchy 元素是彼此的兄弟姐妹,或者每个父元素只有一个 RegistrationPackagingHierarchy 子元素?
  • 永远只有一个孩子@TimC

标签: xml xslt xslt-1.0 wso2esb xslt-grouping


【解决方案1】:

1.0 中的一种方法可以是:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" />

<xsl:template match="/">
    <xsl:for-each select="//regParentPackagingHierarchy//recordId">
        <xsl:sort select="position()" order="descending" />
        <xsl:value-of select="." />
        <xsl:if test="position() != last()">
            <xsl:text>,</xsl:text>
        </xsl:if>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/pPzifpv

编辑:

<xsl:value-of select="concat('Count(regPackagingHierarchyList) = ', count(//regPackagingHierarchyList))"/>
<xsl:value-of select="concat('Count(RegistrationPackagingHierarchy) = ', count(//RegistrationPackagingHierarchy))"/>

编辑 2:

http://xsltfiddle.liberty-development.net/pPzifpv/8

【讨论】:

  • 非常感谢......它按预期工作@Vebbie
  • 如何为 计算子节点。预期输出为 Count(regPackagingHierarchyList)=17
  • @DhanaSekhar :在答案中添加了这一点。
  • @Vibbie,遇到了另一个问题。 这是标签重复,在运行 XSLT 时,我将所有内容组合在一起。像 level1,level2,level3,level4,level5level1,level2 ,level3,level4,level5。你能帮我解决这个问题吗?预计在第一次迭代中我应该得到 Iteration-1 ::level1,level2,level3,level4,level5 Iteration2:level1,level2,level3,level4,level5 将等待您的回复。在这里更新link
  • 这就是 XMl 结构的样子 父包列表父包列表父包列表
【解决方案2】:
<xsl:template match="*">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="recordId">
    <xsl:apply-templates/> 
    <!-- Apply all the templates beneath first and output your id after they got applied. -->
    <xsl:if test="ancestor::regParentPackagingHierarchy">
        <xsl:value-of select="."/>
    </xsl:if>
</xsl:template>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多