|
| 1 | +# QRMarkupXML |
| 2 | + |
| 3 | +[Class `QRMarkupXML`](https://github.com/chillerlan/php-qrcode/blob/main/src/Output/QRMarkupXML.php): [eXtensible Markup Language](https://developer.mozilla.org/en-US/docs/Glossary/XML) (XML) output |
| 4 | + |
| 5 | + |
| 6 | +## Example |
| 7 | + |
| 8 | +See: [XML example](https://github.com/chillerlan/php-qrcode/blob/main/examples/xml.php) |
| 9 | + |
| 10 | +Set the options: |
| 11 | + |
| 12 | +```php |
| 13 | +$options = new QROptions; |
| 14 | + |
| 15 | +$options->outputInterface = QRMarkupXML::class; |
| 16 | +$options->outputBase64 = false; |
| 17 | +// if set to false, the light modules won't be included in the output |
| 18 | +$options->drawLightModules = false; |
| 19 | + |
| 20 | +// assign an XSLT stylesheet |
| 21 | +$options->xmlStylesheet = './qrcode.style.xsl'; |
| 22 | + |
| 23 | +$options->moduleValues = [ |
| 24 | + QRMatrix::M_FINDER_DARK => '#A71111', // dark (true) |
| 25 | + QRMatrix::M_FINDER_DOT => '#A71111', // finder dot, dark (true) |
| 26 | + QRMatrix::M_FINDER => '#FFBFBF', // light (false) |
| 27 | + QRMatrix::M_ALIGNMENT_DARK => '#A70364', |
| 28 | + QRMatrix::M_ALIGNMENT => '#FFC9C9', |
| 29 | + QRMatrix::M_VERSION_DARK => '#650098', |
| 30 | + QRMatrix::M_VERSION => '#E0B8FF', |
| 31 | +]; |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +The XSLT stylesheet `qrcode.style.xsl`: |
| 36 | + |
| 37 | +```XSLT |
| 38 | +<?xml version="1.0" encoding="UTF-8"?> |
| 39 | +<!-- XSLT style for the XML output example --> |
| 40 | +<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 41 | + <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> |
| 42 | + <xsl:template match="/"> |
| 43 | + <!-- SVG header --> |
| 44 | + <svg xmlns="http://www.w3.org/2000/svg" |
| 45 | + version="1.0" |
| 46 | + viewBox="0 0 {qrcode/matrix/@width} {qrcode/matrix/@height}" |
| 47 | + preserveAspectRatio="xMidYMid" |
| 48 | + > |
| 49 | + <!-- |
| 50 | + path for a single module |
| 51 | + we could define a path for each layer and use the @layer attribute for selection, |
| 52 | + but that would exaggerate this example |
| 53 | + --> |
| 54 | + <symbol id="module" width="1" height="1"> |
| 55 | + <circle cx="0.5" cy="0.5" r="0.4" /> |
| 56 | + </symbol> |
| 57 | + <!-- loop over the rows --> |
| 58 | + <xsl:for-each select="qrcode/matrix/row"> |
| 59 | + <!-- set a variable for $y (vertical) --> |
| 60 | + <xsl:variable name="y" select="@y"/> |
| 61 | + <xsl:for-each select="module"> |
| 62 | + <!-- set a variable for $x (horizontal) --> |
| 63 | + <xsl:variable name="x" select="@x"/> |
| 64 | + <!-- draw only dark modules --> |
| 65 | + <xsl:if test="@dark='true'"> |
| 66 | + <!-- position the module and set its fill color --> |
| 67 | + <use href="#module" class="{@layer}" x="{$x}" y="{$y}" fill="{@value}"/> |
| 68 | + </xsl:if> |
| 69 | + </xsl:for-each> |
| 70 | + </xsl:for-each> |
| 71 | + </svg> |
| 72 | + </xsl:template> |
| 73 | +</xsl:stylesheet> |
| 74 | +``` |
| 75 | + |
| 76 | + |
| 77 | +Render the output: |
| 78 | + |
| 79 | +```php |
| 80 | +$data = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'; |
| 81 | +$out = (new QRCode($options))->render($data); // -> XML, rendered as SVG |
| 82 | + |
| 83 | +printf('<img alt="%s" src="%s" />', $alt, $out); |
| 84 | +``` |
| 85 | + |
| 86 | +The associated [XML schema](https://www.w3.org/XML/Schema) can be found over at GitHub: [`qrcode.schema.xsd`](https://github.com/chillerlan/php-qrcode/blob/main/src/Output/qrcode.schema.xsd) |
| 87 | + |
| 88 | + |
| 89 | +## Additional methods |
| 90 | + |
| 91 | +| method | return | description | |
| 92 | +|---------------------------------------------------|--------------------|-------------------------------------------| |
| 93 | +| (protected) `createMatrix()` | `DOMElement` | creates the matrix element | |
| 94 | +| (protected) `row(int $y, array $row)` | `DOMElement\|null` | creates a DOM element for a matrix row | |
| 95 | +| (protected) `module(int $x, int $y, int $M_TYPE)` | `DOMElement\|null` | creates a DOM element for a single module | |
| 96 | + |
| 97 | + |
| 98 | +## Options that affect this class |
| 99 | + |
| 100 | +| property | type | |
| 101 | +|---------------------------|----------| |
| 102 | +| `$drawLightModules` | `bool` | |
| 103 | +| `$outputBase64` | `bool` | |
| 104 | +| `xmlStylesheet` | `string` | |
0 commit comments