рüффп
рüффп

Reputation: 5448

XSLT count nodes by pattern and content

My data are looking like these samples:

Sample 1:

<?xml version="1.0" encoding="UTF-8" ?>
<MYDATA type="store">
    <STORE_ID lang="en">44</STORE_ID>
    <!-- Many other elements here -->
    <MON lang="en">C</MON>
    <TUE lang="en">C</TUE>
    <WED lang="en">C</WED>
    <THU lang="en">C</THU>
    <FRI lang="en">C</FRI>
    <SAT lang="en">C</SAT>
    <SUN lang="en">C</SUN>
</MYDATA>

Sample 2:

<?xml version="1.0" encoding="UTF-8" ?>
<MYDATA type="store">
    <STORE_ID lang="en">48</STORE_ID>
    <!-- Many other elements here -->
    <MON lang="en">O</MON>
    <TUE lang="en">O</TUE>
    <WED lang="en">O</WED>
    <THU lang="en">O</THU>
    <FRI lang="en">O</FRI>
    <SAT lang="en">O</SAT>
    <SUN lang="en">C</SUN>
</MYDATA>

And I have an XSL stylesheet to transform that XML to another one.

I want in some place to add an element if and only if there is at least one 'O' in any of the day (MON to SUN). (Sample 1 must not pass the test and the Sample 2 must pass the test)

I tried this code:

<xsl:if test="count(matches(node-name(), '(MON)(TUE)(WED)(THU)(FRI)(SAT)(SUN)')[text()='O'])>0">
    <OpeningHours id='boutique'>
        <xsl:call-template name="descriptionTemplate" />
        <xsl:call-template name="scheduleTemplate" />
    </OpeningHours>
</xsl:if>

But this syntax is not accepted (Error: Failed to compile stylesheet)

I am looking for a solution in XSLT 1.0 or XSLT 2.0 whatever is the simpler.

Upvotes: 0

Views: 245

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167506

Use

<xsl:template match="MYDATA">
  <xsl:if test="(MON | TUE | WED | THU | FRI | SAT | SUN)[. = 'O']">...</xsl:if>
</xsl:template>

Of course depending on your needs putting the condition into a predicate might even be a better solution:

<xsl:template match="MYDATA[(MON | TUE | WED | THU | FRI | SAT | SUN)[. = 'O']]">...</xsl:template>

Upvotes: 3

Related Questions