I think this is a beginner question, it is regarding the variable references across different XSLT templates. The structure is like this:
I firstly have an XSLT file, and in it, I’ve declared an template A, and within it, I basically loop through each record(row) of an XML file, say, xml_A, and I’ve declared a variable to refer to a particular field called “id” of the current record in xml_A. Then, within this for-each loop, I need to call another template B.
<xsl:template name="A">
<LoopA>
<xsl:for-each select="$xml_A//xml_A_row">
<xsl:variable name="id_A" select="id"/>
....
</LoopA>
<xsl:call-template name="B"/>
</xsl:template>
For template B, I actually created another XSLT file under the same directory. The B contains millions of records from xml file “xml_B”, but I only want the records that have the SAME id field as the current record of template A. Here is how I did it:
<xsl:template name="B">
<LoopB>
<xsl:variable name="id_A" select="id"/>
...
</loopB>
</xsl:template>
Here comes my question. I used the above <xsl:variable name="id_A" select="id"/> to refer to the id field of the current record that the upper loop is on. However, I am not sure whether this is correct, I’ve done some testing, since the data file is so huge, thus it is not easy to visually test if this is doing the right thing.
So I wonder if anybody could advise if the above reference to variable of the outer-loop is correct since if I delete that, the XML editor will complain.
Thanks in advance.
Update from comments
I think I didn’t make it clear enough.
Actually neitherxml_Aorxml_Bis the
xml files that are going to be XSLTed.
They are just data files which I will
use to pull data from, and there’s
another XML file C that is used as for
applying the stylesheet on and it is
empty. So in my style sheet, using
the provide answer, I must have a way to
make it refer to this data storage
file xml_A.
The typical XSLT style would be:
Note: Pattern matching instead of
xsl:for-eachand named templates,xsl:apply-templatesinstead ofxsl:call-template,current()function to reference context node.EDIT: In order to look more like your incomplete stylesheet fragment…
Note: Using
xsl:keybecause you wrote “millons of records”,xsl:for-eachwith singleton root node in order to change context node forfn:key()function, using outer most scope variable$xml_Afor cross reference key value. Themodeis there just in case there is conflic with other rules of the stylesheet you didn’t provide.