Jump to content

Specifying which iteration of an element


Khisanth Magus

Recommended Posts

I have an xml file that has 3 sections appropriately named SECTION. Each contains the name of the section. The problem is that the name can change between source files, only the last 2 characters of the section name being the same in all of the source files. Those last 2 characters of the section names are PS, VR, and BS. I need to be able to specify which one I'm working on. The reason I need to do this is that there is the possibility that a section will be missing, and I need to be able to detect which one is missing, if any.

Link to comment
Share on other sites

You can detect if a certain string (ie a node's content) ends with something by using the ends-with() function. However, scince I don't see this function in any XPath 1.0 reference, suggesting it's only available in XPath 2.0, here's a totally XPath 1.0 alternative:

<xsl:variable name="string" select="SECTION"/><xsl:variable name="ends-with" select="'PS'"/><xsl:if test="substring($string,string-length($string) - string-length($ends-with), string-length($ends-with)) = $ends-wtih"><!--the code here will show only if SECTION ends with "PS".--></xsl:if>

Link to comment
Share on other sites

I've been able to get that much working. Here is the situation that I am having problems with:First, I am taking an xml document and putting it into a CSV format. The sections come in the same order in every document. There is the possibility within each document that 1, 2, or all 3 of the sections is missing. If a section is missing, i have to still have the commas in place where they would normally be.Here is an example:The first thing I get from all three sections is a number(that is forced into a 2 digit format). If all three sections are there, it looks like: *stuff before sections*,27,06,13,*stuff after*.Now, if the first section(PS) is missing, it would have to look like *stuff*,,06,13*stuff*.If section 2 is missing, it would look like *stuff*,27,,13,*stuff*If 1 and 3 were missing: *stuff*,,06,,*stuff*

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...