Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > How to output the all the ancestor and their attribute of an element

Reply
Thread Tools

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

 
 
ai2003lian@yahoo.com
Guest
Posts: n/a
 
      02-03-2005
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!

 
Reply With Quote
 
 
 
 
Joris Gillis
Guest
Posts: n/a
 
      02-03-2005
Tempore 17:16:45, die Thursday 03 February 2005 AD, hinc in foro {comp.text.xml} scripsit <(E-Mail Removed)>:

> 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 xmlnssl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xslutput 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
 
Reply With Quote
 
 
 
 
ai2003lian@yahoo.com
Guest
Posts: n/a
 
      02-03-2005
Thanks! it works!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
obtain element name, or attribute and value of the document name itself, and some elemnts and attributes from an ancestor or the node itself using xquery Jeff Kish XML 4 10-30-2008 05:47 PM
[XSL] Obtaining an attribute from self or ancestor Ebenezer XML 7 10-25-2008 10:00 PM
Get an element's ancestor node based on it's id? harryajh Javascript 4 02-01-2008 12:06 AM
How to run _init for all ancestor classes in diamond inheritance Amir Karger Perl Misc 9 12-10-2003 03:59 PM
XML Schema keys, uniqueness based on ancestor's attribute Ognen Ivanovski XML 0 07-15-2003 02:36 PM



Advertisments