Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Match an attribute value in a set of possible values [Thread Next] Re: Match an attribute value in a set of possible valuesTo: NULL Date: 9/1/2006 9:33:00 PM As others have commented, there are different way to structure the externally passed parameter or even to modify dynamically the stylesheet, so that passing a parameter will not be necessary. But your initial question was how to find that the @id attribute of the current element is part of ids list, encoded as a coma-delimited string. One way to determine this is using the following XPath 1.0 expression: contains($idList, concat(' ', @id, ',')) This will work if every id in the list starts with at least one space and is followed immediately by a coma. The global parameter itself will have to be produced and passed to the stylesheet by an implementation-dependent component -- as this varies for different XSLT processors, one needs to study the documentation of the specific XSLT processor used in order to understand how this should be done. Certainly there are ways to perform more precise tokenization (one example is the tokenization implemented in the FXSL library), but this simple solution is probably good just for a start. Cheers, Dimitre Novatchev "patrin" <patrizio.trinchini@g...> wrote in message news:1157117466.003814.249460@m...... > Hi All, > > given the source document: > > <?xml version="1.0" encoding="UTF-8"?> > <root> > <child> > <test id="1" name="first child"/> > </child> > <child> > <test id="2" name="second child"/> > </child> > <child> > <test id="3" name="third child"/> > </child> > </root> > > I would suppress the the child elements that contain a test element > with the value of the id attribute in a list of known ids; for > instance, assuming that the list of ids to remove is {1,3}, I would > obtain the document: > > <?xml version="1.0" encoding="UTF-8"?> > <root> > <child> > <test id="2" name="second child"/> > </child> > </root> > > where only the second child element is retained. > > I'm only able to suppress a single child element using the following > XSL transform; > > <?xml version="1.0" encoding="UTF-8"?> > <xsl:stylesheet > xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:output method="xml" indent="yes" /> > <xsl:template match="@*|node()"> > <xsl:copy> > <xsl:apply-templates select="@*|node()"/> > </xsl:copy> > </xsl:template> > <xsl:template match="child[test[@id='1']]"/> > </xsl:stylesheet> > > Is it possible to match and suppress multiple child elements whose ids > are contained in a given list ? > > Since I'm invoking the transform from Java, I would like to set a > template parameter to the list of ids to be removed; e.g. as comma > delimited list, and write the last match condition as something like > this: > > ... > <xsl:param name="idToRemove" select="default" /> > <xsl:template match="child[test[@id in $idsToRemove]]"/> > ... > > Note that I'm using XSLT 1.0, so I cannot leverage the enhancements of > XSLT 2.0, but I'm wondering if there is some trick to do the job; for > instance injecting the list of ids to remove into the same XSLT > document and then iterating over them and suppressing any matching > child element from the source document... maybe something like this: > > <?xml version="1.0" encoding="UTF-8"?> > <xsl:stylesheet > xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > xmlns:data="http://sample.array.parameters/data" version="1.0"> > <xsl:output method="xml" indent="yes" /> > <data:parameters> > <id>1</id> > <id>3</id> > </data:parameters> > <xsl:variable name="parameters-root" > select="document('')/*/data:parameters"/> > <xsl:template match="@*|node()"> > <xsl:copy> > <xsl:apply-templates select="@*|node()"/> > </xsl:copy> > </xsl:template> > <xsl:template match="child[test[@id in $parameters-root]]"/> > </xsl:stylesheet> > > Thanks a lot for any suggestion > > Regards, > > Patrizio > | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
