473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to output the all the ancestor and their attribute of an element

Assuming I have the following two xml file:

source.xml:

<AllFields>
<Group name="G1">
<Field fieldName="f1">Value1</Field>
<Field fieldName="f2">Value2</Field>
<Field fieldName="f3">Value3</Field>
</Group>
<Group name="G2">
<Field fieldName="f4">Value4</Field>
<Field fieldName="f5">Value5</Field>
<Field fieldName="f6">Value6</Field>
</Group>
</AllFields>

requirement.xml:

<RequiredFields>
<FieldName>f1</FieldName>
<FieldName>f3</FieldName>
<FieldName>f6</FieldName>
</RequiredFields>

The requirement.xml defines which field will go to the output XML. How
could I use XSLT to get the following output:

<AllFields>
<Group name="G1">
<Field fieldName="f1">Value1</Field>
<Field fieldName="f3">Value3</Field>
</Group>
<Group name="G2">
<Field fieldName="f6">Value6</Field>
</Group>
</AllFields>

Thanks your help in advance!

Jul 20 '05 #1
2 1091
Tempore 17:16:45, die Thursday 03 February 2005 AD, hinc in foro {comp.text.xml} scripsit <ai********@yahoo.com>:
Assuming I have the following two xml file:

source.xml:

<AllFields>
<Group name="G1">
<Field fieldName="f1">Value1</Field>
<Field fieldName="f2">Value2</Field>
<Field fieldName="f3">Value3</Field>
</Group>
<Group name="G2">
<Field fieldName="f4">Value4</Field>
<Field fieldName="f5">Value5</Field>
<Field fieldName="f6">Value6</Field>
</Group>
</AllFields>

requirement.xml:

<RequiredFields>
<FieldName>f1</FieldName>
<FieldName>f3</FieldName>
<FieldName>f6</FieldName>
</RequiredFields>

The requirement.xml defines which field will go to the output XML. How
could I use XSLT to get the following output:

<AllFields>
<Group name="G1">
<Field fieldName="f1">Value1</Field>
<Field fieldName="f3">Value3</Field>
</Group>
<Group name="G2">
<Field fieldName="f6">Value6</Field>
</Group>
</AllFields>

Hi,

You could use some stylesheet like this:

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

<xsl:template match="node()|@*">
<xsl:if test="not(self::Field) or @fieldName=document('requirement.xml')/*/FieldName">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
"Et ipsa scientia potestas est" - Francis Bacon , Meditationes sacrae
Jul 20 '05 #2
Thanks! it works!

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ognen Ivanovski | last post by:
Is the following constraint possible to be expressed with XML Schema (and how): Suppose we have a document in the form <car type="ford"> <part name="door"/> <part name="ashtray"/> </car>
1
by: Wolfgang | last post by:
XSLT transformations by default seem to pass name space attributes into the root element of their output (example below). QUESTION: Is it possible to control this, i.e. not genrating a name...
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
3
by: Mark | last post by:
Hello, What I am trying todo is use .NET serialization to take a Guid property that is an attribute in the XML document and have its output to be the same as...
1
by: Hubidubi | last post by:
Hi, I would like to generate XML (struts code) from XML with XSL transformation. I run into problem when I wanted to use tags with prefixes, like <html:text />. I included namespace...
3
by: 张韡武 | last post by:
Hello. I am a newbie trying to get my first XLST script working. I already know how to do this: Source XML: <category id="a0104"> <name>Oil and Gas</name> </category> XSLT: <xsl:for-each...
3
by: Valvalis | last post by:
Hello, I am trying to set a class attribute of a text.item element to the value of its nearest ancestor. I want to do this in the case that the class of the text.item is currently a blank string....
3
by: super.raddish | last post by:
Greetings, I am relatively new to, what I would call, advanced XSLT/XPath and I am after some advice from those in the know. I am attempting to figure out a mechanism within XSLT to compare the...
7
by: Ebenezer | last post by:
Hello! Let's suppose we have an XML with some nested NODE nodes: <root attr="first"> <node id="1" attr="mike"> <node id="2" /> <node id="3" attr="dave" /> </node> <node id="4">
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.