开发者

How to loop through xml elements using xsl

开发者 https://www.devze.com 2023-03-25 10:01 出处:网络
I have a scenario like this in xml: <ViewFields> <FieldRef Name=\"Planing Status\" /> <FieldRef Name=\"Resource Statu开发者_Go百科s\" />

I have a scenario like this in xml:

<ViewFields>
  <FieldRef Name="Planing Status" />
  <FieldRef Name="Resource Statu开发者_Go百科s" />
  <FieldRef Name="Development Status" />
  <FieldRef Name="Testing Status" />
</ViewStatus>

I have to loop through this in xsl, I followed below:

<xsl:template name="FieldRef_body.Status" match="FieldRef[@Name=/ViewFields/FieldRef[1]]" mode="body">

IT'S NOT RETURNING ANY THING @Name variable i am using several places in this section. I want to assign FieldRef values to @Name variable through loop.


<xsl:template name="FieldRef_body.Status" 
     match="FieldRef[@Name=/ViewFields/FieldRef[1]]" mode="body"> 

There are two things to note here:

  1. The match attribute contains an XPath expression that, when applied on the provided XML document, selects no node at all. It is supposed to select an element named FieldRef the string value of whose Name attribute is equal to the string value of the first FieldRef child of the ViewFields top element. However, none of the FieldRef elements in the XML document have any (non-empty) string value. On the other side, the string values of all Name attributes are non-empty. My guess is that you wanted: match="FieldRef[@Name=/ViewFields/FieldRef[1]/@Name]"

  2. Any template in a (non-anonymous) mode isn't considered by the XSLT processor for selection if its mode is different from the current mode.

As the initial mode in XSLT 1.0 (and in XSLT 2.0) is the empty (anonymous) mode, the only way to make a template in a non-anonymous mode considered for selection is to explicitly specify the mode on an <xsl:apply-templates> instruction.

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <xsl:apply-templates select="*/*" mode="body"/>
 </xsl:template>

 <xsl:template match="FieldRef[@Name=/ViewFields/FieldRef[1]/@Name]"
               mode="body">
  <xsl:value-of select="@Name"/>
 </xsl:template>
</xsl:stylesheet>

produces this result, showing that the template is selected for processing:

Planing Status
0

精彩评论

暂无评论...
验证码 换一张
取 消