使用xslt循环遍历兄弟节点

时间:2013-03-28 10:48:03

标签: xml xslt xpath xslt-2.0

我第一次使用xslt和xpath所以请耐心等待。 我有以下xml(这只是它的一小部分):

<scriptflow description="" entrynodeid="301033924" leftbranch="809" name="Annual Charge and Banding v1.0" quickcode="" rightbranch="810" scriptflowid="301001711" status="1" templateid="-1">
- <scriptnode eventid="-1" name="Annual Charge" nodeid="301033924" templateid="1" templatename="action" text="[bookmark=action]Use Information on right hand screen to advise the customer of their annual charge" url="" xpos="100" ypos="130">
  <scriptnodeexit exitid="0" exitname="Info Provided" /> 
  <scriptnodeexit exitid="1" exitname="Does NOT Know Property Band" /> 
  <scriptnodeexit exitid="2" exitname="Customer Referred To Valuation Office" /> 
  <scriptnodeexit exitid="3" exitname="Transfer To Council Tax/Offer DDI" /> 
  </scriptnode>
  <subprocessnode nodeid="301033925" scriptflowid="101000700" scriptflowname="Anything Else v1.0" xpos="530" ypos="10" /> 
  <subprocessnode nodeid="301033926" scriptflowid="101000733" scriptflowname="Revenue Services - Enquiry Transfer v1.0" xpos="300" ypos="360" /> 
  <eventnode eventid="1474" name="Banding Charge" nodeid="301033927" xpos="346" ypos="50" /> 
- <scriptnode eventid="-1" name="Search for Banding" nodeid="301033928" templateid="1" templatename="action" text="[bookmark=action]Enter postcode and click on go <BR> <BR> Then enter house name/number and click on go [/bookmark]" url="" xpos="420" ypos="84">
  <scriptaction command="http://www.bracknell-forest.gov.uk/counciltaxbandreductions" componentid="10" type="0" /> 
  <scriptnodeexit exitid="0" exitname="Found Banding" /> 
  <scriptnodeexit exitid="1" exitname="Property In Another Borough" /> 
  <scriptnodeexit exitid="2" exitname="Banding NOT Found - Transfer To Revenue Services" /> 
  </scriptnode>
- <scriptnode eventid="-1" name="Details" nodeid="301033929" templateid="1" templatename="action" text="[bookmark=action]If customer is not happy with the information given, please refer them to valuation office [/bookmark]" url="" xpos="760" ypos="287">
  <scriptaction command="http://www.bracknell-forest.gov.uk/your-council/yc-council-tax/yc-amount-payable.htm" componentid="10" type="0" /> 
  <scriptnodeexit exitid="0" exitname="Customer Happy With Information" /> 
  <scriptnodeexit exitid="1" exitname="Customer Not Happy Offer Valuation Office Details" /> 
  </scriptnode>
  <eventnode eventid="1474" name="Banding Charge" nodeid="301033930" xpos="1016" ypos="20" /> 
  <subprocessnode nodeid="301033931" scriptflowid="101000700" scriptflowname="Anything Else v1.0" xpos="1120" ypos="20" /> 
- <scriptnode eventid="-1" name="Valuation" nodeid="301033932" templateid="1" templatename="action" text="[bookmark=action] If their propery banding is showing another authority, give them their details by click on authority name on right hand screen [/bookmark]" url="" xpos="810" ypos="190">
  <scriptnodeexit exitid="0" exitname="Continue" /> 
  </scriptnode>
- <scriptnode eventid="-1" name="Valuation Details" nodeid="301033933" templateid="1" templatename="action" text="[bookmark=action]Give customer details of the valuation office[/bookmark]" url="" xpos="1080" ypos="270">
  <scriptnodeexit exitid="0" exitname="Continue" /> 
  </scriptnode>
- <scriptnode eventid="-1" name="In Another Area" nodeid="301033934" templateid="1" templatename="action" text="[bookmark=action]Click on the authority name to find contact details[/bookmark]" url="" xpos="743" ypos="69">
  <scriptnodeexit exitid="0" exitname="Continue" /> 
  </scriptnode>
  <subprocessnode nodeid="301033935" scriptflowid="101000733" scriptflowname="Revenue Services - Enquiry Transfer v1.0" xpos="800" ypos="120" /> 
  <nodelink fromnode="301033924" fromnodeexit="0" tonode="301033927" /> 
  <nodelink fromnode="301033924" fromnodeexit="1" tonode="301033928" /> 
  <nodelink fromnode="301033924" fromnodeexit="2" tonode="301033932" /> 
  <nodelink fromnode="301033924" fromnodeexit="3" tonode="301033926" /> 
  <nodelink fromnode="301033927" fromnodeexit="0" tonode="301033925" /> 
  <nodelink fromnode="301033928" fromnodeexit="0" tonode="301033929" /> 
  <nodelink fromnode="301033928" fromnodeexit="1" tonode="301033934" /> 
  <nodelink fromnode="301033928" fromnodeexit="2" tonode="301033935" /> 
  <nodelink fromnode="301033929" fromnodeexit="0" tonode="301033930" /> 
  <nodelink fromnode="301033929" fromnodeexit="1" tonode="301033933" /> 
  <nodelink fromnode="301033930" fromnodeexit="0" tonode="301033931" /> 
  <nodelink fromnode="301033932" fromnodeexit="0" tonode="301033930" /> 
  <nodelink fromnode="301033933" fromnodeexit="0" tonode="301033930" /> 
  <nodelink fromnode="301033934" fromnodeexit="0" tonode="301033930" /> 
  </scriptflow>

我需要从这个xml创建一个流程图。将一个节点链接到另一个节点的事情就在这个xml的末尾:

      <nodelink fromnode="301033924" fromnodeexit="0" tonode="301033927" /> 
....
      <nodelink fromnode="301033933" fromnodeexit="0" tonode="301033930" /> 
      <nodelink fromnode="301033934" fromnodeexit="0" tonode="301033930" /> 
      </scriptflow>

我到目前为止的xslt如下:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="html" />

  <xsl:param name="logo"></xsl:param>
  <xsl:param name="folder"></xsl:param>
<xsl:key name="Keytest" match="scriptnode/scriptnodeexit[@exitid]" use="@exitid"/> 

    <xsl:template match="nodelink">
        <xsl:for-each select=".">
        <xsl:choose>
              <xsl:when test="(preceding-sibling::scriptnode/@nodeid)mod @fromnode=0">
            NodeID:<xsl:value-of select="preceding-sibling::scriptnode/@nodeid"/>,
            Name:<xsl:value-of select="preceding-sibling::scriptnode/@name"/>,
              </xsl:when>
              <xsl:otherwise>
                <xsl:text>*no nodeID/name match*</xsl:text>
              </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each>

            <xsl:choose>
              <xsl:when test="(preceding-sibling::scriptnode/scriptnodeexit/@exitid) mod @fromnodeexit=0 and (preceding-sibling::scriptnode/@nodeid) div @fromnode=1 and (preceding-sibling::scriptnode/@nodeid) = @fromnode">
                From Node ExitID: <xsl:value-of select="@fromnodeexit"/>
                From Node ID: <xsl:value-of select="@fromnode"/>,
                ExitID:<xsl:value-of select="preceding-sibling::scriptnode/scriptnodeexit/@exitid"/>,
        Exitname:<xsl:value-of select="preceding-sibling::scriptnode/scriptnodeexit/@exitname"/>
              </xsl:when>
        </xsl:choose>             

        <xsl:choose>
      <xsl:when test="(preceding-sibling::scriptnode/scriptnodeexit/@exitid)mod @fromnodeexit=0">
        ExitID:<xsl:value-of select="preceding-sibling::scriptnode/scriptnodeexit/@exitid"/>,
        Exitname:<xsl:value-of select="preceding-sibling::scriptnode/scriptnodeexit/@exitname"/>
    <img src="{$logo}" />
      </xsl:when>
    <xsl:otherwise>
      <xsl:text>*no editID/exitname match*</xsl:text><img src="{$logo}" />
    </xsl:otherwise>
    </xsl:choose>
          FromNodeExitID:<xsl:apply-templates select="@fromnodeexit"/>, 
          From Node: <xsl:apply-templates select="@fromnode"/>, 
          To Node: <xsl:apply-templates select="@tonode"/>
        <img src="{$logo}" /><br />
  </xsl:template>
</xsl:stylesheet>

我正在尝试创建一个树形结构,以便以后格式化,使其成为流程图,但我正在努力循环遍历scriptnode / scptnode退出属性。我无法上传所需流程图的图像但是我的xslt在正确的轨道上还是完全关闭?

1 个答案:

答案 0 :(得分:0)

使用nodelink:

从您的数据构建树
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" />

    <xsl:param name="logo"/>
    <xsl:param name="folder"/>

    <xsl:key name="nodes" match="*[@nodeid]" use="@nodeid"/>
    <xsl:key name="links" match="nodelink" use="@fromnode"/>

    <xsl:template match="/scriptflow">
        <h1>Entry point</h1>
        <ul>
            <xsl:apply-templates select="key('nodes', @entrynodeid)"/>
        </ul>
    </xsl:template>

    <xsl:template match="*[@nodeid]">
        <li>
            <h2><xsl:value-of select="local-name()"/>: <xsl:value-of select="@name"/></h2>
            <xsl:apply-templates select="scriptnodeexit"/>
        </li>
    </xsl:template>

    <xsl:template match="scriptnodeexit">
        <h3>Link: <xsl:value-of select="@exitname"/></h3>
        <ul>
            <xsl:apply-templates select="key('nodes', key('links', current()/../@nodeid)[@fromnodeexit=current()/@exitid]/@tonode)"/>
        </ul>
    </xsl:template>

</xsl:stylesheet>

<强> Working example