Reading Code/Description lookup via inline XSLT

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ianoble
    New Member
    • Oct 2008
    • 23

    Reading Code/Description lookup via inline XSLT

    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

    Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE xsl:stylesheet [<!ENTITY nbsp "*">]> 
    <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version="1.0"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
        xmlns:xref="http://www.dummy.com/xref"
        extension-element-prefixes="xref">
        
        <xsl:output method="html" version="3.0" encoding="iso-8859-1" indent="yes"/>
    
        <xref:factor>
            <lookup factorcode="K">Test 1</lookup>
            <lookup factorcode="I">Test 2</lookup>
            <lookup factorcode="B">Test 3</lookup>
            <lookup factorcode="G">Test 4</lookup>
        </xref:factor>
    
        <xsl:key name="factor" match="xref:factor/lookup" use="@factorcode" />
        <xsl:variable name="factors" select='document("")//xref:factor' />
        
        <xsl:template match="/CREDITDATA">
            <html>
                <head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head>
                <body>
                    <xsl:for-each select="scoreinformation">
                        <xsl:variable name="origfactor" select="normalize-space(factor1)"/>
                        <xsl:for-each select='$factors'>
                            <xsl:value-of select='key("factor", $origfactor)'/>
                        </xsl:for-each>
                    </xsl:for-each>
                </body>
            </html>
        </xsl:template>
    </xsl:stylesheet>
    Here is a sample of the xml file:

    Code:
    <CREDITDATA>
    	<scoreinformation ID="1">
    		<score>0519</score> 
    		<factor1>K</factor1> 
    		<factor2>I</factor2> 
    		<factor3>B</factor3> 
    		<factor4>G</factor4> 
    		<model>B</model> 
    	</scoreinformation>
    </CREDITDATA>
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    The key element only works on your target document, not the xslt.


    [code=xml]
    <xsl:for-each select="scorein formation">
    <xsl:value-of select="$factor s//lookup[@factorcode = current()/factor1]"/>
    </xsl:for-each>
    [/code]

    Comment

    • ianoble
      New Member
      • Oct 2008
      • 23

      #3
      Here is the updated code, but it's still not outputing anything other than the static HTML.

      Code:
      <?xml version="1.0" encoding="iso-8859-1"?>
      <!DOCTYPE xsl:stylesheet [<!ENTITY nbsp "*">]> 
      <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version="1.0"
          xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
          xmlns:xref="http://www.dummy.com/xref"
          extension-element-prefixes="xref">
       
          <xsl:output method="html" version="3.0" encoding="iso-8859-1" indent="yes"/>
       
          <xref:factor>
              <lookup factorcode="K">Test 1</lookup>
              <lookup factorcode="I">Test 2</lookup>
              <lookup factorcode="B">Test 3</lookup>
              <lookup factorcode="G">Test 4</lookup>
          </xref:factor>
       
          <xsl:key name="factor" match="xref:factor/lookup" use="@factorcode" />
          <xsl:variable name="factors" select='document("")//xref:factor' />
       
          <xsl:template match="/CREDITDATA">
              <html>
                  <head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head>
                  <body>
                      <xsl:for-each select="scoreinformation">
                          <xsl:value-of select="$factors//lookup[@factorcode = 'K']"/> <!--current()/factor1]"/>//-->
                          <!--
                          <xsl:variable name="origfactor" select="normalize-space(factor1)"/>
                          <xsl:for-each select='$factors'>
                              <xsl:value-of select='key("factor", $origfactor)'/>
                          </xsl:for-each>
                          //-->
                      </xsl:for-each>
                  </body>
              </html>
          </xsl:template>
      </xsl:stylesheet>

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        ??
        Could you post your desired html?

        Comment

        • ianoble
          New Member
          • Oct 2008
          • 23

          #5
          I supposed it should look like this:

          Code:
          <html xmlns:msxsl="urn:schemas-microsoft-com:xslt">
              <head><META http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>
              <body>
                  Test 1
              </body>
          </html>

          Comment

          • ianoble
            New Member
            • Oct 2008
            • 23

            #6
            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.

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              I stand by my earlier post:
              Code:
              <?xml version="1.0" encoding="iso-8859-1"?>  
              <!DOCTYPE xsl:stylesheet [<!ENTITY nbsp "*">]>   
              <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version="1.0"  
                  xmlns:msxsl="urn:schemas-microsoft-com:xslt"   
                  xmlns:xref="http://www.dummy.com/xref"  
                  extension-element-prefixes="xref">  
                
                  <xsl:output method="html" version="3.0" encoding="iso-8859-1" indent="yes"/>  
                
                  <xref:factor>  
                      <lookup factorcode="K">Test 1</lookup>  
                      <lookup factorcode="I">Test 2</lookup>  
                      <lookup factorcode="B">Test 3</lookup>  
                      <lookup factorcode="G">Test 4</lookup>  
                  </xref:factor>  
                
                  <xsl:key name="factor" match="xref:factor/lookup" use="@factorcode" />  
                  <xsl:variable name="factors" select='document("")//xref:factor' />  
                
                  <xsl:template match="/CREDITDATA">  
                      <html>  
                          <head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head>  
                          <body>  
              <xsl:for-each select="scoreinformation">   
                <xsl:value-of select="$factors//lookup[@factorcode = current()/factor1]"/> 
              </xsl:for-each>  
                          </body>  
                      </html>  
                  </xsl:template>  
              </xsl:stylesheet>

              Comment

              • ianoble
                New Member
                • Oct 2008
                • 23

                #8
                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!

                Comment

                • jkmyoung
                  Recognized Expert Top Contributor
                  • Mar 2006
                  • 2057

                  #9
                  Could you post your C# code? There may be a setting where the document() function is enabled/disabled depending on a particular property.

                  Comment

                  • ianoble
                    New Member
                    • Oct 2008
                    • 23

                    #10
                    Sure!

                    Code:
                    // Get the generated xml
                    XPathDocument xDoc = new XPathDocument(GetXML());
                    
                    // Load the stylesheet and perform the transform.
                    XslTransform xslt = new XslTransform();
                    xslt.Load(@"c:\Temp\CreditReport.xslt");
                    XmlTextWriter myWriter = new XmlTextWriter(@"c:\Temp\test.html", null) ;
                    xslt.Transform(xDoc, null, myWriter, null);

                    Comment

                    • jkmyoung
                      Recognized Expert Top Contributor
                      • Mar 2006
                      • 2057

                      #11
                      I think you use an XmlResolver: specificially XmlUrlResolver
                      XslTransform.Tr ansform Method (IXPathNavigabl e, XsltArgumentLis t, XmlWriter, XmlResolver) (System.Xml.Xsl )
                      XmlUrlResolver Class (System.Xml)

                      Comment

                      • ianoble
                        New Member
                        • Oct 2008
                        • 23

                        #12
                        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!

                        Comment

                        Working...