Jump to content

How to avoid the unwanted data from the XML file I read from ?


dbdb

Recommended Posts

HiI'd like to get only the data by this path : Report/Chapter/ChapterContent/Part/PartContent/CompanyDetails....I can get to data I want but I can't discard the data in tags <Report> or <code> or <OrderID> ... for example...when I run the page it throw me on screen also the data from the tags above and others too that I don't want .How to fix this ?please :) /// the xslt file ////

<xsl:template match="ChapterContent">	<html xmlns="http://www.w3.org/1999/xhtml">	  <head>		<link href="css/company.css" rel="stylesheet" type="text/css" />	  </head>	  <body>		<div align="center">		  <table dir="rtl" class="company" width="90%" cellspacing="0" cellpadding="0">			<tr>			  <td width="45%" valign="top">				<table width="100%">				  <tr>					<td width="30%"></td>					<td></td>				  </tr>				  				  <xsl:for-each select="ChapterContent/PartContent">					<xsl:apply-templates select="PartContent" />				  </xsl:for-each>				  				  <xsl:apply-templates/>				</table>			  </td>  </tr>		  </table>		</div>	  </body>	</html>  </xsl:template>  <xsl:template match="CompanyDetails">	<xsl:apply-templates select="CompanyNames"/>	<xsl:apply-templates select="AkaName"/>//////etc...

///the XML file - the tree map is like the path I mentioned above///

<Report>  <Code>67</Code>  <ID>Company Details Report Monitored company WS									 </ID>  <DisplayName>report XYZ											 </DisplayName>  <UpdateDate />  <Chapter>	<OrderID>1221298</OrderID>	<Code>2</Code>	<ID>BusinessDetails</ID>	<DisplayName>business data</DisplayName>	<UpdateDate>16/09/2008</UpdateDate>	<ChapterContent>	  <Part>		<Code>1</Code>		<ID>CompanyDetails</ID>		<DisplayName>details</DisplayName>		<UpdateDate>05/10/2006</UpdateDate>		<PartContent>		  <CompanyDetails>			<CompanyNames>			  <Name>AVI DESIGNS</Name>			  <EnglishName>SOFFER AVI DESIGNS LTD.</EnglishName>			  <AkaName />...

Link to comment
Share on other sites

You need to specify a default template that will handle unknown elements, or better yet, one that will handle text nodes.Add into your stylesheet

<xsl:template match="text()" />

And then to every other element, make sure you use xsl:value-of instead of xsl:apply-templates when you do mean to output plain text nodes.BTW, instead of:

				  <xsl:for-each select="ChapterContent/PartContent">					<xsl:apply-templates select="PartContent" />				  </xsl:for-each>

I suggest you simply use:

<xsl:apply-templates select="ChapterContent/PartContent" />

Link to comment
Share on other sites

You need to specify a default template that will handle unknown elements, or better yet, one that will handle text nodes.Add into your stylesheet
<xsl:template match="text()" />

And then to every other element, make sure you use xsl:value-of instead of xsl:apply-templates when you do mean to output plain text nodes.BTW, instead of:

				  <xsl:for-each select="ChapterContent/PartContent">					<xsl:apply-templates select="PartContent" />				  </xsl:for-each>

I suggest you simply use:

<xsl:apply-templates select="ChapterContent/PartContent" />

:) thank you !!!I do have another question to ask you:In my xml file (like above) - their are multiple unit elements and each has the same structure.E.g : the <Part> data is different now (refer to starting topic - XML code) and I need to represent on screen the data from <LegalStatus> only.Now, I know that I can take to my advantage the ID from <ID> and compare them but I don't know how to do thatMy question is how can I approach to the 'next' branch of <PartContent> to read from <LegalStatus> ?when I arleady have <PartContent> which I read from
<Part>		<Code>35</Code>		<ID>LegalStatus</ID>		<DisplayName>legal_status</DisplayName>		<UpdateDate>05/10/2006</UpdateDate>		<PartContent>		  <LegalStatus>			<LegalStatus>			  <EntityCode>1</EntityCode>			  <EntityCodeDescription>private firm</EntityCodeDescription>			  <LegalStatusCode>0</LegalStatusCode>			  <LegalStatusDescription>active</LegalStatusDescription>			  <EstablishYear>1985</EstablishYear>			  <Regis>51-106817-3</Regis>			  <EstablishMonth>5</EstablishMonth>			  <StartOperatedYear>0</StartOperatedYear>			</LegalStatus>		  </LegalStatus>		</PartContent>	  </Part>

Link to comment
Share on other sites

I'm not sure I understand your question correctly.You need to apply a template about an element who's name is in the ID element? How about:

<xsl:variable name="ID" select="ID" /><xsl:apply-templates select="//*[name() = $ID]" />

If that's not it, can you give a little more concrete example? One where the XML can be used "as is" and contains enough <Part> elements (3 will do) to show both failures and successes.

Link to comment
Share on other sites

I'm not sure I understand your question correctly.You need to apply a template about an element who's name is in the ID element? How about:
<xsl:variable name="ID" select="ID" /><xsl:apply-templates select="//*[name() = $ID]" />

If that's not it, can you give a little more concrete example? One where the XML can be used "as is" and contains enough <Part> elements (3 will do) to show both failures and successes.

The thing is that I need to go through multiple node from the same element.in the XML file I need to process this path for all of my xslt : Report/Chapter/ChapterContent/Part/PartContentnow I apply-templates for the 1st <PartContent> , but now I need to apply-templates for the 2nd <PartContent> details.
<xsl:template match="text()" />  <xsl:template match="/">	<html xmlns="http://www.w3.org/1999/xhtml">	  <head>		<link href="css/company.css" rel="stylesheet" type="text/css" />	  </head>	  <body>		<div align="center">		  <table dir="rtl" class="company" width="90%" cellspacing="0" cellpadding="0">			<tr>			  <td width="45%" valign="top">				<table width="100%">				  <tr>					<td width="30%"></td>					<td></td>				  </tr>				  				  <xsl:apply-templates select="Chapter/ChapterContent/Part" />				  				  <xsl:apply-templates/>........etc...</xsl:template><xsl:template match="//CompanyDetails">	<xsl:apply-templates select="CompanyNames"/></xsl:template>  <xsl:template match="/Part[ID='LegalStatus'][1]">  //  <--- here is what I don't know what to do 	<xsl:apply-templates select="/PartContent/LegalStatus/LegalStatus/LegalStatus/EntityCodeDescription"/>

Link to comment
Share on other sites

You can access the siblings following a node with the "following-sibling" axis.For example, assuming you're currently at the first Part node, selecting

following-sibling::Part[1]

will match the next Part node.Template matches can't use axes AFAIK. You can however use then when you apply-templates.

Link to comment
Share on other sites

Replace the last template in your sample with something like:

 <xsl:template match="Part">	<xsl:apply-templates select="following-sibling::Part[1]"/><!-- This will apply the same template for the upcoming Part. Note that this means that the template will be executed the number of times the position of the element, i.e. the first Part will be processed once, the second two times, the third three and so on -->	<xsl:apply-templates select="/PartContent/LegalStatus/LegalStatus/LegalStatus/EntityCodeDescription"/>

Link to comment
Share on other sites

it doesn't work, I probably missing something.If I write <xsl:template match="Part"> then it don't show anything (not even things from templates before)mybe I need a change in the <body> in the stylesheet ? or what ?thanks

Link to comment
Share on other sites

Strange. I'd have though that with the apply-templates in your root template, you'd match any other template, including this one.Well... try to match them manually. In your root template, change the last apply-tempates to

<xsl:apply-templates select="//Part" />

(feel free to be more specific, like "/Part"... I'm just taking the least specific way so as to make sure the template gets executed at least once)Also, make sure you're actually doing something once the Part is matched. For the sake of testing, add into the Part template some plain text (ANY text). If the template is ever matched, you'll see it.

Link to comment
Share on other sites

No good.Just to clearify: I wrote

<xsl:template match="Part" >	<xsl:apply-templates select="following-sibling::Part[1]"/>	<xsl:apply-templates select="//PartContent/LegalStatus/LegalStatus/LegalStatus/EntityCodeDescription"/>..etc..

I also have

<?xml version="1.0" encoding="utf-8" ?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">  <xsl:output method="html" encoding="windows-1255"/>  <!--<xsl:variable name="nXcoord" select="dnb:company/dnb:xcoord" />  <xsl:variable name="nYcoord" select="dnb:company/dnb:ycoord" />  <xsl:variable name="sTown" select="translate(dnb:company/dnb:town, '"', '')" />-->  <xsl:template match="text()" />  <xsl:template match="/">	<html xmlns="http://www.w3.org/1999/xhtml">	  <head>		<link href="css/company.css" rel="stylesheet" type="text/css" />	  </head>	  <body>		<div align="center">		  <table dir="rtl" class="company" width="90%" cellspacing="0" cellpadding="0">			<tr>			  <td width="45%" valign="top">				<table width="100%">				  <tr>					<td width="30%"></td>					<td></td>				  </tr>				  				  <xsl:apply-templates select="//ChapterContent/Part/PartContent" />				  				  <xsl:apply-templates/>  etc..

Now , it still shows only the first <Part> elements. remember I'm asking to approach again to part[1]appreciate your effort

Link to comment
Share on other sites

I'm in a dead end.I'll have to see the full XSLT to further debug this. And as I said before, a large enough (but still full featured) sample XML. I mean, is Part really your root element? I'm guessing not.

Link to comment
Share on other sites

I debug it with XMLspy and noticed a few things that after <xsl:apply-templates select="following-sibling::Part[2]"/>it goes right back to <xsl:template match="Part" > before as it were a loop, and it jump the pointer to another Part. any idea ?the almost Full XML is:

<?xml version="1.0" encoding="utf-8" ?><Report>  <Code>67</Code>  <ID>Company Details Report Monitored company WS									 </ID>  <DisplayName>דו\"ח פרטי חברה במעקב - שרות רשת												 </DisplayName>  <UpdateDate />  <Chapter>	<OrderID>1221298</OrderID>	<Code>2</Code>	<ID>BusinessDetails</ID>	<DisplayName>נתונים עסקיים</DisplayName>	<UpdateDate>16/09/2008</UpdateDate>	<ChapterContent>	  <Part>		<Code>1</Code>		<ID>CompanyDetails</ID>		<DisplayName>פרטי זיהוי והתקשרות</DisplayName>		<UpdateDate>05/10/2006</UpdateDate>		<PartContent>		  <CompanyDetails>			<CompanyNames>			  <Name>אבי סופר עיצוב בע\"מ</Name>			  <EnglishName>SOFFER AVI DESIGNS LTD.</EnglishName>			  <AkaName />			  <AkaEnglishName>AVI SOFFER DESIGNS LTD.</AkaEnglishName>			  <AdditionalName />			  <AdditionalEnglishName />			</CompanyNames>			<Regis>51-106817-3</Regis>			<Duns>600027866</Duns>			<Phones>			  <Phone>				<Type>Phone</Type>				<PhoneNumber>03-7527111</PhoneNumber>			  </Phone>			  <Phone>				<Type>Fax</Type>				<PhoneNumber>03-7527115</PhoneNumber>			  </Phone>			</Phones>			<Address>			  <City>רמת גן</City>			  <Street>שד` החרושת 6</Street>			  <StreetNumber />			  <ZIP>52568</ZIP>			  <POB />			  <POBCity />			  <POB_Zip />			  <X_Coordinate>183023</X_Coordinate>			  <Y_Coordinate>666509</Y_Coordinate>			</Address>			<WebAddress>			  <Url>www.avisoffer.com</Url>			  <Email />			</WebAddress>		  </CompanyDetails>		</PartContent>	  </Part>	  <Part>		<Code>3</Code>		<ID>Sic</ID>		<DisplayName>תחום עיסוק</DisplayName>		<UpdateDate>05/10/2006</UpdateDate>		<PartContent>		  <Sic>			<Sics>			  <Sic>				<ID>39110000  </ID>				<Desc>יצור: תכשיטים, מתכות יקרות</Desc>			  </Sic>			  <Sic>				<ID>39140107  </ID>				<Desc>צורפות כסף</Desc>			  </Sic>			</Sics>		  </Sic>		</PartContent>	  </Part>	  <Part>		<Code>35</Code>		<ID>LegalStatus</ID>		<DisplayName>מעמד משפטי</DisplayName>		<UpdateDate>05/10/2006</UpdateDate>		<PartContent>		  <LegalStatus>			<LegalStatus>			  <EntityCode>1</EntityCode>			  <EntityCodeDescription>חברה פרטית בע\"מ</EntityCodeDescription>			  <LegalStatusCode>0</LegalStatusCode>			  <LegalStatusDescription>פעילה</LegalStatusDescription>			  <EstablishYear>1985</EstablishYear>			  <Regis>51-106817-3</Regis>			  <EstablishMonth>5</EstablishMonth>			  <StartOperatedYear>0</StartOperatedYear>			</LegalStatus>		  </LegalStatus>		</PartContent>	  </Part></ChapterContent>  </Chapter>  <Chapter>	<OrderID>1221298</OrderID>	<Code>29</Code>	<ID>Income</ID>	<DisplayName>הכנסות</DisplayName>	<UpdateDate>16/09/2008</UpdateDate>	<ChapterContent>	  <Part>		<Code>29</Code>		<ID>FinancingDetails</ID>		<DisplayName>הקפי פעילות</DisplayName>		<UpdateDate>16/09/2008</UpdateDate>		<PartContent>		  <FinancingDetails>			<FinancingDetailsItem>...etc...

the stylesheet:

<?xml version="1.0" encoding="utf-8" ?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">  <xsl:output method="html" encoding="windows-1255"/>  <!--<xsl:variable name="nXcoord" select="dnb:company/dnb:xcoord" />  <xsl:variable name="nYcoord" select="dnb:company/dnb:ycoord" />  <xsl:variable name="sTown" select="translate(dnb:company/dnb:town, '"', '')" />-->  <xsl:template match="text()" />  <xsl:template match="/">	<html xmlns="http://www.w3.org/1999/xhtml">	  <head>		<link href="css/company.css" rel="stylesheet" type="text/css" />	  </head>	  <body>		<div align="center">		  <table dir="rtl" class="company" width="90%" cellspacing="0" cellpadding="0">			<tr>			  <td width="45%" valign="top">				<table width="100%">				  <tr>					<td width="30%"></td>					<td></td>				  </tr>				  				  <xsl:apply-templates select="//ChapterContent/Part/PartContent" />				  				  <xsl:apply-templates/>				</table>			  </td>		  </tr>		  </table>		</div>	  </body>	</html>  </xsl:template>  <xsl:template match="//CompanyDetails">	<xsl:apply-templates select="CompanyNames"/>	....  </xsl:template>  <xsl:template match="Part" >	<xsl:apply-templates select="following-sibling::Part[2]"/>	<xsl:apply-templates select="//PartContent/LegalStatus/LegalStatus/LegalStatus/EntityCodeDescription"/>   	....  </xsl:template>//I jumped to this relevent section <xsl:template match="/PartContent/LegalStatus/LegalStatus/EntityCodeDescription">	<xsl:if test="EntityCodeDescription!=''">	  <tr>		<td class="caption">מעמד משפטי</td>		<td class="value">		  <xsl:value-of select="." />		</td>	  </tr>	</xsl:if>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...