0

This is the input :

  <Data>
   <A_ID>123456789</A_ID>
   <A_Code>ojhgf</A_Code>
       <A_Rec>
         <inner1>2345</inner1>
        <inner2>14April</inner2>
         <inner3>15November</inner3>
      </A_Rec>
   </Data>

This is my XSLT:

  <xsl:variable name="AID" select="A_ID" />
  <xsl:variable name="ACode" select="A_Code" />
  <xsl:for-each xmlns:sch="http://schemas.w3.com/" select="//A_Rec">
        <sch:COMPANY>
             <xsl:value-of select="$AID" />
        </sch:COMPANY>
        <sch:COMPANY_CODE>
            <xsl:value-of select="$ACode" />
        </sch:COMPANY_CODE>
   </xsl:for-each>

I am trying to get the value "123456789" in the below mentioned line. $AID should hold 123456789, i am getting the desired value outside the for-each loop though. But I’m not getting AID and ACode values inside the for-each loop for Company and company code. What do I do?

  • In declaration of AID is it? – user3928739 Sep 01 '14 at 10:31
  • I think it would be better to show the _output you expect_. Asking about an attempted solution (variables and for-each) is pointless because it might lead nowhere. Show us what you are trying to achieve and we can point out how to do it in XSLT. – Mathias Müller Sep 01 '14 at 11:03
  • I am trying to get the value "123456789" in the below mentioned line. $AID should hold 123456789, i am getting the desired value outside the for-each loop though. – user3928739 Sep 01 '14 at 11:13
  • If I use your stylesheet inside `...`. I am getting your desired output. I am using saxon 6.5.5. What processor are you using? – Joel M. Lamsen Sep 01 '14 at 11:20

3 Answers3

1

You should be using "Data/A_ID". Assuming that your context node is not "Data" as I see nothing else wrong.

Lingamurthy CS
  • 5,412
  • 2
  • 13
  • 21
1

Once created, XSLT variables cannot change their value. There's a very good SO answer here that may help you redesign your XSLT.

Edit: Lingamurthy CS has correctly identified an issue with your XPath, but take a look at the SO page I've linked as it still might be useful.

Community
  • 1
  • 1
Matt Hogan-Jones
  • 2,981
  • 1
  • 29
  • 35
  • I'm not trying to change the values of the variables AID and ACode. I'm trying to assign it to different tags? Is there scope for these XSLT variables? – user3928739 Sep 01 '14 at 10:49
1

It seems you want to output A_ID and A_Code in a different node with a different namespace.

Try this instead:

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

    <xsl:strip-space elements="*"/>

    <xsl:output indent="yes"/>

    <xsl:template match="/">
        <root xmlns:sch="http://schemas.w3.com/">
            <xsl:apply-templates select="Data/A_ID|Data/A_Code"/>
        </root>
    </xsl:template>

    <xsl:template match="Data/A_ID">
        <xsl:element name="sch:COMPANY" namespace="http://schemas.w3.com/">
            <xsl:value-of select="."/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="Data/A_Code">
        <xsl:element name="sch:COMPANY_CODE" namespace="http://schemas.w3.com/">
            <xsl:value-of select="."/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>
Joel M. Lamsen
  • 7,143
  • 1
  • 12
  • 14