abc123
abc123

Reputation: 8303

Using XSLT and XPath, get the value of an XML node or element?

I know this is super noob, but I just don't understand why my code is not working. Here is XML snippet:

<root>
  <cookies>
    <lastviewedentityname>Category</lastviewedentityname>
    <lastviewedentityinstanceid>72</lastviewedentityinstanceid>
    <lastviewedentityinstancename>Fall Florals</lastviewedentityinstancename>
    some random text bla bla
  </cookies>
  <QueryString>
    <categoryid>34</categoryid>
  </QueryString>
  <!-- other nodes -->
</root>

Here is the XSL snippet:

<?xml version="1.0" standalone="yes" ?>
<package version="2.1" displayname="Categories" debug="false" includeentityhelper="true">
  <PackageTransform>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ise="urn:ise" exclude-result-prefixes="ise">
        <xsl:output method="html" omit-xml-declaration="yes"/>

            <!-- other code -->

            <xsl:value-of select="/root/cookies/lastviewedentityinstanceid"/>

            <!-- other code -->

    </xsl:stylesheet>
  </PackageTransform>
</package>

The result I'm looking for is

72

But it seems I'm getting empty string or something. Looking at this: http://www.w3schools.com/xsl/xsl_value_of.asp, I'm assuming my code is ok. Also, confirming this, is that this code:

<xsl:value-of select="/root/QueryString/categoryid"/>

Gives me the correct value

34

Then in my trying to find answers, I found this site: http://www.mizar.dk/XPath/Default.aspx and I notice when I try here, it highlights the element, not the value inside of it: http://screencast.com/t/NXuNiCHbEd0T

Here is some more info: The system I'm working on is called Interprise Suite Ecommerce (this is where "ise" comes from). It is their web software which integrates with their ERP software called Interprise Suite. ISE an eCommerce software that was adapted from AspDotNetStoreFront some time ago. I'll try to explain the limited knowledge I have.

The page that is being loaded is a product or category page. This particular file I'm working on provides the layout for the sidebar product navigation menu. The file is called rev.categories.xml.config. THis file contains XSLT code. The original XML data that it is transforming, I don't know where it comes from and I don't have access to it. With Dimitre's help in a previous question, I was able to reproduce the XML data in the web pages so I could understand a bit more. In the output of that, the top level node was

<root>

The file that loads this XML package is called template.ascx. It is a template or "master" file. In ASPDNSF, you can use these things called "tokens" to load things into the page and this token is what is calling rev.categories.xml.config to activate and do its thing. The token looks like this:

(!XmlPackage Name="rev.categories"!)

I'm not sure how the tokens work. My thoughts were that it wasn't relevant because everything else in this file is working except for this one single line of code.

Pardon my lack of knowledge, experience and appropriate language.

Edit: Here is a link to the full code of rev.categories.xml.confg. http://jsfiddle.net/v5cNM/

Upvotes: 2

Views: 2561

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243549

Very few if any (I am not aware of such) XSLT processors support embedded stylesheets.

The correct and universally supported way to apply a transformation on an XML document is to place the XSLT stylesheet in a separate XML document (typically residing in its own file).

<xsl:stylesheet> (or its synonym xsl:transform) must be the top element of the stylesheet.

This transformation (occupying its own file):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:value-of select=
  "/root/cookies/lastviewedentityinstanceid"/>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<root>
  <cookies>
    <lastviewedentityname>Category</lastviewedentityname>
    <lastviewedentityinstanceid>72</lastviewedentityinstanceid>
    <lastviewedentityinstancename>Fall Florals</lastviewedentityinstancename>
    some random text bla bla
  </cookies>
  <!-- other nodes -->
</root>

produces the wanted, correct result:

72

Upvotes: 1

Related Questions