fragment vs. document?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jan Eliasen

    #1

    fragment vs. document?

    Hi

    I have some C# code that loops through an XMLDocument and needs to
    apply an XSLT stylesheet to each node in the XML document.

    My (simplified) code looks like this:
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.Load(fil ename);

    XmlNodeList nodeliste =
    xmldoc.SelectNo des("/*[local-name()='Guideli ne' and
    namespace-uri()='ns']/*[local-name()='ABIE' and
    namespace-uri()='ns']");

    foreach (XmlNode node in nodeliste)
    {
    XmlWriterSettin gs settings = new XmlWriterSettin gs();
    settings.Indent = true;
    settings.Indent Chars = "\t";
    settings.Encodi ng = Encoding.UTF8;
    settings.OmitXm lDeclaration = true;
    settings.Confor manceLevel = ConformanceLeve l.Fragment;

    StringBuilder createdContent = new StringBuilder() ;

    XmlWriter writer;
    writer = XmlWriter.Creat e(createdConten t, settings);

    XmlUrlResolver resolver = new XmlUrlResolver( );

    XslCompiledTran sform transform = new XslCompiledTran sform(true);
    transform.Load( @"XMLtoHTML.xsl ", XsltSettings.Tr ustedXslt, resolver);

    XsltArgumentLis t arguments = new XsltArgumentLis t();
    arguments.AddPa ram("country", "", country);

    transform.Trans form(tempFilena me, arguments, writer);
    }

    If I run this code, the output from the transform method is the
    innertext of the XmlNode given to the transform method. And NOT the
    output I expected :-)

    If I, however, create a new XmlDocument, load it with the
    node.OuterXml and save this XmlDocument to disc, then the transform
    method produces the correct output, given the new filename as input
    instead of the XmlNode.

    I'd really like not to ahve to write the node to disc before
    performing the transformation.

    Can anyone help me with that?

    --
    eliasen, representing himself and not the company he works for.

    Private blog: http://blog.eliasen.dk

    Private email: jan@eliasen.dk
  • Martin Honnen

    #2
    Re: fragment vs. document?

    Jan Eliasen wrote:
    I have some C# code that loops through an XMLDocument and needs to
    apply an XSLT stylesheet to each node in the XML document.
    >
    My (simplified) code looks like this:
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.Load(fil ename);
    >
    XmlNodeList nodeliste =
    xmldoc.SelectNo des("/*[local-name()='Guideli ne' and
    namespace-uri()='ns']/*[local-name()='ABIE' and
    namespace-uri()='ns']");
    >
    foreach (XmlNode node in nodeliste)
    {
    XmlWriterSettin gs settings = new XmlWriterSettin gs();
    settings.Indent = true;
    settings.Indent Chars = "\t";
    settings.Encodi ng = Encoding.UTF8;
    settings.OmitXm lDeclaration = true;
    settings.Confor manceLevel = ConformanceLeve l.Fragment;
    >
    StringBuilder createdContent = new StringBuilder() ;
    >
    XmlWriter writer;
    writer = XmlWriter.Creat e(createdConten t, settings);
    >
    XmlUrlResolver resolver = new XmlUrlResolver( );
    >
    XslCompiledTran sform transform = new XslCompiledTran sform(true);
    transform.Load( @"XMLtoHTML.xsl ", XsltSettings.Tr ustedXslt, resolver);
    >
    XsltArgumentLis t arguments = new XsltArgumentLis t();
    arguments.AddPa ram("country", "", country);
    >
    transform.Trans form(tempFilena me, arguments, writer);
    }
    >
    If I run this code, the output from the transform method is the
    innertext of the XmlNode given to the transform method. And NOT the
    output I expected :-)
    But your foreach loop does not even use the XmlNode |node| variable at
    all so it is not clear what you are trying to achieve in that loop.

    --

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

    Comment

    • Jan Eliasen

      #3
      Re: fragment vs. document?

      On Wed, 14 Feb 2007 15:15:01 +0100, Martin Honnen <mahotrash@yaho o.de>
      wrote:
      >Jan Eliasen wrote:
      >
      >I have some C# code that loops through an XMLDocument and needs to
      >apply an XSLT stylesheet to each node in the XML document.
      >>
      >My (simplified) code looks like this:
      >XmlDocument xmldoc = new XmlDocument();
      >xmldoc.Load(fi lename);
      >>
      >XmlNodeList nodeliste =
      >xmldoc.SelectN odes("/*[local-name()='Guideli ne' and
      >namespace-uri()='ns']/*[local-name()='ABIE' and
      >namespace-uri()='ns']");
      >>
      >foreach (XmlNode node in nodeliste)
      >{
      >XmlWriterSetti ngs settings = new XmlWriterSettin gs();
      >settings.Inden t = true;
      >settings.Inden tChars = "\t";
      >settings.Encod ing = Encoding.UTF8;
      >settings.OmitX mlDeclaration = true;
      >settings.Confo rmanceLevel = ConformanceLeve l.Fragment;
      >>
      >StringBuilde r createdContent = new StringBuilder() ;
      >>
      >XmlWriter writer;
      > writer = XmlWriter.Creat e(createdConten t, settings);
      >>
      >XmlUrlResolv er resolver = new XmlUrlResolver( );
      >>
      > XslCompiledTran sform transform = new XslCompiledTran sform(true);
      > transform.Load( @"XMLtoHTML.xsl ", XsltSettings.Tr ustedXslt, resolver);
      >>
      > XsltArgumentLis t arguments = new XsltArgumentLis t();
      > arguments.AddPa ram("country", "", country);
      >>
      > transform.Trans form(tempFilena me, arguments, writer);
      > }
      >>
      >If I run this code, the output from the transform method is the
      >innertext of the XmlNode given to the transform method. And NOT the
      >output I expected :-)
      >
      >But your foreach loop does not even use the XmlNode |node| variable at
      >all so it is not clear what you are trying to achieve in that loop.
      Sorry! Replaece the line
      transform.Trans form(tempFilena me, arguments, writer);
      with
      transform.Trans form(node, arguments, writer);

      The first one works, if I have saved the XML into a file given by
      tempFilename. The second version doesn't work, ie. just passing the
      XmlNode as a parameter to the transform method.

      --
      eliasen, representing himself and not the company he works for.

      Private blog: http://blog.eliasen.dk

      Private email: jan@eliasen.dk

      Comment

      • Martin Honnen

        #4
        Re: fragment vs. document?

        Jan Eliasen wrote:
        >>XmlNodeList nodeliste =
        >>xmldoc.Select Nodes("/*[local-name()='Guideli ne' and
        >>namespace-uri()='ns']/*[local-name()='ABIE' and
        >>namespace-uri()='ns']");
        transform.Trans form(node, arguments, writer);
        >
        The first one works, if I have saved the XML into a file given by
        tempFilename. The second version doesn't work, ie. just passing the
        XmlNode as a parameter to the transform method.
        Can you show us the stylesheet?

        --

        Martin Honnen --- MVP XML
        http://JavaScript.FAQTs.com/

        Comment

        • Jan Eliasen

          #5
          Re: fragment vs. document?

          On Wed, 14 Feb 2007 16:27:30 +0100, Martin Honnen <mahotrash@yaho o.de>
          wrote:
          >Can you show us the stylesheet?
          Sure



          --
          eliasen, representing himself and not the company he works for.

          Private blog: http://blog.eliasen.dk

          Private email: jan@eliasen.dk

          Comment

          • Jan Eliasen

            #6
            Re: fragment vs. document?

            On Wed, 14 Feb 2007 22:16:17 +0100, Jan Eliasen
            <eliasen@nospam .nospamwrote:
            >>Can you show us the stylesheet?
            >Sure
            >
            >http://www.eliasen.dk/xslt/convertXMLtoHTML.xsl
            Turns out, that I don't have to write the XmlNode to disk to make it
            work, I can just create a new XmlDocument, and
            xmldoc.LoadXml( node.OuterXml) and use the XmlDocument as a parameter
            instead of the XmlNode.

            But I'd still like to not having to do that and just use the XmlNode I
            have...

            --
            eliasen, representing himself and not the company he works for.

            Private blog: http://blog.eliasen.dk

            Private email: jan@eliasen.dk

            Comment

            • Martin Honnen

              #7
              Re: fragment vs. document?

              Jan Eliasen wrote:
              Turns out, that I don't have to write the XmlNode to disk to make it
              work, I can just create a new XmlDocument, and
              xmldoc.LoadXml( node.OuterXml) and use the XmlDocument as a parameter
              instead of the XmlNode.
              >
              But I'd still like to not having to do that and just use the XmlNode I
              have...
              The difference is that creating a new document with just that element
              makes this element the root node so XPath expression or match patterns like
              /*
              select/match that element.
              On the other hand when you directly pass that element node to the
              Transform method then transformation starts with that node and looks for
              a matching template but the node is not the root element, it still has
              all its ancestors. Thus if your stylesheet tries to process stuff
              <xsl:template match="/">
              or
              <xsl:template match="/*">
              those templates are not used, rather any template matching the element
              node will be used. So when you pass those nodes to the transform method
              you should make sure you have a template matching those nodes.


              --

              Martin Honnen --- MVP XML
              http://JavaScript.FAQTs.com/

              Comment

              • Jan Eliasen

                #8
                Re: fragment vs. document?

                On Thu, 15 Feb 2007 16:25:40 +0100, Martin Honnen <mahotrash@yaho o.de>
                wrote:
                >those templates are not used, rather any template matching the element
                >node will be used. So when you pass those nodes to the transform method
                >you should make sure you have a template matching those nodes.
                Thanks!

                --
                eliasen, representing himself and not the company he works for.

                Private blog: http://blog.eliasen.dk

                Private email: jan@eliasen.dk

                Comment

                Working...