Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 16th, 2006, 11:08 AM
Authorized User
 
Join Date: Jul 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default Child Help.

Hey guys trying to figure this out. wondering if anyone could help.

  <make desc="Assembled Vehicle" value="ASVE">
      <assembled />
  </make>


I need to match on if assembled element is there. When I find it I will output the make desc and value.

Here is what I come up with so far:

 <xsl:if test="@node::child() = 'aircraft'">
      I know you can't do this, but just wondering if I am on the right track?

Thanks again.

 
Old November 16th, 2006, 11:09 AM
Authorized User
 
Join Date: Jul 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sorry i meant....

<xsl:if test="@node::child() = 'assembled'">

 
Old November 16th, 2006, 11:17 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<xsl:template match="make">
 <xsl:if test="assembled">
  <xsl:value-of select="@desc"/>
 </xsl:if>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 16th, 2006, 02:47 PM
Authorized User
 
Join Date: Jul 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael, sorry to bother you again but I am having a tough time figuring this out. I have the following and trying to check make and if it has a value of aircraft in addition if aircraft has children of code.

<make desc="Chrysler" value="CHRY">
        <aircraft/>
 </make>
 <make desc="test">
        <aircraft>
                 <code desc="Jet Propelled" value="JET"/>
         </aircraft>
  </make>


This is what I have:
 <xsl:if test="child::node() = aircraft and ??>

it is after the and that I am struggling here is where I need to see if aircraft has any 'code' children, I just have to know if they are present.

Here is what I was guessing but i guess you can't do it:
child::node(aircraft)

P.S. Michael I have purchased your book off of amazon.com

Thanks again sorry to be a bother.

 
Old November 16th, 2006, 02:57 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

As I said before, you want

<xsl:if test="aircraft">

That's short for

<xsl:if test="boolean(child::aircraft)">

that is, it selects the children called "aicraft" to produce a node-set, and converts this node-set to a boolean; the result is true if the node-set is not empty.

Your code test="child::node() = aircraft" doesn't work because "=" tests the value of the node (that is its content) rather than its name: more strictly, the string-value of the node, which in the case of an element is the concatenation of all the text nodes within it, which in your case is a chunk of whitespace.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 20th, 2006, 06:04 PM
Authorized User
 
Join Date: Jul 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello again, I am trying a new solution: below is the code that my xml that my style sheet parses(sorry if it is long) and my style sheet code is below that...I am trying to sort on @value. I have also included my output at the end. Thanks again...sorry, but i am still a newbie at all of this:

*XML*

<?xml version="1.0" encoding="UTF-8"?>
<vehicleCodes module="MESSENGER" name="VEHICLE_CODES" type="spclCodes" version="CPI000715">
  <makes sort="n">
    <generic sort="y">
      <makeEdit desc="Aircraft" editEnd="" editStart="5" value="AERO">
        <aircraft/>
        </makeEdit>
        <makeEdit desc="Aircraft" editStart="5" value="AERO Enter Make Here">
          <aircraft/>
          </makeEdit>
          <make desc="Assembled Vehicle" value="ASVE">
            <assembled/>
          </make>
          </generic>
          <specific sort="y">
      <make desc="AAA Mobile Home Mfg. Co." value="AAA">
        <trailer/>
      </make>
      </specific>
      <military sort="y">
        <make desc="US Army" value="USA">
          <aircraft/>
          <auto/>
          <construction/>
          <motorcycle/>
          <snowmobile/>
          <special/>
          <trailer/>
          <truck/>
        </make>
        <make desc="US Air Force" value="USAF">
          <aircraft/>
          <auto/>
          <construction/>
          <motorcycle/>
          <snowmobile/>
          <special/>
          <trailer/>
          <truck/>
        </make>
        <make desc="US Coast Guard" value="USCG">
          <aircraft/>
          <auto/>
          <construction/>
          <motorcycle/>
          <snowmobile/>
          <special/>
          <trailer/>
          <truck/>
        </make>
        <make desc="US Marine Corps" value="USMC">
          <aircraft/>
          <auto/>
          <construction/>
          <motorcycle/>
          <snowmobile/>
          <special/>
          <trailer/>
          <truck/>
        </make>
        <make desc="US Navy" value="USN">
          <aircraft/>
          <auto/>
          <construction/>
          <motorcycle/>
          <snowmobile/>
          <special/>
          <trailer/>
          <truck/>
        </make>
        </military>
        <guard sort="y">
          <guardMake desc="Air National Guard" value="AG">
            <aircraft/>
            <auto/>
            <construction/>
            <motorcycle/>
            <snowmobile/>
            <special/>
            <trailer/>
            <truck/>
            </guardMake>
            <guardMake desc="Civil Air Patrol" value="AP">
              <aircraft/>
              <auto/>
              <construction/>
              <motorcycle/>
              <snowmobile/>
              <special/>
              <trailer/>
              <truck/>
              </guardMake>
              <guardMake desc="National Guard" value="NG">
                <aircraft/>
                <auto/>
                <construction/>
                <motorcycle/>
                <snowmobile/>
                <special/>
                <trailer/>
                <truck/>
                </guardMake>
        </guard>
  </makes>
</vehicleCodes>

*stylesheet*

<?xml version='1.0'?>
  <!DOCTYPE xsl:stylesheet [
  <!ENTITY lf "#x0a;">
  <!ENTITY nbsp "#160;">
  ]>
  <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
   <xsl:output method='html' indent='no' omit-xml-declaration='yes'/>

   <xsl:param name="Sort" ><xsl:value-of select="'Code'"/></xsl:param>

   <xsl:template match="@*|node()">
    <xsl:apply-templates />
    <xsl:for-each select="aircraft">
     <xsl:sort select="../@value" />
     <xsl:value-of select="'#x0a;'"/>
     <xsl:value-of select="../@value" />
     </xsl:for-each>

   </xsl:template>
  </xsl:stylesheet>

*output*
AERO
AERO Enter Make Here
USA
USAF
USCG
USMC
USN
AG
AP
NG

Desired would be:
AERO
AERO Enter Make Here
AG
AP
NG
USA
USAF
USCG
USMC
USN

 
Old November 20th, 2006, 06:53 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Let's look at your code and see what it means.

<xsl:template match="@*|node()">

This is processing any element or attribute (or text node, etc) that you apply-templates to. You never apply templates to an attribute node, so why have a template rule that matches one?

    <xsl:apply-templates />

If you're processing an element, this causes all the children to be processed.

    <xsl:for-each select="aircraft">

If you're processing an element with aircraft children, this causes the aircraft children to be processed a second time.

     <xsl:sort select="../@value" />

You want the aircraft children to be sorted. But in your data, there is never more than one aircraft child, so sorting them has no effect. And even if there was more than one aircraft, the expression ../@value would return the same result for each one, so the sort would be meaningless.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Only first child displayed? gabster XSLT 6 September 27th, 2007 03:14 AM
checking for a child? gabster XSLT 2 March 7th, 2007 02:18 PM
Opening MDI Child on Button Click of MDI Child Sachin Shinde Visual Studio 2005 0 June 8th, 2006 05:05 AM
child element kgoldvas XML 4 April 27th, 2006 01:51 AM
insert child rajanikrishna XML 3 October 5th, 2004 11:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.