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!

xsl:copy to filter a document and leave the structure intact with mult

Status
Not open for further replies.

gratik

Programmer
Jul 15, 2004
1
GB
Hi
I have the following document (made up sample)

<doc>
<node id="1"/>
<node id="2">
<node id="2.1"/>
</node>
<node id="3">
<node id="3.1"/>
<node id="3.2"/>
</node>
</doc>


I want to be able to filter the document so that only certain id's are
returned but the number of ids may change each call, so for example:

If I wanted Id's 2.1 and 3 I would expect to see the following document
returned

<doc>
<node id="2">
<node id="2.1"/>
</node>
<node id="3"/>
</doc>


and maybe next time I would want to filter just on id 3.1 to return

<doc>
<node id="3">
<node id="3.1"/>
</node>
</doc>

each time I want to return the element with the id I have selected and
everything leading up to it but no further and I may want to pass in
different ids and number of ids each time

I currently have the following xsl which if I hardcode in a single id will
return the results I expect for a single id, put I have no idea how to
expand this for multiple ids.


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl=" <xsl:param name="idwanted">3.2</xsl:param>
<xsl:template match="/ | * | @* | node()">
<xsl:copy>
<xsl:apply-templates select="* | @* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node[not(@id='3.2') and not(.//node[@id='3.2'])]" />
</xsl:stylesheet>

as soon as I try to get this to use a param to replace the hardcoded id like
this

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl=" <xsl:param name="idwanted">3.2</xsl:param>
<xsl:template match="/ | * | @* | node()">
<xsl:copy>
<xsl:apply-templates select="* | @* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node[not(@id=$idwanted) and
not(.//node[@id=$idwanted])]" />
</xsl:stylesheet>

I am getting the error that param and variables are not allowed in patterns

Any help greatly appreciated, even if its just to say your being stupid you
should be doing x as this is driving me nuts!!


Thanks

G
 
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
   <xsl:param name="idwanted">3.2</xsl:param>

   <xsl:template match="/ | * | @* ">
      <xsl:copy>
         <xsl:if test="@id=$idwanted or .//node[@id=$idwanted]">
            <xsl:apply-templates select="* | @* " />
         </xsl:if>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top