How can I using XSLT, select only some xml tags from my input xml to my output XML? example input:
<Country value="USA">
<State value="KY>
<City value="Hebron" />
<City value="Lexington" />
<City value="Owensboro" />
<City value="Jonesville" />
</State>
<State value="OH">
<City value="Cincinnati" />
<City value="Columbus" />
<City value="Cleveland" />
<City value="Jonesville" />
</State>
<State value="IN" >
<City value="Indianapolis" />
</State>
</Country>
So, keep the Country/State tags in place and only copy Hebron and Cincinnati?
expected output:
<Country value="USA">
<State value="KY>
<City value="Hebron" />
</State>
<State value="OH">
<City value="Cincinnati" />
</State>
</Country>
This will leave only specific cities:
This will leave only first city:
The following stylesheet:
On this input:
Produces the following result:
This stylesheet uses the identity transform to copy all but the undesired nodes to the output unchanged.
Another example
You might also want to remove any
State
element that does not have a desired city. This stylesheet:Applied to this input:
Produces:
Here's my (probably inadequate) 2.0 solution. Cities are a regular expression passed as a parameter.