XSLT 1.0遍历同一父级的不同子节点

时间:2018-11-05 12:57:22

标签: xslt

我有一个xslt,它需要遍历三个具有不同值的子节点并将它们放在文件中。

到目前为止,它仅拾取第一个孩子,并将这些值放置在结果文件的所有三个节点中。我尝试了很多事情,但没有结果,有人可以帮我吗?

我搜索了整个网站多次,但无济于事。

这是需要转换的部分资源:

<followUpObjects>
<followUpObject length="4">
<followUpObjectNumber>1</followUpObjectNumber>
<followUpObjectTargetNumber>1</followUpObjectTargetNumber>
<followUpObjectName>Kostenplaats</followUpObjectName>
<followUpObjectValue desc="Kwaliteitsdienst">5260</followUpObjectValue>
</followUpObject>
<followUpObject length="13">
<followUpObjectNumber>2</followUpObjectNumber>
<followUpObjectTargetNumber>3</followUpObjectTargetNumber>
<followUpObjectName>kostensoort</followUpObjectName>
<followUpObjectValue desc="kostensoort1">kostensoort1</followUpObjectValue>
</followUpObject>
<followUpObject length="6">
<followUpObjectNumber>3</followUpObjectNumber>
<followUpObjectTargetNumber>2</followUpObjectTargetNumber>
<followUpObjectName>Project</followUpObjectName>
<followUpObjectValue desc="">2412</followUpObjectValue>
</followUpObject>
</followUpObjects>

我用这块xslt:

<CodingField>
<xsl:for-each
select="followUpObject[followUpObjectName='Kostenplaats']"></xsl:for-
each>
<Type>
<xsl:text>Costcenter</xsl:text>
</Type>
<Text>
<xsl:value-of
select="followUpObjects/followUpObject/followUpObjectValue"/>
</Text>
<Name>
<xsl:value-of
select="followUpObjects/followUpObject/followUpObjectValue/@desc"/>    
</Name>
<TextDetail>
<xsl:value-of
select="followUpObjects/followUpObject/followUpObjectValue"/>
</TextDetail>
<ShowAsSubLine>
<xsl:text>false</xsl:text>
</ShowAsSubLine>
</CodingField>
<CodingField>
<xsl:for-each
select="./followUpObject[followUpObjectName='kostensoort']"></xsl:for-
each>
<Type>
<xsl:text>Costunit</xsl:text>
</Type>
<Text>
<xsl:value-of
select="followUpObjects/followUpObject/followUpObjectValue"/>
</Text>
<Name>
<xsl:value-of
select="followUpObjects/followUpObject/followUpObjectValue/@desc"/>    
</Name>
<TextDetail>
<xsl:value-of
select="followUpObjects/followUpObject/followUpObjectValue"/>
</TextDetail>
<ShowAsSubLine>
<xsl:text>false</xsl:text>
</ShowAsSubLine>
</CodingField>
<CodingField>
<xsl:for-each
select="./followUpObject[followUpObjectName='Project']"></xsl:for-each>
<Type>
<xsl:text>Project</xsl:text>
</Type>
<Text>
<xsl:value-of
select="followUpObjects/followUpObject/followUpObjectValue"/>
</Text>
<Name>
<xsl:value-of
select="followUpObjects/followUpObject/followUpObjectValue/@desc"/>    
</Name>
<TextDetail>
<xsl:value-of
select="followUpObjects/followUpObject/followUpObjectValue"/>
</TextDetail>
<ShowAsSubLine>
<xsl:text>false</xsl:text>
</ShowAsSubLine>
</CodingField>

但是我每次都能得到:

Type Costcenter
Text 5260
Name Kwaliteitsdienst
TextDetail 5260

Type Costunit
Text 5260
Name Kwaliteitsdienst
TextDetail 5260

Type Project
Text 5260
Name Kwaliteitsdienst
TextDetail 5260

如您所见,第一个孩子每次都会用于填写除类型(我进行过硬编码)之外的所有其他编码字段。

1 个答案:

答案 0 :(得分:0)

您有三个xsl:for-each块为空。例如...

<xsl:for-each select="followUpObject[followUpObjectName='Kostenplaats']"></xsl:for-each>

您确实应该将要对每个项目执行的代码放在xsl:for-each中,而不是放在其后。

<CodingField>
  <xsl:for-each select="followUpObject[followUpObjectName='Kostenplaats']">
    <Type>
      <xsl:text>Costcenter</xsl:text>
    </Type>
    <Text>
      <xsl:value-of select="followUpObjectValue"/>
    </Text>
    <Name>
      <xsl:value-of select="followUpObjectValue/@desc"/>   
    </Name>
    <TextDetail>
      <xsl:value-of select="followUpObjectValue"/>
    </TextDetail>
    <ShowAsSubLine>
      <xsl:text>false</xsl:text>
    </ShowAsSubLine>
  </xsl:for-each>
</CodingField>

请注意,xsl:value-of语句不需要完整的补丁程序,而是相对于您选择的followUpObject

话虽如此,xsl:for-each仅选择一项,而您实际上并不想重复该代码三次。因此,改为让xsl:for-each选择所有项目。...

尝试使用此XSLT

 <xsl:for-each select="followUpObject">
   <CodingField>
    <Type>
      <xsl:choose>
        <xsl:when test="followUpObjectName = 'Kostenplaats'">Costcenter</xsl:when>
        <xsl:when test="followUpObjectName = 'kostensoort'">Costunit</xsl:when>
        <xsl:otherwise>Project</xsl:otherwise>
      </xsl:choose>
    </Type>
    <Text>
      <xsl:value-of select="followUpObjectValue"/>
    </Text>
    <Name>
      <xsl:value-of select="followUpObjectValue/@desc"/>   
    </Name>
    <TextDetail>
      <xsl:value-of select="followUpObjectValue"/>
    </TextDetail>
    <ShowAsSubLine>
      <xsl:text>false</xsl:text>
    </ShowAsSubLine>
  </CodingField>
</xsl:for-each>

http://xsltfiddle.liberty-development.net/pPqsHUo/1上查看此操作