Browse files

toXML(): add the option of _not_ wrapping a record in a <collection> …

…element

Ironically, this is useful if you want to generate a collection of MARC records.
The user will be responsible for wrapping the resulting XML with an XML header
and <collection> element in the current implementation.


git-svn-id: https://svn.php.net/repository/pear/packages/File_MARC/trunk@301719 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent 9a5e6d9 commit 2c5bf0ed3bbf0dd41d9de22cf9e732eb0690b931 Dan Scott committed Jul 30, 2010
Showing with 16 additions and 8 deletions.
  1. +16 −8 MARC/Record.php
View
24 MARC/Record.php
@@ -488,22 +488,28 @@ function __toString()
* attempts to adhere to the MARCXML standard documented at
* http://www.loc.gov/standards/marcxml/
*
- * @param string $encoding output encoding for the MARCXML record
- * @param bool $indent pretty-print the MARCXML record
+ * @param string $encoding output encoding for the MARCXML record
+ * @param bool $indent pretty-print the MARCXML record
+ * @param bool $collection wrap the <record> element in a <collection> element
*
* @return string representation of MARC record in MARCXML format
*
* @todo Fix encoding input / output issues (PHP 6.0 required?)
*/
- function toXML($encoding = "UTF-8", $indent = true)
+ function toXML($encoding = "UTF-8", $indent = true, $collection = true)
{
$marcxml = new XMLWriter();
$marcxml->openMemory();
$marcxml->setIndent($indent);
- $marcxml->startDocument("1.0", $encoding);
- $marcxml->startElement("collection");
- $marcxml->writeAttribute("xmlns", "http://www.loc.gov/MARC21/slim");
+ if ($collection) {
+ $marcxml->startDocument("1.0", $encoding);
+ $marcxml->startElement("collection");
+ $marcxml->writeAttribute("xmlns", "http://www.loc.gov/MARC21/slim");
+ }
$marcxml->startElement("record");
+ if (!$collection) {
+ $marcxml->writeAttribute("xmlns", "http://www.loc.gov/MARC21/slim");
+ }
// MARCXML schema has some strict requirements
// We'll set reasonable defaults to avoid invalid MARCXML
@@ -551,8 +557,10 @@ function toXML($encoding = "UTF-8", $indent = true)
}
$marcxml->endElement(); // end record
- $marcxml->endElement(); // end collection
- $marcxml->endDocument();
+ if ($collection) {
+ $marcxml->endElement(); // end collection
+ $marcxml->endDocument();
+ }
return $marcxml->outputMemory();

0 comments on commit 2c5bf0e

Please sign in to comment.