I have a grid where you input the XPath to retrieve the values from an XML file. Client wants the last four of the Credit Card. I have come up with the following XPath Substring statement, but my usual XPath statements start with // and in the code I use a SelectSingleNode or SelectNodes. Now I do not want a node, want the actual value. How do I take this XPath Substring and extract the last 4 of the Credit Card Number?

substring(//CREDIT_CARD/CARD_NBR, string-length(//CREDIT_CARD/CARD_NBR) - 3)

Thanks

Use for-each to select all the nodes.

<xsl:template match="/">
<xsl:for-each select="//CREDIT_CARD/CARD_NBR">
<xsl:value-of select="substring(., string-length(.) - 3)"/>
</xsl:for-each>
</xsl:template>

So I need to do a transform to get my last 4 of the Credit Card? Are you thinking that it would be something like the following? Is there a way to do it without an XSL Transform?

myXMLFile = "c:\brendatest_input.xml"
        Dim myXSLFile As String = "c:\brendatest_convert.xsl"
        Dim destFileName As String = "c:\brendatest_output.xml"

        Dim myXslTransform As Xsl.XslCompiledTransform = New Xsl.XslCompiledTransform()
        myXslTransform.Load(myXSLFile)
        myXslTransform.Transform(myXMLFile, destFileName)

My XSL file

<?xml version="1.0" encoding="ISO-8859-1" ?> 
- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- <xsl:template match="/">
  <xsl:value-of select="substring (//CREDIT_CARD/CARD_NBR, string-length(//CREDIT_CARD/CARD_NBR) - 3)" /> 
  </xsl:template>
  </xsl:stylesheet>

So I need to do a transform to get my last 4 of the Credit Card? Are you thinking that it would be something like the following? Is there a way to do it without an XSL Transform?

myXMLFile = "c:\brendatest_input.xml"
        Dim myXSLFile As String = "c:\brendatest_convert.xsl"
        Dim destFileName As String = "c:\brendatest_output.xml"

        Dim myXslTransform As Xsl.XslCompiledTransform = New Xsl.XslCompiledTransform()
        myXslTransform.Load(myXSLFile)
        myXslTransform.Transform(myXMLFile, destFileName)

My XSL file

<?xml version="1.0" encoding="ISO-8859-1" ?> 
- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- <xsl:template match="/">
  <xsl:value-of select="substring (//CREDIT_CARD/CARD_NBR, string-length(//CREDIT_CARD/CARD_NBR) - 3)" /> 
  </xsl:template>
  </xsl:stylesheet>

xsl:value-of will select only current/context node and get the value. In your case, xsl:value-of will always dispaly only first credit-card value because your context/current node is root element. If you want to get the value-of all the credit-cards, in such case, you need to use xsl:for-each (for each credit card) and use xsl:value-of inside this xsl:for-each.

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="//CREDIT_CARD/CARD_NBR">
<xsl:value-of select="substring(., string-length(.) - 3)"/>
</xsl:for-each>
</xsl:template> 
</xsl:stylesheet>

[/QUOTE]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.