Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

List of contacts using XML and DTD

Return to Member Forum

  • Author
    Posts
    • #57629
      Webby77
      Member

      I've just spent the last couple of days getting my head around XML and DTD with the help of Jim Maivald's Lynda.com series and his book. I've sussed out how to create an XML document using Open Office spreadsheet (I don't have MS Office), Dreamweaver and importing it into Indesign. I've also created a DTD that is able to omit missing telephone numbers if a person in a contacts list has a landline but not a mobile, and vice versa. XSLT will have to wait until my brain cools down.

      However, the one thing I can't seem to fathom is how to have a category header show only once at the top of multiple contacts rather than with each contact. For example:

      Category Header (which would otherwise show above each contact)
      Name
      Address
      Phone
      Email

      Name
      Address
      Phone
      Email

      Name
      Address
      Phone
      Email

      Another example is a list of events:

      Month (again which would show up above each Date – Event listing)
      Date – Event
      Date – Event
      Date – Event
      Date – Event

      Can this be done using DTD or must I manually delete each heading? Thanks.

    • #57630

      The DTD is not the correct place to add or remove items; it merely describes what goes where (and with what attributes, and in which order — etc.).

      In fact this shows a shortcoming in your XML file: it ought to look like

      <categoryHeader>
      <contact>
      <name><address><etc>
      </contact>
      <contact>
      <name><address><etc>
      </contact>
      </categoryHeader>

      — but in this case, each contact has its own header. I'm sorry for your overheated brain, but … XSLT is the way to transform one type of XML into another type.

      Can you post a small sample of what your XML looks like now, i.e., with the cat headers in the wrong position?

    • #57631
      Webby77
      Member

      Here's a sample of what I'm doing now…

      <contact>

      <department></department> (the category heading if you like)

      <name></name>

      <phonemobile><phone></phone><mobile></mobile></phonemobile>

      <email></email>

      </contact>

      I can see what you're suggesting in having the <department></department> tags wrap the rest of the content.

      How would I lay this out in in the original spreadsheet I wonder from which the XML derives?

    • #57634

      It would require you to sort by the contact/department field in your spreadsheet, then move the department “one level up” around all of the contact groups with that same department field.

      I have no idea if that's even possible.

      Here is my idea instead :) Let InDesign's XSLT parser do the hard work. If you import your original XML and apply this XSLT on importing, it will create a “dep_group” around a list of contacts for that department. The first element inside the dep_group is your original “department”, and this line is removed from the actual contact information.

      XSLT:

      <?xml version=”1.0″ encoding=”UTF-8″?>
      <xsl:stylesheet version=”1.0″ xmlns:xsl=”https://www.w3.org/1999/XSL/Transform”&#62;
      <xsl:output method=”xml” version=”1.0″ encoding=”UTF-8″ indent=”yes”/>

      <xsl:key name=”all_deps” match=”department” use=”.” />

      <xsl:template match=”/”>
      <xsl:element name=”contactlist”><xsl:apply-templates /></xsl:element>
      </xsl:template>

      <xsl:template match=”contactlist”>
      <xsl:for-each select=”contact[department[generate-id() = generate-id(key('all_deps', .)[1])]]”>
      <xsl:variable name=”cur_dep” select=”department” />
      <xsl:element name=”dep_group”>
      <xsl:element name=”department”><xsl:value-of select=”department” /></xsl:element>
      <xsl:apply-templates select=”../contact[department=$cur_dep]” />
      </xsl:element>
      </xsl:for-each>
      </xsl:template>

      <xsl:template match=”department”></xsl:template>

      <xsl:template match=”@*|node()”>
      <xsl:copy>
      <xsl:apply-templates select=”@*|node()” />
      </xsl:copy>
      </xsl:template>

      </xsl:stylesheet>

      (Warning: It seems some of the straight quotes are translated into 'curly' ones. Sorry 'bout that –you'll gonna have to weed them out one by one…)

    • #57647
      Webby77
      Member

      Thanks Jongware, great, that seems to work – all I need to do now is understand it all ;)

      What resources can you recommend I have a look at to learn more?

    • #57656

      The canonical XSLT reference is at https://www.w3.org/TR/xslt, and https://www.w3schools.com/xsl/ contains lots of useful snips and tips.

      One thing is important: within seconds (well, minutes anyway) I had something up-and-running using the powerful “for-each-group” and “group-by” XSLT commands. However, InDesign didn't load the XSL at all — those commands are “XSLT 2.0”, and apparently InDesign only can handle XSLT 1.0. I didn't even get an error message …

      So I had to take a short round-trip to the web — fortunately, your grouping problem is a well-known one (look for “Grouping with the Muenchian Method”, named after its inventor Steve Muench).

      XSLT is a somewhat odd 'programming language', as it's not procedural/structured, like Javascript or Visual Basic, but rather functional (see https://en.wikipedia.org/wiki/C&#8230;.._paradigms) — you define a set of 'rules' to apply, and the XSLT parser runs each of the rules on your input file.

      It takes some mind-shifting to “get what you want”, because it would seem that most of the times you don't get anything at all … (when none of your rules appear to match anything of the input XML). So yeah, it takes some practice. But, in the words of Kay, “Oh yeah, it's worth it. … If you're strong enough.”

    • #57674
      Webby77
      Member

      I've put those links etc in my favourites and not being a code minded person know its going to take some trawling through. But as you, or Kay says,”It's worth it…”

      I remember getting quite excited by XML when I heard about it on the Indesign Secrets podcast one time (each episode of which I've listened to at least twice (maybe thrice?)). I'm keen to integrate what I do in print with my web presence using a wordpress blog, i.e. people post events, etc on the website which goes to a mysql database, I then create a .csv from that, create an xml file, import into Indesign and so on. I believe that's possible, right?

      Thanks for all your help, and thus, encouragement.

    • #57676

      Mind you, XML is not the only way you can import database data. Look at Mail Merge, for example — it should be able to import your CSV (never used it, personally). You can use this if the transition from web text to InDesign is a straightforward one — i.e., import text, apply styles, print, go home.

      But if you need to move stuff around, rename and group elements, or add custom texts, XML is the way.

      I would recommend not to use InDesign to test your first XSTL transformations with. It doesn't appear to emit any useful errors, and if anything at all is wrong, the XML doesn't appear at all. So I use Saxon, a powerful XSLT processor, through the Kernow GUI. The screenshots are Windows, but it also works happily on my Mac. You edit your XML and XSLT with a good plain text editor (I use TextPad/Windows and TextWrangler/Mac) and process the files with Kernow. Since it has an output window of its own, you can immediately see what happens. You can dot your XSLT with debug stuff like <xsl:message>… thus far it works …</xsl:message>, and these will also appear in the Kernow output window. And if everything seems to work, you can try the same files in InDesign.

      (Apart from the huge difference in XSLT versions — 1.0 for InDesign, which covers pretty much the basics and nothing else, where Saxon supports 2.0 plus a handful of extensions — there appear to be a few minor differences as well. My first try did work in Kernow/Saxon but failed in InDesign. I wouldn't know where to start looking for more info on that. Fortunately, there are dozens of different ways to do the same thing, even with bog standard XSLT 1.0.)

Viewing 7 reply threads
  • The forum ‘General InDesign Topics (CLOSED)’ is closed to new topics and replies.
Forum Ads