首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用xsl:for-each获取输入

如何使用xsl:for-each获取输入
EN

Stack Overflow用户
提问于 2020-12-29 07:56:57
回答 1查看 52关注 0票数 0

下面是我的输入请求。由于我的输入和XSLT都很长,所以我在这里缩短了它们。我将获得近10个ProductID的产品,我必须在不使用xsl:template match的情况下获取所有产品,因为我不能在XSLT中使用模板作为子元素。我应该如何用XSLT编写它以获得想要的输出?

输入:

代码语言:javascript
复制
<ChannelPartnerMembershipProfile>
<Dimension actionCode="4 ">
                                <ProductID>10000091</ProductID>
                                <ReceiverProductID>800GB</ReceiverProductID>
                                <DimensionStatusCode>02</DimensionStatusCode>
                                <StartDate>2020-12-07</StartDate>
                                <EndDate>2023-12-19</EndDate>
                            </Dimension>
                            <Dimension actionCode="4 ">
                                <ProductID>10000085</ProductID>
                                <ReceiverProductID>50GB</ReceiverProductID>
                                <DimensionStatusCode>02</DimensionStatusCode>
                                <StartDate>2020-12-06</StartDate>
                                <EndDate>2023-12-25</EndDate>
                            </Dimension>
</ChannelPartnerMembershipProfile>

XSLT:

代码语言:javascript
复制
<xsl:for-each select="//Dimension">
                    <eep:EemblInternalProduct>
                      <xsl:value-of select="following-sibling::EndDate[1]"/>
                       <eep:EndDate> 
                     <xsl:value-of select="$EndDate"/>
                     </eep:EndDate>
                     <eep:Name>
                       <xsl:value-of select="$ReceiverProductID"/>
                       </eep:Name>
                       <eep:Part>
                           <xsl:value-of select="ProductID"/>
                      </eep:Part>
                       <eep:StartDate>
                         <xsl:value-of select="$StartDate"/>
                      </eep:StartDate>
                      </eep:EemblInternalProduct>
                     </xsl:for-each>

我的预期产出:

代码语言:javascript
复制
<eep:EemblInternalProduct>
                     <eep:EndDate>2023-12-19</eep:EndDate>
                     <eep:Name>800GB</eep:Name>
                     <eep:Part>10000091</eep:Part>
                     <eep:StartDate>2020-12-07</eep:StartDate>
                  </eep:EemblInternalProduct>
                  <eep:EemblInternalProduct>
                     <eep:EndDate>2023-12-25</eep:EndDate>
                     <eep:Name>50GB</eep:Name>
                     <eep:Part>10000085</eep:Part>
                     <eep:StartDate>2020-12-06</eep:StartDate>
                  </eep:EemblInternalProduct> 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-29 08:18:29

我想在不使用“模板匹配”的情况下编写它,因为我不能在xslt中使用模板作为子元素。

没问题。您根本不需要嵌套<xsl:template>。这就是<xsl:apply-templates>的作用。

代码语言:javascript
复制
<xsl:template match="/">
    <list>
        <xsl:apply-templates select="//Dimension" />
    </list>
</xsl:template>

<xsl:template match="Dimension">
    <eep:EemblInternalProduct>
        <eep:EndDate> 
            <xsl:value-of select="EndDate" />
        </eep:EndDate>
        <eep:Name>
            <xsl:value-of select="ReceiverProductID" />
        </eep:Name>
        <eep:Part>
            <xsl:value-of select="ProductID" />
        </eep:Part>
        <eep:StartDate>
            <xsl:value-of select="StartDate" />
        </eep:StartDate>
    </eep:EemblInternalProduct>
</xsl:template>

结果:

代码语言:javascript
复制
<list xmlns:eep="...">
    <eep:EemblInternalProduct>
        <eep:EndDate>2023-12-19</eep:EndDate>
        <eep:Name>800GB</eep:Name>
        <eep:Part>10000091</eep:Part>
        <eep:StartDate>2020-12-07</eep:StartDate>
    </eep:EemblInternalProduct>
    <eep:EemblInternalProduct>
        <eep:EndDate>2023-12-25</eep:EndDate>
        <eep:Name>50GB</eep:Name>
        <eep:Part>10000085</eep:Part>
        <eep:StartDate>2020-12-06</eep:StartDate>
    </eep:EemblInternalProduct>
</list>

请注意,

  • <xsl:apply-templates>替换<xsl:for-each>.
  • <xsl:value-of select="$EndDate" />,选择变量$EndDate的值。你没有这样的变数。您希望选择元素<EndDate>的值。一个用<xsl:value-of select="EndDate" />.

完成的

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65489023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档