Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Match an attribute value in a set of possible values

From: "Dimitre Novatchev" <dimitren@---.---.-->
To: 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
> 




transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent