Skip to main content
suresh_ed
Known Participant
February 23, 2011
Question

Namespace in xml element

  • February 23, 2011
  • 2 replies
  • 215246 views

I have an xml file tagged with namespaces:
for example, ce:section, ce:para

i just trying to collect the 'ce:floats' xml nodes by using the following code:

//Defining the Namespace in the array of arrays of 2 strings
Dim NmSpArr(,) As String = {{"sb", "http://www.elsevier.com/xml/common/struct-bib/dtd"}, {"cl", "http://xml.cengage-learning.com/cendoc-core"}, {"ce", "http://www.elsevier.com/xml/common/dtd"}, {"aid", "http://ns.adobe.com/AdobeInDesign/4.0/"}, {"aid5", "http://ns.adobe.com/AdobeInDesign/5.0/"}, {"xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"}, {"xmlns:mml", "http://www.w3.org/1998/Math/MathML"}, {"xmlns:xlink", "http://www.w3.org/1999/xlink"}}

//Trying to collect the 'ce:floats'
Dim obj As InDesign.Objects = IndDoc.XMLElements(1).EvaluateXPathExpression("//ce:floats", NmSpArr)

ID CS4
Vb.net

Above code always returning 0 as count. It seems there is problem with the xml namespaces definition.

Any help would be greately appreciated.


Regards,
Suresh

This topic has been closed for replies.

2 replies

February 12, 2015

Hi,

This will result elements names with namespace.

var myDocument = app.activeDocument;

var alltags = new Array;

var i1 =0;

var myXPath =  "//*";

var myRuleSet = new Array (new ProcessProduct);

var patt1=new RegExp("^ce:section-title|ce:table|ce:figure|ce:textbox$");

//var patt1=new RegExp(arguments[0]);

with(myDocument) {

        try {

            var elements = xmlElements;

            __processRuleSet(elements.item(0), myRuleSet);

            }

        catch (err) {

            alert (err);

            }

        }

  output()

function output(){

{

     return alltags;

   }

}

function ProcessProduct()

{

    this.name = "ProcessProduct";

    this.xpath = myXPath;

    this.apply = function(myElement, myRuleProcessor)  {

        if(patt1.test(myElement.markupTag.name)==true)

        {

             //alltags[i1++] = myElement.markupTag.name;

             alltags[i1++] = myElement;

            }

      

              }

    return true;

    }

function __processRuleSet (root, ruleSet, prefixMappingTable)

{

    var mainRProcessor = __makeRuleProcessor(ruleSet, prefixMappingTable);

  try {

  __processTree(root, mainRProcessor);

    __deleteRuleProcessor(mainRProcessor);

  } catch (e) {

    __deleteRuleProcessor(mainRProcessor);

    throw e;

    }

}

function __makeRuleProcessor(ruleSet, prefixMappingTable)

{

  var pathArray = new Array();

  for (i=0; i<ruleSet.length; i++)

  {

  pathArray.push(ruleSet.xpath);

  }

    try

    {

     var ruleProcessor = app.xmlRuleProcessors.add(pathArray, prefixMappingTable);

    }

    catch(e){

    throw e;

    }

    var rProcessor =  new ruleProcessorObject(ruleSet, ruleProcessor);

  return rProcessor;

}

function ruleProcessorObject(ruleSet, ruleProcessor)

{

   this.ruleSet = ruleSet;

   this.ruleProcessor = ruleProcessor;

}

function __deleteRuleProcessor(rProcessor) {

  rProcessor.ruleProcessor.remove();

  delete rProcessor.ruleProcessor;

  delete rProcessor.ruleSet;

  delete rProcessor;

}

function __processTree (root, rProcessor)

{

  var ruleProcessor = rProcessor.ruleProcessor;

  try

  {

     var matchData = ruleProcessor.startProcessingRuleSet(root);

  __processMatchData(matchData, rProcessor);

  

  ruleProcessor.endProcessingRuleSet();

  }

  catch (e)

  {

  ruleProcessor.endProcessingRuleSet();

  throw e;

  }

}

function __processMatchData(matchData, rProcessor)

{

  var ruleProcessor = rProcessor.ruleProcessor;

  var ruleSet = rProcessor.ruleSet;

  while (matchData != undefined)

  {

  var element = matchData.element;

  var matchRules = matchData.matchRules;

  var applyMatchedRules = true;

  for (var i=0; i<matchRules.length && applyMatchedRules && !ruleProcessor.halted; i++)

  {

  applyMatchedRules = (false == ruleSet[matchRules].apply(element, rProcessor));

  }

  matchData = ruleProcessor.findNextMatch();

  }

}

Regards,

Parthiban Paramanathan

suresh_ed
suresh_edAuthor
Known Participant
February 26, 2011

It is working for the xml which doesn't have namespaces. For example, xml nodes such as book, author, price.

When it comes to namespaces only it is not working. For example, xml nodes such as ce:book; ce:author; ce:price.

Any help?

Regards,

Suresh

John Hawkinson
Inspiring
February 27, 2011

I assume it doens't work with //float? You could use some ugly XPath expression like "contains(@name,'float')"?

suresh_ed
suresh_edAuthor
Known Participant
February 28, 2011

Its works properly if the xml node doesn't have namespaces. For example, float ('//float') works fine with me.

It is not working at all if the xml node has namespaces. For example, ce:float ('//ce:float')  not working with me.

Breaking my head for the last few days. Any Help?

Regards,

Suresh