14 Replies Latest reply on Dec 12, 2014 8:38 AM by Dirk Becker

    Can you replace 
 with 
 in Indesign (xml)?

    Stamm Level 1

      Hey guys,

       

      when you tag an element in Indesign, you get 
 for linebreaks. I need this to be 


       

      I tried the following:

       

      for(i = 0; i < myDocument.selection.length; i++){

          current = myDocument.selection;

          currentText[i] = current[i].contents;

          currentText[i].replace('&#xD','&#xA');

      }

       

      Any suggestions? Thanks in advance!

        • 1. Re: Can you replace &#xd; with &#xa; in Indesign (xml)?
          Dirk Becker Level 4

          What do you mean with "I need this to be"?

          If your XML goes thru Import and/or Export, you can perform the change by XSLT.

           

          Dirk

          • 2. Re: Can you replace &#xd; with &#xa; in Indesign (xml)?
            Stamm Level 1

            I have never heard of XSLT... Is there any documentation about XSLT? Some guide/tutorial?

             

            Thank you!

            • 3. Re: Can you replace &#xd; with &#xa; in Indesign (xml)?
              Dirk Becker Level 4

              XSLT is a web standard. Amazon yields 75 pages when you search for xslt books.

              You must know that InDesign provides only the old version 1 defined here XSL Transformations.

               

              With Google I found plenty more options regarding whitespace mentioned on this page: Tricky whitespace handling in XSL, unfortunately only incomplete snippets.

              StackExchange is a good site for XSLT related questions, for example this thread will get you close. Remember you have to change that version number to 1.

               

              Anyway, you should first decide (and tell us) whether you want your changes within the existing document (as attempted with your script) or for example on import or export.

               

              Dirk

              • 4. Re: Re: Can you replace &#xd; with &#xa; in Indesign (xml)?
                Dirk Becker Level 4

                As a starting point, the following style sheet replaces both CR and LF (with ! and ? respectively).

                As these are special characters, you may want to play with output options mentioned in previous links.

                 

                Dirk

                 

                <?xml version="1.0" encoding="UTF-8"?>
                 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
                     <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
                 
                     <xsl:template match="text()">
                         <xsl:value-of select="translate(.,'&#xd;&#xa;','?!')"/>
                     </xsl:template>
                     
                     <xsl:template match="@*|node()">
                         <xsl:copy>
                             <xsl:apply-templates select="@*|node()"/>
                         </xsl:copy>
                     </xsl:template>
                         
                 </xsl:stylesheet>
                
                
                • 5. Re: Can you replace &#xd; with &#xa; in Indesign (xml)?
                  Stamm Level 1

                  Hey,

                   

                  first: Thank you really much! I read about XSLT and get the concept now. I want to do this on export in the active Document. My script tags the page items automatically and after tagging everything it exports the XML. I saw now that you can choose a xslt stylesheet on export or "use stylesheet from xml". My last question now is: Does that mean, I can write my xslt on the beginning of the xml document and he will then transform the linebreak entities? Or did I misunderstood something here?

                  • 6. Re: Can you replace &#xd; with &#xa; in Indesign (xml)?
                    Dirk Becker Level 4

                    I've never used that "stylesheet from xml" option, and thus don't know the correct (xml) syntax, or whether it could/must refer to an external source file. I'd anyway prefer the XSLT in a separate file as resource of the program rather than embedded in the document. Have you already found document.xmlExportPreferences?

                     

                    Dirk

                    • 7. Re: Can you replace &#xd; with &#xa; in Indesign (xml)?
                      Stamm Level 1

                      Hey, yeah I found that, via this I am exporting my images in a certain type and quality. I know I can also set this to external xslt stylesheet and also tell indesign to use a certain xslt in the filepath where my .jsx is. I saw people using the indesign xml rules in combination with xslt. Do I need to use these xml rules, or can I just use the xslt stylesheet and link to it in the xml file?

                      • 8. Re: Can you replace &#xd; with &#xa; in Indesign (xml)?
                        Dirk Becker Level 4

                        For your purposes (export) you don't need XML rules.

                         

                        XML Rules is just an overcomplicated mechanism to iterate (search) the in-document XML and invoke some ExtendScript callbacks. You would use it - or its simpler incarnation evaluateXPathExpression() - to change the document e.g. by formatting after xml import.

                        • 9. Re: Can you replace &#xd; with &#xa; in Indesign (xml)?
                          Stamm Level 1

                          Okay, thank you! You really helped much. I will come back here and post the final code when I got it. Thank you

                          • 10. Re: Can you replace &#xd; with &#xa; in Indesign (xml)?
                            Stamm Level 1

                            I don't know why, but without changing anything in the xslt(just using the standard stuff, like indent:yes, omit xml declration: yes , etc.) it converts all &#xd; to &#13; , how does this happen?

                             

                            This is what I got so far:

                             

                            <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

                                 <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

                             

                                <xsl:template match="@*">

                                     <xsl:value-of select="translate(.,'&#13;,&#xd;','&#xa;')"/>

                                 </xsl:template>

                                 

                                 <xsl:template match="@*|node()">

                                     <xsl:copy>

                                         <xsl:apply-templates select="@*|node()"/>

                                     </xsl:copy>

                                 </xsl:template>

                                     

                            </xsl:stylesheet>

                             

                            But I don't know why this won't replace anything. Note that I use @* because my linebreaks are written in the "value" attribute. Sample:

                             

                            <text value="Fröhliche Weihnachten!&#13;&#13;Ich bedanke mich für die schöne und wertvolle Zusammenarbeit und wünsche Ihnen und Ihrer Familie besinnliche aber auch fröhliche Festtage und einen vertrauensvollen Start in das&#13;neue Jahr.&#13;&#13;Ihr Ansprechpartner"/>

                            • 11. Re: Can you replace &#xd; with &#xa; in Indesign (xml)?
                              Dirk Becker Level 4

                              The character entities &#13; and &#xd; mean the same - just different notation (decimal 13 can also be expressed as hex 0x0D ).

                              As the XSLT processor deals with them in some internal XML format, it will just output anything equivalent.

                               

                              Btw, in the second (search for) translate parameter you better remove the comma, otherwise it will also be replaced.

                              • 12. Re: Can you replace &#xd; with &#xa; in Indesign (xml)?
                                Stamm Level 1

                                I removed the comma right there, but the code still does not work. Do you have any suggestion what I have to change in my example? The text tag i posted in my last post, was the output after using the xslt stylesheet.

                                • 13. Re: Re: Can you replace &#xd; with &#xa; in Indesign (xml)?
                                  Dirk Becker Level 4

                                  I'm only casual user of XSLT, so don't take my following comments as ultimate wisdom.

                                  Order of template matching is relevant, the attribute match better works below the generic copy.

                                  When you override the generic "copy" for attributes, you must generate an attribute yourself.

                                   

                                  Btw, expect that &#xa; is output as &#10; which is again the equivalent entity for the same character code.

                                  The following XSLT works in my XSL editor, I haven't tried with InDesign:

                                   

                                  <?xml version="1.0" encoding="UTF-8"?>
                                   <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
                                       <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
                                   
                                       <xsl:template match="@*|node()">
                                           <xsl:copy>
                                               <xsl:apply-templates select="@*|node()"/>
                                           </xsl:copy>
                                       </xsl:template>
                                       
                                       <xsl:template match="@*">
                                           <xsl:attribute name="{name(.)}">
                                               <xsl:value-of select="translate(.,'&#xd;','&#xa;')"/>
                                           </xsl:attribute>
                                       </xsl:template>
                                       
                                   </xsl:stylesheet>
                                  
                                  
                                  
                                  • 14. Re: Can you replace &#xd; with &#xa; in Indesign (xml)?
                                    Stamm Level 1

                                    Woohooo!!! It works!! Perfect!!!! Thank you REALLY much!!!

                                     

                                    I just changed the {name(.)} to "value", because before it didn't work. I guess your selector should select all attributes, but somehow this doesn't work in Indesign and I couldn't find the universal selector('.' didn't work also).

                                     

                                    edit: sorry, the code edited every attribute. the code works now.

                                     

                                    1. <?xml version="1.0" encoding="UTF-8"?> 
                                    2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
                                    3.      <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 
                                    4.   
                                    5.      <xsl:template match="@*|node()"> 
                                    6.          <xsl:copy> 
                                    7.              <xsl:apply-templates select="@*|node()"/> 
                                    8.          </xsl:copy> 
                                    9.      </xsl:template> 
                                    10.       
                                    11.      <xsl:template match="@value"> 
                                    12.          <xsl:attribute name="value"> 
                                    13.              <xsl:value-of select="translate(.,'&#xd;','&#xa;')"/> 
                                    14.          </xsl:attribute> 
                                    15.      </xsl:template> 
                                    16.       
                                    17. </xsl:stylesheet> 
                                    • 15. Re: Can you replace &#xd; with &#xa; in Indesign (xml)?
                                      Dirk Becker Level 4

                                      I'm glad it works for you. More ideas:

                                       

                                      We could reduce the competition between these templates by reducing the first match to <xsl:template match="node()">

                                      Eventually you'll have more luck copying the attribute name with "{name()}" - I omitted the "." here. I don't know whether it works, InDesign's XSLT is notoriously broken in some places.

                                      On the other hand, if you produce only attribute name="value", then your template should also match="@value", otherwise you'll run into duplicates.