开发者

XSL test on a set of nodes?

I have a set of "file" nodes with a type attribute

<files>
    <file type="main">asdf</file>
    <file type="main_en">asdf</file>
    <file type="main_fr">asdf</file>
    <file type="pdf">asdf<开发者_如何学Go;/file>
</files>

How do I check on the set of files if one of the nodes has at least 1 attribute that starts with "main". I was thinking something like:

<xsl:when test="contains(string(files/file[@type]),'main')">

But all the functions or tests I know seem to only be for a specific node and not a set of nodes. I would rather avoid using a for each type solution.


How do I check on the set of files if one of the nodes has at least 1 attribute that starts with "main".

Use:

boolean(/*/file[@*[starts-with(., 'main')]])

This evaluates to true() exactly when there is at least one file element that is a child of the top element of the XML document and that has at least one attribute, whose string value starts with the string "main"

When used in a test attribute (of <xsl:if> or <xsl:when>) or as a predicate, the reference to the boolean() function is not necessary and may be omitted:

/*/file[@*[starts-with(., 'main')]]


<xsl:when test="files/file[contains(@type, 'main')]">

Or, even better:

<xsl:when test="files/file[starts-with(@type, 'main')]">


Don't know your real purpose, but this example might help you:

 <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/files">
        <xsl:if test="file[contains(@type,'main')]">
            There is at least one file element with 'main' in @type
        </xsl:if>
  </xsl:template>


</xsl:stylesheet>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜