get line number of xml node in js - javascript

I'm trying to validate (custom rules) a xml source. Therefore I parse the source with document.evaluate and a certain xpath and validate the result nodes.
If a node is not correct, I would like to give an error with the nodes line number in the source.
How can I go about accomplishing this?

I had similar problem and I wrote a function that finds the n-th tag on the original string based on the result of getElementsByTagName.
It is something like this:
function xmlNodeToOriginalLineNumber(element, xmlRootNode, sContent) {
var sTagName = element.tagName;
var aNodeListByTag = xmlRootNode.getElementsByTagName(sTagName);
var iMaxIndex = 0;
for (var j = 0; j < aNodeListByTag.length; j++) {
if (aNodeListByTag.item(j) === element) {
iMaxIndex = j;
break;
}
}
var regex = new RegExp("<" + sTagName + "\\W", 'g');
var offset = 0;
for (var i = 0; i <= iMaxIndex; i++) {
offset = regex.exec(sContent).index;
}
var line = 0;
for (var i = 0; i < sContent.substring(0, offset).length; i++) {
if (sContent[i] === '\n') {
line++;
}
}
return line + 1;
}
I updated your sample: https://jsfiddle.net/g113c350/3/

Related

how to fill in the value in the array

i have code like this in actionscript3,
var map: Array = [
[[0,1,0],[0,1,0]],
[[0,1,0], [0,1,0]]];
var nom1: int = 0;
var nom2: int = 0;
var nom3: int = 1;
var nom4: int = 18;
stage.addEventListener (Event.ENTER_FRAME, beff);
function beff (e: Event): void
{
map[nom1][nom2][nom3] = nom4
}
stage.addEventListener (MouseEvent.CLICK, brut);
function brut(e: MouseEvent):void
{
trace (map)
}
when run, it gets an error in its output
what I want is to fill in each "1" value and not remove the "[" or "]" sign
so when var nom1, var nom2 are changed
Then the output is
[[[0,18,0],[0,18,0]],
[[0,18,0],[0,18,0]]]
please helps for those who can solve this problem
If what you want to achieve is to replace every 1 by 18 in this nested array, you could try :
for (var i = 0; i < map.length; i++) {
var secondLevel = map[i];
for (var j = 0; j < secondLevel.length; j++) {
var thirdLevel = secondLevel[j];
for (var k = 0; k < thirdLevel.length; k++) {
if (thirdLevel[k] === 1) {
thirdLevel[k] = 18;
}
}
}
}
Note that, this would only work for nested arrays with 3 levels of depth

Extracting Specific Values to TXT from PDF using a Javascript Sequence

I can not find a proper javascript solution for creating a sequence in Adobe Acrobat that will extract text into a .txt file; based on certain criteria.
I have over 500 pdfs with images & financial data on them. I need to extract specific values from these pages. Including values such as: Check number, check date, check amount.
I tried the example at:
https://www.evermap.com/javascript.asp#Title:%20Extract%20ISBN%20numbers
I even created a PDF with ISBN numbers and it doesn't work.
In my PDF I have the below data:
ProcDate: 2019/01/04
AccountNum: 69447885236
CheckAmt: 157.52
SerialNum: 8574
MflmSeqNum: 268245062738
ProcDate: 2019/01/14
AccountNum: 69447885236
CheckAmt: 2,415.36
SerialNum: 8570
MflmSeqNum: 268545187745
I need to extract the values into a text file (or excel table) in a delimited format. The expected output is below:
2019/01/14; 2,415.36; 8570
2019/01/04; 157.52; 8574
Ok so with a little tweaking and getting the loops to carry down I was able to get the desired output the only problem is that it was repeating data and they did not remain correlated:
Below is the loop info:
for (var i = 0; i < this.numPages; i++)
{
numWords = this.getPageNumWords(i);
var PageText = "";
for (var j = 0; j < numWords; j++) {
var word = this.getPageNthWord(i,j,false);
PageText += word;
}
var strMatches = PageText.match(reMatch);
if (strMatches == null) continue;
for (var o = 0; o < this.numPages; o++)
{
numWordsAmt = this.getPageNumWords(o);
var PageTextAmt = "";
for (var k = 0; k < numWordsAmt; k++) {
var wordAmt = this.getPageNthWord(o,k,false);
PageTextAmt += wordAmt;
}
var strMatchesAmt = PageTextAmt.match(reMatchAmt);
if (strMatches == null) continue;
for (var p = 0; p < this.numPages; p++)
{
numWordsNum = this.getPageNumWords(p);
var PageTextNum = "";
for (var l = 0; l < numWordsNum; l++) {
var wordNum = this.getPageNthWord(p,l,false);
PageTextNum += wordNum;
}
var strMatchesNum = PageTextNum.match(reMatchNum);
if (strMatchesAmt == null) continue;
// now output matches into report document
for (j = 0; j < strMatches.length; j++) {
for (k = 0; k < strMatchesAmt.length; k++) {
for (l = 0; l < strMatchesNum.length; l++) {
Out[strMatches[j].replace("ProcDate: ", "")+" , "+strMatchesAmt[k].replace("CheckAmt: ", "")+" , "+strMatchesNum[l].replace("SerialNum: ", "")] = true; // store email as a property name
}
}
}
}
}
}

Remove attributes from html string - error with removeAttributeNode

I am trying to remove all attributes except for certain whitelisted ones from a long html string. I am using the DOM to parse it.
I know there are a lot of other questions about this, but my question is specifically about the error I am getting with executing removeAttributeNode in my code below.
var div = document.createElement('div');
div.innerHTML = '<p class=\"p\">text here</p> <div id=\"divId\"></div>';
var elements = div.getElementsByTagName('*');
var whitelist = ["src", "width", "height"];
for (var i = 0; i < elements.length; i++) {
if (elements[i].attributes.length !== 0) {
var attr = elements[i].attributes;
for (var j = 0; j < attr.length; j--) {
var attrName = attr[j].name;
for (var k = 0; k < whitelist.length; k++) {
if (attrName !== whitelist[k])
elements[i].removeAttributeNode(attr);
}
}
}
}
But, I keep getting the following error: Failed to execute 'removeAttributeNode ' on Element: the 1st argument provided is either null, or an invalid Attr object.
But, I checked with console statements and elements[i] is not null. For example, elements[0] = <p class="p">. How do I get removeAttributeNode to work? Thank you for your help!
In the last loop, do something like this.
for (var k = 0; k < whitelist.length; k++) {
if (attrName !== whitelist[k])
{
elements[i].removeAttributeNode(elements[i].getAttributeNode(attrName ));
}
}

how to add a line break in javascript that works with paragraph

I wrote this function to format a certain string :
var desc = document.getElementById('desc');
var parContent = desc.textContent.trim();
var tempPar = "";
var j = 0;
var k = 0;
var built_val = "";
for (var i = 0; i < parContent.length ; i++)
{
if (j == 19 || i == parContent.length-1)
{
tempPar = parContent.substring(k, i);
tempPar = tempPar.concat("- \n");
built_val = built_val.concat(tempPar);
tempPar = "";
//Restart j
j = 0;
//update k
k = i;
continue;
}
j++;
}
desc.textContent = built_val;
Desc is a dynamic paragraph that is usually empty at first then filled (its data are composed after the page loads), j is the number of characters desired in one line.
Though now I have another problem and that is \n doesn't work ; I also tried br . How can I insert a new linebreak within a javascript string such as "built_val" ? please note how it's assigned to an Html after everything.
The textContent property sets the literal text of the element (by adding a text node), and will not parse your tags as html. Instead, you should do this:
desc.innerHTML = built_val;
Yes, you're using .textContent which is why <br>s won't be parsed (which is a good thing!)
You want to use document.createTextNode() and document.createElement('br').
var desc = document.getElementById('desc');
var parContent = desc.textContent.trim();
var tempPar = "";
var j = 0;
var k = 0;
var built_val = "";
for (var i = 0; i < parContent.length; i++) {
if (j == 19) {
tempPar = parContent.substring(k, i);
tempPar = tempPar.concat("- \n");
desc.appendChild(document.createTextNode(tempPar));
desc.appendChild(document.createElement('br'));
tempPar = "";
//Restart j
j = 0;
//update k
k = i;
continue;
}
j++;
}
// No need for textContent anymore. Appending nodes did the work for us!
Just for fun: an Array.forEach and String.slice method:
var desc = document.querySelector('#desc');
var parContent = desc.textContent.trim();
var block = 0;
parContent.split('').forEach(
function(v, i) {
if (i % 19 == 0) {
desc.appendChild(document.createTextNode(parContent.slice(block, i)));
desc.appendChild(document.createElement('br'));
block = i;
}
}
);
// last part
desc.appendChild(document.createTextNode(parContent.slice(block)));
<p id="desc">
This string should be truncated every 19th position right?
Ok, let's give it a try using [Array.forEach]<br>
</p>
There's an easy regex version to wrap text as well:
function Wrap(src, maxLineLength) {
return src.replace(new RegExp("(.{1,"+maxLineLength+"})", "g"), "$1<br/>\r\n");
}
Though this wraps on characters, not words.

Getting a nth element from a string

How can I get a certain nth element from a string. Like if I want to get every 3rd element from the word GOOGLE how can i do that. SO far i've done this but i dont know what to type after the If
function create_string( string ) {
var string_length=string.length;
var new_string=[];
for( var i=0; i<string_length; i++) {
if(string[i]%3==0) {
}
new_string.push(string[i]);
}
return new_string;
}
Use the charAt() function of String which returns the char at a specific index passed to the function. Using charAt, I have created a script that will return every third character.
var result = "";
for(var i = 2; i < test.length; i+=3){
result += test.charAt(i);
}
If you would like to turn this script into a more reusable function:
var test = "GOOGLE";
function getEveryNthChar(n, str){
var result = "";
for(var i = (n-1); i < test.length; i+=n){
result += str.charAt(i);
}
return result;
}
alert(getEveryNthChar(1,test));
alert(getEveryNthChar(2,test));
alert(getEveryNthChar(3,test));
alert(getEveryNthChar(4,test));
Working Demo: http://jsfiddle.net/Q7Lx2/
Documentation
How about this?
function create_string( string ) {
var string_length=string.length;
var new_string=[];
for( var i=2; i<string_length; i+=3) { // instead of an if, use +=3
new_string.push(string.charAt(i));
}
return new_string.join(""); // turn your array back into a string
}
Note that if you start making this compact, you'll end up with the same answer as Kevin's ;-)
function create_string( s ) {
var new_string = '';
for( var i=2; i<s.length; i+=3) { // instead of an if, use +=3
new_string += s.charAt(i);
}
return new_string;
}
Here's a function that will work for any number, not just 3:
function stringHop(s, n) {
var result = "";
for (var i = 0; i < s.length; i+= n) {
result += s.charAt(i);
}
return result;
}
var foo = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var bar = stringHop(foo, 2); // returns "ACEGIKMOQSUWY"
var baz = stringHop(foo, 3); // returns "ADGJMPSVY"
String.charAt(index) will return the character at the specified index, from 0 to String.length - 1. So:
String.prototype.every = function(n) {
var out = '';
for (var i = 0; i < this.length; i += n) {
out += this.charAt(i);
}
return out;
}
var str = "GOOGLE";
console.log(str.every(3)) // Outputs: GG
If you don't want to include the first character, then change the for loop to:
for (var i = n - 1; i < this.length; i += n) {

Categories