aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-077/ash/xslt/ch-2.xml25
-rw-r--r--challenge-077/ash/xslt/ch-2.xslt41
2 files changed, 66 insertions, 0 deletions
diff --git a/challenge-077/ash/xslt/ch-2.xml b/challenge-077/ash/xslt/ch-2.xml
new file mode 100644
index 0000000000..ce27ac5cdb
--- /dev/null
+++ b/challenge-077/ash/xslt/ch-2.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0"?>
+
+<!--
+ Task 2 from
+ https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/
+
+ To get a solution, run:
+ $ xsltproc ch-2.xslt ch-2.xml
+
+ For the given example matrix, you get the following output (items count from 1):
+ 1, 2
+ 3, 1
+-->
+
+<matrix>
+ <row>
+ <item>O</item> <item>X</item> <item>O</item>
+ </row>
+ <row>
+ <item>O</item> <item>O</item> <item>O</item>
+ </row>
+ <row>
+ <item>X</item> <item>O</item> <item>O</item>
+ </row>
+</matrix>
diff --git a/challenge-077/ash/xslt/ch-2.xslt b/challenge-077/ash/xslt/ch-2.xslt
new file mode 100644
index 0000000000..3a1fb688da
--- /dev/null
+++ b/challenge-077/ash/xslt/ch-2.xslt
@@ -0,0 +1,41 @@
+<?xml version="1.0"?>
+
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+
+<xsl:output omit-xml-declaration="yes"/>
+
+<xsl:template match="/matrix">
+ <xsl:apply-templates select="row"/>
+</xsl:template>
+
+<xsl:template match="row">
+ <xsl:apply-templates select="item"/>
+</xsl:template>
+
+<!-- catch 'O's -->
+<xsl:template match="item"/>
+
+<xsl:template match="item[text() = 'X']">
+ <xsl:variable name="row" select="count(../preceding-sibling::*) + 1"/> <!-- position of parent -->
+ <xsl:variable name="col" select="position()"/>
+
+ <xsl:variable name="lonely" select="
+ (not(/matrix/row[$row ]/item[$col - 1]) or /matrix/row[$row ]/item[$col - 1] = 'O') and
+ (not(/matrix/row[$row ]/item[$col + 1]) or /matrix/row[$row ]/item[$col + 1] = 'O') and
+ (not(/matrix/row[$row + 1]/item[$col ]) or /matrix/row[$row + 1]/item[$col ] = 'O') and
+ (not(/matrix/row[$row - 1]/item[$col ]) or /matrix/row[$row - 1]/item[$col ] = 'O') and
+ (not(/matrix/row[$row + 1]/item[$col + 1]) or /matrix/row[$row + 1]/item[$col + 1] = 'O') and
+ (not(/matrix/row[$row - 1]/item[$col - 1]) or /matrix/row[$row - 1]/item[$col - 1] = 'O') and
+ (not(/matrix/row[$row + 1]/item[$col - 1]) or /matrix/row[$row + 1]/item[$col - 1] = 'O') and
+ (not(/matrix/row[$row - 1]/item[$col + 1]) or /matrix/row[$row - 1]/item[$col + 1] = 'O')
+ "/>
+
+ <xsl:if test="$lonely">
+ <xsl:value-of select="$row"/>
+ <xsl:text>, </xsl:text>
+ <xsl:value-of select="$col"/>
+ <xsl:text disable-output-escaping="yes">&#10;</xsl:text>
+ </xsl:if>
+</xsl:template>
+
+</xsl:stylesheet>