elango j
elango j

Reputation: 13

xslt validation

I have the following XML code and I need to extract some specific attributes using the xslt. there might be 1000 of rows. it should loop through each row. if the FeatureDisplay is invalid it should show the corresponding part and the featurecode to the user that it is wrong. My validation conditions are:

  1. If the FeatureDisplay length is less than 5 throw an error
  2. If the FeatureDisplay length is longer than 5, then during the validation part, break the string into substrings of length of 6. Test the substring. The substring’s last value should be an ; or |. If the value at position 0 or 4 are a whitespace throw an error. If the value at position 0 thru 4 is something other than an alpha-numeric value or "@" or whitespace, throw an error. If there are more substrings, repeat the testing process.

if the FeatureDisplay value is 12345;98765; it should break as 12345; and 98765; and it should test each substring and throw the error if there is any invalid string is there.

My xml code is

<sample>
<row>
  <FeaturesDisplay>
     <NewValue>VLTUB2</NewValue>
  </FeaturesDisplay>
  <part>
    <NewValue>a</NewValue>
  </part>
</row>
<row>
  <FeaturesDisplay>
     <NewValue>VLTU</NewValue>
  </FeaturesDisplay>
  <part>
    <NewValue>b</NewValue>
  </part>
</row>
  
</sample>

Upvotes: 1

Views: 1201

Answers (1)

Peter
Peter

Reputation: 1796

You could do someting like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
    <list>
        <xsl:apply-templates/>
    </list>
</xsl:template>

<xsl:template match="//NewValue">
    <output>
    <xsl:choose>
        <xsl:when test="string-length(.)&lt;5">
            <xsl:value-of select="."/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="substring(.,1,1)"/>
        </xsl:otherwise>
    </xsl:choose>
    </output>
</xsl:template>


</xsl:stylesheet>

If the string length of NewValue is smaller than 5 give out that value otherwise only give out the first character. That XSL applied to your XML source gives this output:

<?xml version="1.0" encoding="UTF-8"?>
<list>
<output>V</output>
<output>VLTU</output>
</list>

Please adapt the XSLT to your specific needs.

Upvotes: 1

Related Questions