开发者

how to add condition which check xml data with lookup table

I want to ask. there is posible to add condition which will be checking xml data with lookup table, and if we didnt't have value in lookup table add const 8 to output? xslt Code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="Department" match="Department" use="../Collection"/>
    <xsl:template match="/">
    <document>
        <xsl:apply-templates/>
    </document>
</xsl:template>
 <xsl:template match="line">
     <xsl:variable name="inputDep" select="field[@id='3']"/>
<Department>
    <xsl:for-each select="document('lookup.xml')">
        <xsl:for-each select="key('Deparment',$inputDep)">
            <xsl:value-of select="."/>
        </xsl:for-each>
    </xsl:for-each>
</Department>

 </xsl:template>

</xsl:stylesheet>

lookup table:

<document>
    <line-item>
        <Collection>1</Collection>
        <Department>3</Department>
    </line-item>
    <line-item>
        <Collection>2</Collection>
        <Department>1</Department>
    </line-item>
    <line-item>
        <Collection>3</Collection>
        <Department>2</Department>
    </line-item>
</document>

xml file:

<document>
    <line id="0">
        <field id="3"><![CDATA[1]]></field>
    </line>
    <line id="1">
        <field id="3"/>
    </line>
    <line id="2">
        <field id="3"/><![CDATA[4]]></field>
    </line>
</document>

result:

<Department>3<Department>
<Department>8<Department>
<Department>开发者_如何学运维;8<Department>


You could assign the looked-up value to a variable and choose what to output based on whether anything was found.

Edit 2: A full demonstration stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:key name="Department" match="Department" use="../Collection"/>

  <xsl:template match="/">
    <document>
      <xsl:apply-templates/>
    </document>
  </xsl:template>

  <xsl:template match="line">
    <xsl:variable name="inputDep" select="field[@id='3']"/>
    <Department>
      <xsl:for-each select="document('lookup.xml')">
        <xsl:variable name="value" select="key('Department',$inputDep)"/>
        <xsl:choose>
          <xsl:when test="$value">
            <xsl:value-of select="$value"/> <!-- see note -->
          </xsl:when>
          <xsl:otherwise>8</xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </Department>
  </xsl:template>

</xsl:stylesheet>

Note: Replaced the xsl:for-each loop in the original stylesheet with a simple xsl:value-of, assuming that the looping the values was not intentional. If it actually was, you can replace this with a for-each loop.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜