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 12th, 2010, 05:58 AM
Authorized User
 
Join Date: Oct 2010
Posts: 25
Thanks: 8
Thanked 0 Times in 0 Posts
Default Searching a XML file for a element

Hello,

I have a XML document that uses references, this is a section of the file just to give the idea
Code:
<?xml version="1.0" encoding="UTF-8"?>
<doc:iso_10303_28 xmlns:exp="urn:oid:1.0.10303.28.2.1.1" xmlns:doc="urn:oid:1.0.10303.28.2.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oid:1.0.10303.28.2.1.1 ex.xsd" version="2.0">
	<exp:uos id="uos_1" description="" configuration="default" edo="">
	    		<Document id="i172">
			<Id>DOC40</Id>
			<Revision>-</Revision>
			<Description>LUBRICAT</Description>
			<Eco>
				<Effectivity id="i173">
					<Effdate></Effdate>
					<Changenum></Changenum>
				</Effectivity>
			</Eco>
			<Oda>81349</Oda>
			<Org></Org>
		</Document>
		<Document id="i174">
			<Id>DOC41</Id>
			<Revision>S1</Revision>
			<Description>INSTALLA</Description>
			<Eco>
				<Effectivity id="i175">
					<Effdate></Effdate>
					<Changenum></Changenum>
				</Effectivity>
			</Eco>
			<Oda>07482</Oda>
			<Org></Org>
		</Document>		
		<Part id="i169">
			<Id>PART39</Id>
			<Marking>Serial Number</Marking>
			<Vse>Not Required</Vse>
			<Description>BASE</Description>
			<Eco>
				<Effectivity id="i170">
					<Effdate>2010-06-07 16:36:16.0</Effdate>
					<Changenum>949703</Changenum>
				</Effectivity>
			</Eco>
			<Oda>07482</Oda>
			<Org>G3</Org>
			<Applicable_documents id="i171" exp:cType="set">
				<Document xsi:nil="true" ref="i172"/>
				<Document xsi:nil="true" ref="i174"/>							
			</Applicable_documents>
		</Part>
		<Part id="i10992">
			<Id>PART3943</Id>
			<Marking>None</Marking>
			<Vse>Not Required</Vse>
			<Description>PIN</Description>
			<Eco>
				<Effectivity id="i10993">
					<Effdate></Effdate>
					<Changenum></Changenum>
				</Effectivity>
			</Eco>
			<Oda></Oda>
			<Org></Org>
		</Part>
		<Part id="i10994">
			<Id>PART3944</Id>
			<Marking>None</Marking>
			<Vse>Not Required</Vse>
			<Description>BOLT</Description>
			<Eco>
				<Effectivity id="i10995">
					<Effdate></Effdate>
					<Changenum></Changenum>
				</Effectivity>
			</Eco>
			<Oda>96906</Oda>
			<Org>W1</Org>
		</Part>
		<Assembly id="i11069">
			<Part>
				<Part xsi:nil="true" ref="i169"/>
			</Part>
			<Id>PART39</Id>
			<Find>0010C</Find>
			<Quantity>0</Quantity>
			<Usage>X</Usage>
			<Level>0</Level>
		</Assembly>
		<Ge_part210 id="i15621">
			<Document_list id="i15623" exp:cType="set">
				<Document xsi:nil="true" ref="i172"/>
				<Document xsi:nil="true" ref="i174"/>
			</Document_list>
			<Part_list id="i15622" exp:cType="set">
				<Part xsi:nil="true" ref="i10992"/>
				<Part xsi:nil="true" ref="i10994"/>
			</Part_list>
			<Part_structure id="i15624" exp:cType="set">
				<Assembly xsi:nil="true" ref="i11069"/>
			</Part_structure>
		</Ge_part210>
	</exp:uos>
</doc:iso_10303_28>
I want read Ge_part210/Document_list/Document get its ref and then search for a Document with a matching ref. The chosen element must the Document element and not the referenced Document within the Part/Applicable_documents node.
so I have the following
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:fn="http://www.w3.org/2005/xpath-functions"
	xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
	xmlns:err="http://www.w3.org/2005/xqt-errors"
	exclude-result-prefixes="xs xdt err fn">

	<xsl:output method="xml" indent="yes"/>
	
	<xsl:template match="/">		
		<xsl:apply-templates/>
	</xsl:template>
        <xsl:template match="Ge_part210">
             <xsl:element name="GE_PART210">
                         <xsl:apply-templates/>
             </xsl:element>
        </xsl:template>
        <xsl:template match="Document_list">
             <xsl:element name="document-list">
                   <xsl:apply-templates/>
             </xsl:element>
        </xsl:template>
         <xsl:template match="Document">
                <xsl:call-template name="get_document">
                            <xsl:with-param name="ref" select="@ref" />
                 </xsl:call-template>
         </xsl:template>
        <xsl:template name="get_document">
                <xsl:param name="ref"/>
                 <xsl:if test="id = @ref" >
                      <xsl:copy-of select="."/>
                           </xsl:if>
          </xsl:template>
</xsl:stylesheet>
I'm getting this
Code:
<?xml version="1.0" encoding="UTF-8"?>
	
			PART3943
			None
			Not Required
			PIN
			
			PART3944
			None
			Not Required
			BOLT
			
			96906
			W1
		
			PART39
			0010C
			0
			X
			0
		
		<GE_PART210>
			<document-list>
								
			</document-list>
		</GE_PART210>
what am I missing here,am I using the param and if correctly?

cheers,
Ehsan

Last edited by ehsansad; November 12th, 2010 at 06:08 AM..
 
Old November 12th, 2010, 07:55 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The easiest way to do a lookup is to use the xsl:key instruction, with the key() xpath function.

Your use of xsl:call-template is wrong, as when you call it you are passing in the current context, so when you are doing you <xsl:if> you are still in the Document element inside the GP_Part. Therefore there is no @id attribute.

Code:
   <xsl:key name="documents" match="Document" use="@id"/>

  <xsl:template match="/">
        <xsl:apply-templates select="//Ge_part210" />
  </xsl:template>
  
  <xsl:template match="Ge_part210">
  	<GE_PART210>
	  		<document-list>
		  			<xsl:apply-templates select="//Document"/>
	  			</document-list>
  		</GE_PART210>
  </xsl:template>
  
    <xsl:template match="Document">
	    	<xsl:copy-of select="key('documents',@ref)" copy-namespaces="no"/>
    </xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
ehsansad (November 12th, 2010)
 
Old November 12th, 2010, 08:14 AM
Authorized User
 
Join Date: Oct 2010
Posts: 25
Thanks: 8
Thanked 0 Times in 0 Posts
Default

it always amazes me how simply you solve my very complicated problems.
thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching for body element in an html not working aldwinenriquez XML 8 August 28th, 2008 05:56 AM
Getting value of an element in an XML file semilemon C# 2005 5 April 13th, 2007 07:54 AM
Converting XML to XML (making element mandatory) boondocksaint20 XSLT 8 April 28th, 2006 10:54 AM
Adding an Element with Attribute to XML file xergic Classic ASP XML 0 November 20th, 2004 08:26 AM
How to insert a new element in XML file? kmriyad C# 1 September 20th, 2004 09:18 AM





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