Reputation: 13
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:
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
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(.)<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