By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,742 Members | 2,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,742 IT Pros & Developers. It's quick & easy.

Reading Code/Description lookup via inline XSLT

P: 23
I've been trying to piece together various code snippets to create a lookup table inside my xslt without the need for a supplemental xml file. Here is what I have so far. As of now, it does not return anything in the output. I would love to be able to query the xref:factor table to retrieve the value of xref:factor/lookup

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <!DOCTYPE xsl:stylesheet [<!ENTITY nbsp "*">]> 
  3. <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version="1.0"
  4.     xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
  5.     xmlns:xref="http://www.dummy.com/xref"
  6.     extension-element-prefixes="xref">
  7.  
  8.     <xsl:output method="html" version="3.0" encoding="iso-8859-1" indent="yes"/>
  9.  
  10.     <xref:factor>
  11.         <lookup factorcode="K">Test 1</lookup>
  12.         <lookup factorcode="I">Test 2</lookup>
  13.         <lookup factorcode="B">Test 3</lookup>
  14.         <lookup factorcode="G">Test 4</lookup>
  15.     </xref:factor>
  16.  
  17.     <xsl:key name="factor" match="xref:factor/lookup" use="@factorcode" />
  18.     <xsl:variable name="factors" select='document("")//xref:factor' />
  19.  
  20.     <xsl:template match="/CREDITDATA">
  21.         <html>
  22.             <head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head>
  23.             <body>
  24.                 <xsl:for-each select="scoreinformation">
  25.                     <xsl:variable name="origfactor" select="normalize-space(factor1)"/>
  26.                     <xsl:for-each select='$factors'>
  27.                         <xsl:value-of select='key("factor", $origfactor)'/>
  28.                     </xsl:for-each>
  29.                 </xsl:for-each>
  30.             </body>
  31.         </html>
  32.     </xsl:template>
  33. </xsl:stylesheet>
  34.  
Here is a sample of the xml file:

Expand|Select|Wrap|Line Numbers
  1. <CREDITDATA>
  2.     <scoreinformation ID="1">
  3.         <score>0519</score> 
  4.         <factor1>K</factor1> 
  5.         <factor2>I</factor2> 
  6.         <factor3>B</factor3> 
  7.         <factor4>G</factor4> 
  8.         <model>B</model> 
  9.     </scoreinformation>
  10. </CREDITDATA>
  11.  
Jan 7 '09 #1
Share this Question
Share on Google+
11 Replies


jkmyoung
Expert 100+
P: 2,057
The key element only works on your target document, not the xslt.


Expand|Select|Wrap|Line Numbers
  1. <xsl:for-each select="scoreinformation"> 
  2.   <xsl:value-of select="$factors//lookup[@factorcode = current()/factor1]"/>
  3. </xsl:for-each>
  4.  
Jan 8 '09 #2

P: 23
Here is the updated code, but it's still not outputing anything other than the static HTML.

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <!DOCTYPE xsl:stylesheet [<!ENTITY nbsp "*">]> 
  3. <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version="1.0"
  4.     xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
  5.     xmlns:xref="http://www.dummy.com/xref"
  6.     extension-element-prefixes="xref">
  7.  
  8.     <xsl:output method="html" version="3.0" encoding="iso-8859-1" indent="yes"/>
  9.  
  10.     <xref:factor>
  11.         <lookup factorcode="K">Test 1</lookup>
  12.         <lookup factorcode="I">Test 2</lookup>
  13.         <lookup factorcode="B">Test 3</lookup>
  14.         <lookup factorcode="G">Test 4</lookup>
  15.     </xref:factor>
  16.  
  17.     <xsl:key name="factor" match="xref:factor/lookup" use="@factorcode" />
  18.     <xsl:variable name="factors" select='document("")//xref:factor' />
  19.  
  20.     <xsl:template match="/CREDITDATA">
  21.         <html>
  22.             <head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head>
  23.             <body>
  24.                 <xsl:for-each select="scoreinformation">
  25.                     <xsl:value-of select="$factors//lookup[@factorcode = 'K']"/> <!--current()/factor1]"/>//-->
  26.                     <!--
  27.                     <xsl:variable name="origfactor" select="normalize-space(factor1)"/>
  28.                     <xsl:for-each select='$factors'>
  29.                         <xsl:value-of select='key("factor", $origfactor)'/>
  30.                     </xsl:for-each>
  31.                     //-->
  32.                 </xsl:for-each>
  33.             </body>
  34.         </html>
  35.     </xsl:template>
  36. </xsl:stylesheet>
  37.  
Jan 8 '09 #3

jkmyoung
Expert 100+
P: 2,057
??
Could you post your desired html?
Jan 8 '09 #4

P: 23
I supposed it should look like this:

Expand|Select|Wrap|Line Numbers
  1. <html xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  2.     <head><META http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>
  3.     <body>
  4.         Test 1
  5.     </body>
  6. </html>
  7.  
Jan 8 '09 #5

P: 23
I found that if I set the xml-stylesheet of the xml file, the lookup table works correctly. However, if I use XslTransform in c#, the lookup table does not work. I'm not sure what the difference is, but for now I'll just have to specify the xml-stylesheet for my xml file.
Jan 9 '09 #6

jkmyoung
Expert 100+
P: 2,057
I stand by my earlier post:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="iso-8859-1"?>  
  2. <!DOCTYPE xsl:stylesheet [<!ENTITY nbsp "*">]>   
  3. <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version="1.0"  
  4.     xmlns:msxsl="urn:schemas-microsoft-com:xslt"   
  5.     xmlns:xref="http://www.dummy.com/xref"  
  6.     extension-element-prefixes="xref">  
  7.  
  8.     <xsl:output method="html" version="3.0" encoding="iso-8859-1" indent="yes"/>  
  9.  
  10.     <xref:factor>  
  11.         <lookup factorcode="K">Test 1</lookup>  
  12.         <lookup factorcode="I">Test 2</lookup>  
  13.         <lookup factorcode="B">Test 3</lookup>  
  14.         <lookup factorcode="G">Test 4</lookup>  
  15.     </xref:factor>  
  16.  
  17.     <xsl:key name="factor" match="xref:factor/lookup" use="@factorcode" />  
  18.     <xsl:variable name="factors" select='document("")//xref:factor' />  
  19.  
  20.     <xsl:template match="/CREDITDATA">  
  21.         <html>  
  22.             <head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head>  
  23.             <body>  
  24. <xsl:for-each select="scoreinformation">   
  25.   <xsl:value-of select="$factors//lookup[@factorcode = current()/factor1]"/> 
  26. </xsl:for-each>  
  27.             </body>  
  28.         </html>  
  29.     </xsl:template>  
  30. </xsl:stylesheet>  
  31.  
Jan 9 '09 #7

P: 23
Definitely. That works great if I declare the stylesheet in the xml file. Unfortunately I am generating the xml dynamically and I can't set the stylesheet like that. So this seems to be a c# problem, rather than an xslt.

Thank you for your help!
Jan 12 '09 #8

jkmyoung
Expert 100+
P: 2,057
Could you post your C# code? There may be a setting where the document() function is enabled/disabled depending on a particular property.
Jan 12 '09 #9

P: 23
Sure!

Expand|Select|Wrap|Line Numbers
  1. // Get the generated xml
  2. XPathDocument xDoc = new XPathDocument(GetXML());
  3.  
  4. // Load the stylesheet and perform the transform.
  5. XslTransform xslt = new XslTransform();
  6. xslt.Load(@"c:\Temp\CreditReport.xslt");
  7. XmlTextWriter myWriter = new XmlTextWriter(@"c:\Temp\test.html", null) ;
  8. xslt.Transform(xDoc, null, myWriter, null);
  9.  
Jan 12 '09 #10

P: 23
Shoot, that was so simple! I'm still on .net 1.1, so I didn't think to look at the XmlUrlResolver class, but that did the trick.

Thank you again for your help!
Jan 12 '09 #12

Post your reply

Sign in to post your reply or Sign up for a free account.