Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

Unable to read values from the XML which is passed as a parameter to the XSLT

$
0
0

I need a help with the below situation.

I have two sources of XML which I am passing to the XSLT and read the values from both the XMLs by mapping the common field and derive the output, but I am not able to read the values from the XML file which is passed as the parameter to the XSLT.

Here is what I have tried…everything is working except that I am not able to map and read the values from the file which is passed as a parameter…

In this case, I am passing one of the XML as a parameter to the XSLT( which is File1, the content of File1 is given below) ...

C# Code:

privatestring TransformXML(string xml,string PathClient, string strKeyId)

       {

         

           //read XML

           TextReader tr1 = newStringReader(xml);

           XmlTextReader tr11 = newXmlTextReader(tr1);

           XPathDocument xPathDocument = newXPathDocument(tr11);

           

           //read XSLT

           XmlTextReader tr22 = newXmlTextReader(PathClient);

           XslCompiledTransform xslt = newXslCompiledTransform();

           XsltSettings xsettings = newXsltSettings(false,true);

           xsettings.EnableDocumentFunction = true;

           xslt.Load(tr22, xsettings, new XmlUrlResolver());

           //create the output stream

           StringBuilder sb = newStringBuilder();

           TextWriter tw = newStringWriter(sb);

           //Add doc....

           XmlDocument ptrDoc = newXmlDocument();

           ptrDoc.Load(@"C:/Users/PX/MYDOCUMENTS/1_File_1.xml");

           //xsl.Transform (doc, null, Console.Out);

           XsltArgumentList args = newXsltArgumentList();

           args.AddParam("ParamPKeyName","", strKeyId);

           args.AddParam("File1","", ptrDoc);

           

     

           xslt.Transform(xPathDocument, args, tw);

           return sb.ToString();

           

       }

File1 source (which is passed as parameter to the xslt):

<DATA>

<CHILD PNUM="1" PKID="" PKID1="" PKID2="" PName1="" PName2="" PName3="" PAdd1="" PAdd2="" PCity=""

PID="Form" />

   <CHILD PNUM="" PKID="" PKID1="" PKID2="" PName1="" PName2="" PName3="" PAdd1="" PAdd2="" PCity=""

PID="Line" />

   <CHILD PNUM="" PKID="" PKID1="" PKID2="" PName1="" PName2="" PName3="" PAdd1="" PAdd2="" PCity=""

PID="Code" />

   <CHILD PNUM="P No" PKID="PKey ID" PKID1="Other ID" PKID2="Other ID - 2" PName1="P Name Line 1"

PName2="P Name PAdd1="Street Address 1" PAdd2="Street Address 2" PCity="City" PID=""/>

  <CHILD PNUM="" PKID="PDetails" PKID1="" PKID2="" PName1="" PName2="" PName3="" PAdd1="" PAdd2=""

PCity="" PTD_USFORIND="" PTDI_ADDST="" PTDI_ADDZIP="" PID="Fund" PTDI_CUST5="" />

  <CHILD PNUM="1" PKID="1" PKID1="" PKID2="" PName1=" Name Line 1# " PName2="Name Line 1b" PName3=""

PAdd1=" 1 Mai$n S.t" PAdd2="Apt 1" PCity="NewCity" PID="1" />

   <CHILD PNUM="2" PKID="2" PKID1="" PKID2="" PName1="Name Line 2a" PName2="" PName3="" PAdd1="2 Main St"

PAdd2="" PCity="Berlin" PID="2" />

   <CHILD PNUM="3" PKID="3" PKID1="" PKID2="" PName1="First Middle Last" PName2="" PName3="" PAdd1="3

Main St" PAdd2="" PCity="Los Angeles" PID="3" /> 

 </DATA>

Now the source XML is as follows…

Source XML:

<XMLContent>

<CONTENT1>

 <DATA PRJTID="217" REGID="F_DATA">

   <CHILD PNUM="" PKID="" PKID1="" PKID2="" PName1="" PName2="" P_DOMFOR="" DP1="" DP2=""/>

   <CHILD PNUM="" PKID="" PKID1="" PKID2="" PName1="" PName2="" P_DOMFOR="" DP1="" DP2=""/>

   <CHILD PNUM="" PKID="" PKID1="" PKID2="" PName1="" PName2="" P_DOMFOR="" DP1="" DP2=""/>

   <CHILD PNUM="P No" PKID="P Key ID" PName1="Name Line 1" PName2="Name Line 2" P_DOMFOR="Domestic" />

   <CHILD PNUM="ROUNDING VAL" PKID="" PKID2="" PName1="TOTALS" PName2="" P_DOMFOR="" DP1="" DP2=""/>

   <CHILD PNUM="1" PKID="1" PKID1="" PKID2="" PName1="Name Line 1a" PName2="Name Line 1b"

P_DOMFOR="Domestic"  DP1="10" DP2="20"/>

   <CHILD PNUM="2" PKID="2" PKID1="" PKID2="" PName1="Name Line 2a" PName2="" P_DOMFOR="Foreign" 

DP1="100" DP2="200"/>

   <CHILD PNUM="3" PKID="3" PKID1="" PKID2="" PName1="First Middle Last" PName2="" P_DOMFOR="Domestic" 

DP1="1000" DP2="2000"/> 

 </DATA>

</CONTENT1>

</XMLContent>

The output I am looking for is –

Expected output:

For each of the PKID in the source XML,

For ex: when PKID=1

<Root>

   <ContentValues>

    <Name>

               Get the corresponding value of the PKID=1 of PName1 attribute from the Input XML which is = Name Line 1#

 </Name>

  

<Value1>

 Get the value of PKID=1 of DP1 (from the Source XML)

 </Value1>

<Value2>

Get the value of PKID=1 of DP2 (from the Source XML)

 </Value2>

</ContentValues>

</Root>

So for the selected PKID=1, 2, the output will be -

<Root>

   <ContentValues>

    <Name>

               Name Line 1#

 </Name>

   

<Value1>

 10

 </Value1>

<Value2>

20

 </Value2>

</ContentValues>

<ContentValues>

    <Name>

               Name Line 2a </Name>

    <Value1>

 100

 </Value1>

<Value2>

200

 </Value2>

</ContentValues>

</Root>

The XSLT code that I have tried is –

XSLT Code:

<?xml version="1.0" encoding="utf-8"?>

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

     xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"

     xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl"

 >

   <xsl:output method="xml" indent="yes"/>

 

<xsl:param name="File1" select="msxsl:node-set('')"/>

   

  <xsl:variable name="id-list" select="concat(',', $ParamPKeyName, ',')"/>

 

   <xsl:template match="/">

  

         <Return>

           <xsl:apply-templates select="XMLContent/CONTENT1/DATA/CHILD[(string(number(@PNUM)) != 'NaN') and (@PNUM != '') and contains($id-list, concat(',', @PNUM, ','))]"/>

         </Return>

     

  

   </xsl:template>

   <xsl:template match="CHILD">

     <xsl:variable name="RecordNo">

       <xsl:value-of select="position()"/>

     </xsl:variable>

    <xsl:variable name="PNo" select="@PNUM"/>

  <Root>

   <ContentValues>

    <Name>

 <xsl:copy-of select="File1"/> ( I am able to get the complete set of File-1 which is passed as parameter to XSLT, but not the Name Line 1# value)

 </Name>

  

                        <Value1>

 <xsl:value-of select="@DP1"/>

 </Value1>

                         <Value2>

 <xsl:value-of select="@DP2"/>

 </Value2>

                </ContentValues>

         </Root>

   </xsl:template>

 </xsl:stylesheet>

Could you please help with this, also if I load around 15000 records (CHILD elements) will that affect the performance)


Rpaul


Viewing all articles
Browse latest Browse all 935

Trending Articles