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>
<Group name="G3">
<Field fieldName="f7"> Value7</Field>
<Field fieldName="f8"> Value8</Field>
<Field fieldName="f9"> Value9</Field>
</Group>
</AllFields>
required.xml:
<RequiredFields >
<FieldName>f1 </FieldName>
<FieldName>f3 </FieldName>
<FieldName>f6 </FieldName>
</RequiredFields>
The requirement.xml defines which field will go to the output XML.
The xsl file and output XML are like the following:
xsl file:
<xsl:template match="node()|@ *">
<xsl:if test="(not(self ::Field )or
@FieldName=docu ment('required .xml')/*/FieldName">
<xsl:copy>
<xsl:apply-templates select="node()| @*"/>
</xsl:copy>
</xsl:if>
</xsl:template>
output.xml
<AllFields>
<Group name="G1">
<Field fieldName="f1"> Value1</Field>
<Field fieldName="f3"> Value3</Field>
</Group>
<Group name="G2">
<Field fieldName="f6"> Value6</Field>
</Group>
<Group name="G3">
</Group>
</AllFields>
How could I use XSLT to get the following desired output(without all
the empty lines and the last Group element:
<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!
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>
<Group name="G3">
<Field fieldName="f7"> Value7</Field>
<Field fieldName="f8"> Value8</Field>
<Field fieldName="f9"> Value9</Field>
</Group>
</AllFields>
required.xml:
<RequiredFields >
<FieldName>f1 </FieldName>
<FieldName>f3 </FieldName>
<FieldName>f6 </FieldName>
</RequiredFields>
The requirement.xml defines which field will go to the output XML.
The xsl file and output XML are like the following:
xsl file:
<xsl:template match="node()|@ *">
<xsl:if test="(not(self ::Field )or
@FieldName=docu ment('required .xml')/*/FieldName">
<xsl:copy>
<xsl:apply-templates select="node()| @*"/>
</xsl:copy>
</xsl:if>
</xsl:template>
output.xml
<AllFields>
<Group name="G1">
<Field fieldName="f1"> Value1</Field>
<Field fieldName="f3"> Value3</Field>
</Group>
<Group name="G2">
<Field fieldName="f6"> Value6</Field>
</Group>
<Group name="G3">
</Group>
</AllFields>
How could I use XSLT to get the following desired output(without all
the empty lines and the last Group element:
<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!
Comment