QRMarkupXML.md.txt 3.6 KB

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