Page 1 of 1

A way to select a character at index point of tag??

Posted: Thu Dec 13, 2018 8:40 am
by voltaparabol
Hello everyone, I am somewhat new to XML and just begun using it A LOT at work. I have this problem I want to resolve but I am not sure if I can do this in XML. Basically, I want to take the very first character of a tag from incoming XML <App_Data App="MOD" Name="Episode_Number" Value="303"/> and assign it to another tag that is empty. I am trying to get the first character of Episode_Number which is 3 and represents the season number. I then want to put that into the season number tag but I am not sure how to go about that. My transform rule would look something like:

<input Episode_Number="*" Provider_ID="epix.com">
<Season_Number>Episode_Number.char(0)</Season_Number> **Episode_Number.char(0) is JAVA syntax because I dont know how else to express it**
</input>

Any advice would help. Please excuse my rookie-ness if I am posting in the wrong area or if this doesnt even make sense. :shock:

Re: A way to select a character at index point of tag??

Posted: Wed Dec 19, 2018 1:16 pm
by adrian
Hi,

Are you planning to do this by means of an XSLT transformation?

You seem to be calling everything a "tag", but some are attributes or attribute values. A tag usually refers to an element.
You have to learn a bit of the XML technology and its terminology before starting to use it. Without the terminology it is difficult to understand the means you plan to use and the desired result. I did get part of what is the desired result, but that is just a piece of the result that you are describing. Provide a broader input and output (desired result), not just a single entry.

This is the programmer's approach to XSLT (via xsl:for-each):

Code: Select all

        <xsl:template match="/">
<outroot>
<xsl:for-each select="//App_Data[@Name = 'Episode_Number']">
<input Episode_Number="{@Value}" Provider_ID="epix.com">
<Season_Number>
<xsl:value-of select="substring(@Value, 1, 1)"/>
</Season_Number>
</input>
</xsl:for-each>
</outroot>
</xsl:template>
This code assumes you have multiple "App_Data" elements and each useful one has the attribute Name = 'Episode_Number'. Assuming the output is still XML, you need a single root element. So, this code adds an "outroot" root element/tag for the output that surrounds all the "input" elements.
This outputs something like this:

Code: Select all

<outroot>
<input Episode_Number="303" Provider_ID="epix.com">
<Season_Number>3</Season_Number>
</input>
<input Episode_Number="405" Provider_ID="epix.com">
<Season_Number>4</Season_Number>
</input>
...
</outroot>
Regards,
Adrian