• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

Transforming XML with SelectSingleNode

Hi XMLers

I have some VB Script that processes some XML using XSLT.
I wanted to select just one node based on some request variables and then display that in HTML using XSLT.

I can select the correct node, but the XSLT simply draws out the values in one line.

Can someone help me .?

---------------
XML:
<PORTFOLIO>
      <PORTFOLIO_NAME id="Latest">
            <PORTFOLIO_PAGE id="1">
                  <CLIENT>
                        <CLIENT_NAME>AAAAAAAAAA</CLIENT_NAME>
                        <CLIENT_URL>AAAAAAAAAA</CLIENT_URL>
                        <CLIENT_IMAGE>AAAAAAAAAA</CLIENT_IMAGE>
                        <CLIENT_TEXT>AAAAAAAAAA.</CLIENT_TEXT>
                  </CLIENT>
                  <CLIENT>
                        <CLIENT_NAME>BBBBBBBBB</CLIENT_NAME>
                        <CLIENT_URL>BBBBBBBBB</CLIENT_URL>
                        <CLIENT_IMAGE>BBBBBBBBB</CLIENT_IMAGE>
                        <CLIENT_TEXT>BBBBBBBBB</CLIENT_TEXT>
                  </CLIENT>
                  <CLIENT>
                        <CLIENT_NAME>CCCCCCCCC</CLIENT_NAME>
                        <CLIENT_URL>CCCCCCCCC</CLIENT_URL>
                        <CLIENT_IMAGE>CCCCCCCCC</CLIENT_IMAGE>
                        <CLIENT_TEXT>CCCCCCCCC</CLIENT_TEXT>
                  </CLIENT>
            </PORTFOLIO_PAGE>
            <PORTFOLIO_PAGE id="2">
                  <CLIENT>
                        <CLIENT_NAME>NNN</CLIENT_NAME>
                        <CLIENT_URL>NNN</CLIENT_URL>
                        <CLIENT_IMAGE>NNN</CLIENT_IMAGE>
                        <CLIENT_TEXT>NNN</CLIENT_TEXT>
                  </CLIENT>
                  
            </PORTFOLIO_PAGE>
      </PORTFOLIO_NAME>
      <PORTFOLIO_NAME id="Latest2">
            <PORTFOLIO_PAGE id="1">
                  <CLIENT>
                        <CLIENT_NAME>XXXXXX</CLIENT_NAME>
                        <CLIENT_URL>XXXXXX</CLIENT_URL>
                        <CLIENT_IMAGE>XXXXXX</CLIENT_IMAGE>
                        <CLIENT_TEXT>XXXXXX.</CLIENT_TEXT>
                  </CLIENT>
                  <CLIENT>
                        <CLIENT_NAME>YYYYYYYY</CLIENT_NAME>
                        <CLIENT_URL>YYYYYYYY</CLIENT_URL>
                        <CLIENT_IMAGE>YYYYYYYY</CLIENT_IMAGE>
                        <CLIENT_TEXT>YYYYYYYY</CLIENT_TEXT>
                  </CLIENT>
                  <CLIENT>
                        <CLIENT_NAME>ZZZZZZZZZ</CLIENT_NAME>
                        <CLIENT_URL>ZZZZZZZZZ</CLIENT_URL>
                        <CLIENT_IMAGE>ZZZZZZZZZ</CLIENT_IMAGE>
                        <CLIENT_TEXT>ZZZZZZZZZ</CLIENT_TEXT>
                  </CLIENT>
            </PORTFOLIO_PAGE>
      </PORTFOLIO_NAME>
      
</PORTFOLIO>

---------------
XSLT:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:template match="/PORTFOLIO_PAGE">

            
<xsl:for-each select="CLIENT">
      <xsl:value-of select="CLIENT_NAME"/>
</xsl:for-each>

</xsl:template>

</xsl:stylesheet>

---------------
Code:
xmlPath= "portfolio.xml"    'XML file path
      xslPath="transform.xslt"     'XSL file path

      Set xmlDoc = Server.CreateObject("msxml2.DOMDocument")
      Set xslDoc = Server.CreateObject("msxml2.DOMDocument")
      
      xmlDoc.async = False     'Allow the document to complete loading
      
      
      success = xmlDoc.Load(Server.MapPath(xmlPath))     'load the XML
      
      
      If (success) then     'file successfully loaded
            'set oNode = xmldoc.documentElement.SelectSingleNode("//PORTFOLIO_NAME[@id='Latest']").SelectSingleNode("PORTFOLIO_PAGE[@id=2]")
            'set oNode = xmldoc.SelectSingleNode("//PORTFOLIO_NAME[@id='Latest']/PORTFOLIO_PAGE[@id='1']")
            
            'Single Node with Params
            'strXsl.documentElement.selectSingleNode("//xsl:param[@name='sort_column']");

            set oNode = xmldoc.SelectSingleNode("//PORTFOLIO_NAME[@id='Latest']/PORTFOLIO_PAGE[@id='1']")
            response.Write oNode.xml & "<hr>"
            'response.End
            xslDoc.async = False
            success = xslDoc.Load(Server.MapPath(xslPath))     'load the XSL
                  
            If (success) then
            
                  Response.write oNode.transformNode(xslDoc)
            Else
                  Response.Write "Unable to load the stylesheet"
            End if
      Else
            Response.Write "Unable to load the XML source"
      End If

      Set xmlDoc = Nothing
      Set xslDoc = Nothing
0
lawso
Asked:
lawso
  • 3
  • 2
1 Solution
 
lawsoAuthor Commented:
Just to reiterate:
If I find 'Latest' and '2'
I get NNNNNNNNNNNN
0
 
Geert BormansCommented:
Hi lawso,
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
try this on the next line
<xsl:output indent="yes" />

Cheers!
0
 
Geert BormansCommented:
lawso,

if you want this to be html,
then you need tags
your code has a > in front of it
new lines don't

> <?xml version="1.0" encoding="UTF-8" ?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
> <xsl:template match="/PORTFOLIO_PAGE">
<html><body>
> <xsl:for-each select="CLIENT">
<p>
>      <xsl:value-of select="CLIENT_NAME"/>
</p>
> </xsl:for-each>
</body></html>
> </xsl:template>

> </xsl:stylesheet>
0
 
lawsoAuthor Commented:
Gertone... The function is called from inside HTML so it has the HTML around it.
I will give your other suggestion a try.
0
 
Geert BormansCommented:
well, it has the html around it,
but not in every repetion for CLIENT

If the entire stylesheet is called inside a <P> element,
then put a <BR /> in the repitition, like this

<xsl:for-each select="CLIENT">
   <xsl:value-of select="CLIENT_NAME"/> <BR />
</xsl:for-each>

if the stylesheet is called in a <body> element
keep my original example, without the html and body tags

<xsl:for-each select="CLIENT">
   <P><xsl:value-of select="CLIENT_NAME"/></P>
</xsl:for-each>

you will not get newlines in the browser because of newlines in the XML
you need paragraphs or <BR/> in the HTML

my other suggestion will work for XML tags only

cheers

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now