This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: modifiing string based on other nodes


> hai everybody
> I just got one problem and can anybody solve it:
>
> here is the sample xml file:
>
> (text omitted)
>
> what i want is:
> <PermanentName>S_LDD_DBMS_DATA_TYPE</PermanentName>
> <Value DataType="STRING">NUMBER</Value>,which is there
> in the line number4 of above xml file to be
>
> (more omitted)

The stylesheet below doesn't do everything you need, because demonstrates
the parts of XSLT that should handle all your program logic. The second
template rule copies all source tree nodes except for Value elements whose
DataType attribute has a value of INTEGER. The first template rule, for
these Value elements, adds an element to the result tree using an
xsl:element element and an xsl:attribute element. The xsl:attribute element
has program logic to figure out the value of the DataType attribute. If the
element's content has no period in it (the "precision=0" part), it checks
the content value and sets the DataType value appropriately.

Bob DuCharme            www.snee.com/bob             <bob@
snee.com>      see http://www.snee.com/bob/xsltquickly for
info on new book "XSLT Quickly" from Manning Publications.

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

<xsl:template match="Value[@DataType='INTEGER']">

<!-- Need to reference it a lot, so declare var  -->
  <xsl:variable name="val" select="."/>

    <xsl:element name="Value">

      <xsl:attribute name="DataType">
        <xsl:if test="not(contains($val,'.'))">
          <xsl:choose>
            <xsl:when test="$val &lt; 4">
              <xsl:text>SMALL INTEGER</xsl:text>
            </xsl:when>
            <xsl:when test="$val &gt; 15">
              <xsl:text>S_LDD_PRECISION</xsl:text>
            </xsl:when>
            <xsl:when test="($val &gt; 4) and ($val &lt; 15)">
              <xsl:text>BIG_INT</xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="@DataType"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:if>
      </xsl:attribute>

    <xsl:apply-templates/> <!-- output actual value -->

  </xsl:element>

</xsl:template>

<!-- Just copy everything else -->
<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]