Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filtering an XML doc with xsl:copy 1

Status
Not open for further replies.

jf73

Programmer
Apr 2, 2009
2
CA
Hi!,
I have this sample xml document :
<?xml version="1.0" ?>
<?xml-stylesheet href="TestFile.xsl" type="text/xsl" ?>
<CustList>
<Custb x="x">
<b>Test</b>
<Custa>z</Custa>
</Custb>
<z x="x">
<Custa>z</Custa>
</z>
</CustList>

I need to create a new document containing only element with a name like "Cust*". I think the best way to do this is with xml:copy but it does'nt seem to work. The result of this transformation should be :
<?xml version="1.0" encoding="ISO-8859-1"?>
<CustList>
<Custb x="x">
<Custa>z</Custa>
</Custb>
</CustList>

Do you have any idea ?
Thanks!
Jeff
 
>I need to create a new document containing only element with a name like "Cust*".
What happens with the Custa under z? What do you mean by that statement?

>I think the best way to do this is with xml:copy but it does'nt seem to work.
What have you tried so far?
 
This is what I've tried. I'm new to XML and XSLT but it seems that the "*[contains(name(), 'Cust')]" does not work because it returns the complete xml file content without filtering.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl=" version="1.0">
<xsl:eek:utput method="xml" version="1.0" indent="yes" encoding="ISO-8859-1"/>
<xsl:template match="*">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="/">
<xsl:copy>
<xsl:apply-templates select="*[contains(name(), 'Cust')]"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
 
I can see roughly what you mean. You have to get it done with these three templates or any variant of them. (I won't point out the differences, you discover them for yourself.)
[tt]
<xsl:template match="*">
[blue]<xsl:copy>
<xsl:apply-templates select="*[contains(name(), 'Cust')]|@*|text()" />
</xsl:copy>[/blue]
</xsl:template>
<xsl:template match="/">
[blue]<xsl:apply-templates select="*[contains(name(), 'Cust')]" />[/blue]
</xsl:template>
[blue]<xsl:template match="@*|text()">
<xsl:copy-of select="." />
</xsl:template>[/blue]
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top