Finding a particular attribute of tags within the document

Post here questions and problems related to oXygen frameworks/document types.
pramanikhimangshu
Posts: 17

Finding a particular attribute of tags within the document

Tue Jan 16, 2018 5:13 pm

Hi,

I need to find a particular type of attribute from the any xml tags of dita document in Oxygen Web Author.
I tried doing it by JSAPI modifying framework.js:

Code: Select all

   function printAllAttrVals(rootNode)
   {
      if(rootNode == null || rootNode == undefined || rootNode.id == undefined)
             return;
        console.log(rootNode);
        console.log(rootNode.id);
        console.log(rootNode.getContent());
        console.log(rootNode.getTagName());
       console.log(rootNode.getAttribute("<attribute>"));
        var children = rootNode.getChildNodes();
        if(children.length == 0)
        {

        }
        else
        {
             for(var xtrip = 0;xtrip<children.length;xtrip++)
            {
                  printAllAttrVals(children[xtrip]);
             }
        }
   }
   var checkNode = globalAttrs.selectedElement;
     while(checkNode && checkNode.getTagName() != 'conbody')
     {
          console.log(checkNode.getTagName());
          checkNode = checkNode.getParent();
     }
     printAllAttrVals(checkNode);//[calling from conbody]


Getting JS error:
workspace-c71778b94e.js:1359 Uncaught TypeError: Cannot read property 'nextElementSibling' of null
at Object.sync.model.XmlDom.getNextSibling (workspace-c71778b94e.js:1359)
at Object.sync.model.XmlDom.getChildNodes (workspace-c71778b94e.js:1359)
at sync.api.dom.Element.sync.api.dom.Node.getChildNodes (workspace-c71778b94e.js:1534)

Is it the correct way to find this or any other way is there.
Can you please let me know the best way to do this?
mihaela
Posts: 190

Re: Finding a particular attribute of tags within the document

Thu Jan 18, 2018 12:13 pm

Hi,

Can you please give us more details about what you are trying to achive?
More context will be helpful in order to guide you to use some API.

Best Regards,
Mihaela
Mihaela Calotescu
http://www.oxygenxml.com
pramanikhimangshu
Posts: 17

Re: Finding a particular attribute of tags within the document

Thu Jan 18, 2018 12:29 pm

HI Mihaela,

I am trying to customize the dita framework. I am trying to get the dita tags(sync.api.dom.Element) which has a particular attribute in whole dita document.

Eg: If I want to get all the Dita tags which are links. So I would like to search for href attribute in all the tags and whichever tags have the href attribute I will Store in some variable. I want to know how to achieve this, to start from root tag and go through all tags searching for the attr? Or there is any other way to perform this.
Considering if I have a concept Dita topic I started from <concept> in my current implementation, in some case while calling getChildNodes on some nodes getting this Error:
workspace-c71778b94e.js:1359 Uncaught TypeError: Cannot read property 'nextElementSibling' of null
at Object.sync.model.XmlDom.getNextSibling (workspace-c71778b94e.js:1359)
at Object.sync.model.XmlDom.getChildNodes (workspace-c71778b94e.js:1359)
at sync.api.dom.Element.sync.api.dom.Node.getChildNodes (workspace-c71778b94e.js:1534)

I hope I can explain it.
Regards,
Himangshu.
cristi_talau
Posts: 169

Re: Finding a particular attribute of tags within the document

Thu Jan 18, 2018 8:04 pm

Hello,

We have to kinds of APIs, a limited client-side JS API and a more powerful server-side Java API. The JS API currently does not allow you to easily make a list of nodes with a certain attribute.

If we understand your end-goal, we can either suggest a more suitable JS or Java API, or we can improve the existing API to accomodate your use-case, if there is no alternative. To this end we need to understand your end-goal. The kind of end-goal that we would be interested in is something like
- I am trying to create a side-view with all the elements with links
- I am trying to implement an operation that changes the attributes of all elements in bulk
- I am trying to validate that all elements have a valid attribute
- etc.

Best,
Cristian
pramanikhimangshu
Posts: 17

Re: Finding a particular attribute of tags within the document

Fri Jan 19, 2018 9:35 am

Hi,

I am trying to create internal content linking in the Dita Document.
To link a internal content, I need to display all the elements having ID attribute to the user to select which element to use and create a link of that element in the currently selected text.
This is the use-case: A example document, which have three elements with ID attribute -
ID TAG Name
---------------------------
test1 dt
test2 dt
table1 table

User selects any one of those and a link is created at the selection : with <xref href=ditaID/#<ID of Selected item>/>
For this I need to find all the elements with ID attr and display it to the user to select from, I am trying to accomplish this using JS-api:

Code: Select all

function collectNodeID(rootNode)
{
  if(rootNode == null || rootNode == undefined || rootNode.id == undefined || rootNode.getType() != goog.dom.NodeType.ELEMENT)
    return;
  if(rootNode.getAttribute("id"))
  {
    globalAttrs.listOfIDs.push(rootNode);
  }
  var children;
  try
  {
    children = rootNode.getChildNodes();
  }
  catch(error)
  {
    // console.log(error);
    children = [];
  }
  if(children.length == 0)
  {
    return;
  }
  else
  {
    for(var xtrip = 0;xtrip<children.length;xtrip++)
    {
      collectNodeID(children[xtrip]);
    }
  }
}

rootNode (of type sync.api.dom.Element) is the root tag.
globalAttrs.listOfIDs is the array where I store the IDs.

With this code I can establish the use case. I would like to know if there is any better way to establish this use case.
I got error at the position : // console.log(error); stating :

Code: Select all

workspace-c71778b94e.js:1359 Uncaught TypeError: Cannot read property 'nextElementSibling' of null
at Object.sync.model.XmlDom.getNextSibling (workspace-c71778b94e.js:1359)
at Object.sync.model.XmlDom.getChildNodes (workspace-c71778b94e.js:1359)
at sync.api.dom.Element.sync.api.dom.Node.getChildNodes (workspace-c71778b94e.js:1534)

for some empty elements like <p></p>. Thus catching the error solved the issue but I want to confirm if this customization is full-proof or there is some better alternative way.

Thank you.
Regards,
Himangshu
cristi_talau
Posts: 169

Re: Finding a particular attribute of tags within the document

Fri Jan 19, 2018 1:12 pm

Hello,

Thanks for the detailed explanation. Now it makes sense. I managed to reproduce the problem you reported. The fix will be shipped in the next version of Web Author. Meanwhile you can include this patch in your code:

Code: Select all

if (sync.api.Version === 'v19.1.0') {
  sync.model.XmlDom.getChildNodes = function(elem) {
    var children = [];
    var child = sync.model.XmlDom.getFirstChild(elem);
    if (child) {
      do {
        children.push(child);
        child = sync.model.XmlDom.getNextSibling(child);
      } while (child !== null);
    }

    return children;
  };
}


This patch works only with version '19.1'. Once you upgrade, you can safely remove it.

Best,
Cristian

Return to “SDK-API, Frameworks - Document Types”

Who is online

Users browsing this forum: No registered users and 3 guests