#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0

    Why does the xsl transformation miss on of my nodes?


    Below is an xml document, xsl and output of those two. Why does this miss outputting the querylist node? Thanks.

    My xml:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <n1:treasure  xmlns:n1="n1" xmlns:n2="n2">
      <querylist>
        <stuff>jjj</stuff>
        <stuff>rrrr</stuff>
        <n2:jnk>
          <n2:truck>
    	<motor>yes</motor>
    	<color>blue</color>
          </n2:truck>
          <n2:truck>
             <motor>no</motor>
             <color>red</color>
          </n2:truck>
        </n2:jnk>
    
      </querylist>
    </n1:treasure>
    My xsl:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="*|/">
       <xsl:for-each select="*">
      <xsl:element name="{local-name()}">
          <xsl:apply-templates select ="@*|node()" />
        </xsl:element>
      </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>
    Current Results ( why does it not contain querylist?)

    Code:
    <?xml version="1.0" encoding="utf-16"?>
    <treasure>
      <stuff>jjj</stuff>
    <stuff>rrrr</stuff>
    <jnk>
          <motor>yes</motor><color>blue</color>
          <motor>no</motor><color>red</color>
        </jnk>
    </treasure>
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,111
    Rep Power
    9398
    It also doesn't include the <truck>s.

    Your XSLT will skip alternating levels of hierarchy. The template will match one level, the for-each a second, and the node() the third (which is what gets passed to the template).

    I take it you're removing namespaces? Try this, shamelessly copied from SO:
    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes"/>
    
        <!-- Stylesheet to remove all namespaces from a document -->
        <!-- NOTE: this will lead to attribute name clash, if an element contains
            two attributes with same local name but different namespace prefix -->
        <!-- Nodes that cannot have a namespace are copied as such -->
    
        <!-- template to copy elements -->
        <xsl:template match="*">
            <xsl:element name="{local-name()}">
                <xsl:apply-templates select="@* | node()"/>
            </xsl:element>
        </xsl:template>
    
        <!-- template to copy attributes -->
        <xsl:template match="@*">
            <xsl:attribute name="{local-name()}">
                <xsl:value-of select="."/>
            </xsl:attribute>
        </xsl:template>
    
        <!-- template to copy the rest of the nodes -->
        <xsl:template match="comment() | text() | processing-instruction()">
            <xsl:copy/>
        </xsl:template>
    
    </xsl:stylesheet>
    It also handles attributes and other node types.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0
    Thanks for giving me something that works. I also need an understanding of how the processing takes place. I thought I had figured it out then this came along a told me that I did not know as much as I thought. I have read quite abit on the internet and bought a book but none of the sources explain the processing well enough for me. Do you know of a good source to help me know how the documents are processed? If I understood that I think I would not be struggling so much to get the transformations done. Thanks again.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0
    If you don't mind me digging a little deeper on this subject I need to know why these two act so differently. I thought I could define a template to start at the n1:treasure node and go from there like you example that you shared with me. But it does not behave as your other example did. It has the same xml but this is xsl:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:n1="n1" xmlns:n2="n2">
      
    <xsl:template match="n1:treasure">
         <xsl:element name="{local-name()}">
          <xsl:apply-templates select ="@*|node()" />
        </xsl:element>
    </xsl:template>
      <xsl:template match="*"></xsl:template>
     
    </xsl:stylesheet>
  8. #5
  9. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,111
    Rep Power
    9398
    Your * template is empty. It'll replace a node with nothingness.

    I can't really recommend anything to read: I'm weird in that I learn best by reading documentation (like the actual W3 standards ), not books or tutorials. But I'm pretty decent at explaining things.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by requinix
    Your * template is empty. It'll replace a node with nothingness.

    I can't really recommend anything to read: I'm weird in that I learn best by reading documentation (like the actual W3 standards ), not books or tutorials. But I'm pretty decent at explaining things.

    I am not really sure How to explain this, but here goes. I want to use the xml to create an html form. But the first step is to remove the namespacing and then use that result, getxmlNoNs, to create the html form. So I thought I could create a variable at the top of my xsl, getxmlNoNs, and then use that $getxmlNoNs to do the transformation to html. What you sent me took and xml and created an xml without the namespace but I could not get that result into a variable so that the remainder of my transformation would use the $getxmlNoNs. Any Ideas? Below is where I am starting.

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:n1="myNamespace">
    <xsl:output method="html"/>
    <xsl:variable name="xmlNoNs">
      <xsl:call-template name="getxmlNoNs" />
    </xsl:variable>
    <xsl:template match="">
    
      <html>
      <head>
          
      </head>
      <body>
     
      </body>
    </html>
    
       <xsl:template match="*" name= "xmlNoNs">
            <xsl:element name="{local-name()}">
                <xsl:apply-templates select="@* | node()"/>
            </xsl:element>
        </xsl:template>
    
        <!-- template to copy attributes -->
        <xsl:template match="@*">
            <xsl:attribute name="{local-name()}">
                <xsl:value-of select="."/>
            </xsl:attribute>
        </xsl:template>
    
        <!-- template to copy the rest of the nodes -->
        <xsl:template match="comment() | text() | processing-instruction()">
            <xsl:copy/>
        </xsl:template>
  12. #7
  13. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,111
    Rep Power
    9398
    Running XSLT on the result of an XSLT transformation isn't straightforward: when I looked into it myself a month or so back I learned that it relies on support from the XSLT processor itself, and that's not easy to come by.

    Let's approach this from a different angle. What's your source XML and what is the target HTML?

IMN logo majestic logo threadwatch logo seochat tools logo