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

A

ai2003lian

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!
 
J

Joris Gillis

Tempore 17:16:45 said:
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:eek:utput 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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top