英文:
Looping through page nos for XML data
问题
以下是我的XML数据。
<?xml version="1.0" encoding="UTF-8"?>
<pam:article xml:lang="en-US">
<FEATURES>
<p id="title">WITH A SONG IN MY HEART</p>
<p id="pageNo">6</p>
<p>The screen version of the Jane Froman story performed in brilltant technicolor.</p>
<p id="title">LES PAUL AND MARY FORD</p>
<p id="pageNo">12</p>
<p id="title">SONG OF THE MONTH</p>
<p id="pageNo">14</p>
</FEATURES>
<POPULAR>
<p id="title">A GARDEN IN THE RAIN</p>
<p id="pageNo">10</p>
<p id="title">A HEART FULL OF LOVE</p>
<p id="pageNo">17</p>
<p id="title">A WEAVER OF DREAMS</p>
<p id="pageNo">13</p>
<p id="title">ALLEGHENY FIDDLER</p>
<p id="pageNo">15</p>
<p id="title">ANY TIME</p>
<p id="pageNo">7</p>
<p id="title">AT LAST! AT LAST!</p>
<p id="pageNo">3</p>
<p id="title">BABY, WE'RE REALLY IN LOVE</p>
<p id="pageNo">16</p>
<p id="title">BE MY-LIFE'S COMPANION</p>
<p id="pageNo">15</p>
<p id="title">CALL ME A DREAMER</p>
<p id="pageNo">15</p>
<p id="title">CHINATOWN, MY CHINATOWN</p>
<p id="pageNo">16</p>
<p id="title">CLOSER YOU ARE, THE</p>
<p id="pageNo">13</p>
<p id="title">COME WHAT MAY</p>
<p id="pageNo">11</p>
<p id="title">CRY</p>
<p id="pageNo">3</p>
<p id="title">DANCE ME LOOSE</p>
<p id="pageNo">5</p>
<p id="title">DID ANYONE CALL</p>
<p id="pageNo">15</p>
<p id="title">8TH STREET ASSOCIATION</p>
<p id="pageNo">16</p>
<p id="title">ETERNALLY</p>
<p id="pageNo">8</p>
<p id="title">EV'RY DAY</p>
<p id="pageNo">16</p>
<p id="title">GENTRE HANDS</p>
<p id="pageNo">10</p>
<p id="title">GET HAPPY</p>
<p id="pageNo">6</p>
<p id="title">GOODBYE SWEETHEART</p>
<p id="pageNo">3</p>
<p id="title">GRAND CENTRAL STATION</p>
<p id="pageNo">8</p>
<p id="title">HEART OF A CLOWN, THE</p>
<p id="pageNo">9</p>
<p id="title">IF YOU GO</p>
<p id="pageNo">17</p>
<p id="title">I'LL ALWAYS BE FOLLOWING YOU</p>
<p id="pageNo">8</p>
<p id="title">I'LL STILL LOVE YOU</p>
<p id="pageNo">8</p>
<p id="title">I'LL WALK ALONE</p>
<p id="pageNo">4</p>
<p id="title">IT'S RAINING</p>
<p id="pageNo">15</p>
<p id="title">LITTLE WHITE CLOUD THAT CRIED, THE</p>
<p id="pageNo">7</p>
<p id="title">MAKE WITH THE MAGIC</p>
<p id="pageNo">10</p>
<p id="title">MARSHMALLOW MOON</p>
<p id="pageNo">9</p>
<p id="title">MOTHER AT YOUR FEET IS KNEELING</p>
<p id="pageNo">5</p>
<p id="title">NECESSARY EVIL</p>
<p id="pageNo">13</p>
<p id="title">NEITHER AM I</p>
<p id="pageNo">9</p>
<p id="title">OLD PAINT (Ride Around, Little Dogies)</p>
<p id="pageNo">7</p>
<p id="title">ON MY WAY (Got My Travelin' Shoes)</p>
<p id="pageNo">5</p>
<p id="title">OOH! WHAT YOU DID!</p>
<p id="pageNo">14</p>
<p id="title">PLAY ME A HURTIN' TUNE</p>
<p id="pageNo">8</p>
<p id="title">PLEASE MR. SUN</p>
<p id="pageNo">3</p>
<p id="title">RETREAT</p>
<p id="pageNo">5</p>
<p id="title">RIVER, STAY 'WAY FROM MY DOOR</p>
<p id="pageNo">10</p>
<p id="title">SAD</p>
<p id="pageNo">14</p>
<p id="title">SHRIMP BOATS</p>
<p id="pageNo">10</p>
<p id="title">SILLY DREAMER</p>
<p id="pageNo">10</p>
<p id="title">SINCE MY LOVE HAS GONE</p>
<p id="pageNo">3</p>
<p id="title">SLOWPOKE</p>
<p id="pageNo">7</p>
<p id="title">SWEETHEART SERENADE, THE</p>
<p id="pageNo">16</p>
<p id="title">TAKE ME BACK</p>
<p id="pageNo">11</p>
<p id="title">TAKE ME HOME</p>
<p id="pageNo">11</p>
<p id="title">TELL ME WHY</p>
<p id="pageNo">14</p>
<p id="title">THIS IS MY NIGHT TO DREAM</p>
<p id="pageNo">11</p>
<p id="title">TULIPS AND HEATHER</p>
<p id="pageNo">7</p>
<p id="title">UNDECIDED</p>
<p id="pageNo">14</p>
<p id="title">UNTIL</p>
<p id="pageNo">3</p>
<p id="title">WE WON'T LIVE IN A CASTLE</p>
<p id="pageNo">17</p>
<p id="title">WHEEL OF FORTUNE</p>
<p id="pageNo">3</p>
<p id="title">WHEN I TAKE MY SUGAR TO TEA</p>
<p id="pageNo">3</p>
<p id="title">WHERE I MAY LIVE WITH MY LOVE</p>
<p id="pageNo">8</p>
<p id="title">WIMMIN</p>
<p id="pageNo">10</p>
<p id="title">YOU'RE GONNA LOVE SOMEBODY, SOMEDAY</p>
<p id="pageNo">10</p>
</POPULAR>
</pam:article>
这是一本书的目录页。我在我的脚本中从开头循环遍历每一页。有些页面可能不在目录页中。如果我找到一个目录页,并且它是特征页面之一(XML特征页号),我想执行X操作,如果它是流行页面(XML流行页号)之一,我想执行Y操作。如果以上都不是,则执行Z操作。我已经有了X、Y和Z的必要代码。我无法解决这个比较问题。请帮忙!
英文:
Following is my XML data.
<?xml version="1.0" encoding="UTF-8"?>
<pam:article xml:lang="en-US">
<FEATURES>
<p id="title">"WITH A SONG IN MY HEART�</p>
<p id="pageNo">6</p>
<p>The screen version of the Jane Froman story performed in brilltant technicolor.</p>
<p id="title">LES PAUL AND MARY FORD</p>
<p id="pageNo">12</p>
<p id="title">SONG OF THE MONTH</p>
<p id="pageNo">14</p>
</FEATURES>
<POPULAR>
<p id="title">A GARDEN IN THE RAIN</p>
<p id="pageNo">10</p>
<p id="title">A HEART FULL OF LOVE</p>
<p id="pageNo">17</p>
<p id="title">A WEAVER OF DREAMS</p>
<p id="pageNo">13</p>
<p id="title">ALLEGHENY FIDDLER</p>
<p id="pageNo">15</p>
<p id="title">ANY TIME</p>
<p id="pageNo">7</p>
<p id="title">AT LAST! AT LAST!</p>
<p id="pageNo">3</p>
<p id="title">BABY, WE'RE REALLY IN LOVE</p>
<p id="pageNo">16</p>
<p id="title">BE MY-LIFE'S COMPANION</p>
<p id="pageNo">15</p>
<p id="title">CALL ME A DREAMER</p>
<p id="pageNo">15</p>
<p id="title">CHINATOWN, MY CHINATOWN</p>
<p id="pageNo">16</p>
<p id="title">CLOSER YOU ARE, THE</p>
<p id="pageNo">13</p>
<p id="title">COME WHAT MAY</p>
<p id="pageNo">11</p>
<p id="title">CRY</p>
<p id="pageNo">3</p>
<p id="title">DANCE ME LOOSE</p>
<p id="pageNo">5</p>
<p id="title">DID ANYONE CALL</p>
<p id="pageNo">15</p>
<p id="title">8TH STREET ASSOCIATION</p>
<p id="pageNo">16</p>
<p id="title">ETERNALLY</p>
<p id="pageNo">8</p>
<p id="title">EV'RY DAY</p>
<p id="pageNo">16</p>
<p id="title">GENTRE HANDS</p>
<p id="pageNo">10</p>
<p id="title">GET HAPPY</p>
<p id="pageNo">6</p>
<p id="title">GOODBYE SWEETHEART</p>
<p id="pageNo">3</p>
<p id="title">GRAND CENTRAL STATION</p>
<p id="pageNo">8</p>
<p id="title">HEART OF A CLOWN, THE</p>
<p id="pageNo">9</p>
<p id="title">IF YOU GO</p>
<p id="pageNo">17</p>
<p id="title">I'LL ALWAYS BE FOLLOWING YOU</p>
<p id="pageNo">8</p>
<p id="title">I'LL STILL LOVE YOU</p>
<p id="pageNo">8</p>
<p id="title">I'LL WALK ALONE</p>
<p id="pageNo">4</p>
<p id="title">IT'S RAINING</p>
<p id="pageNo">15</p>
<p id="title">LITTLE WHITE CLOUD THAT CRIED, THE</p>
<p id="pageNo">7</p>
<p id="title">MAKE WITH THE MAGIC</p>
<p id="pageNo">10</p>
<p id="title">MARSHMALLOW MOON</p>
<p id="pageNo">9</p>
<p id="title">MOTHER AT YOUR FEET IS KNEELING</p>
<p id="pageNo">5</p>
<p id="title">NECESSARY EVIL</p>
<p id="pageNo">13</p>
<p id="title">NEITHER AM I</p>
<p id="pageNo">9</p>
<p id="title">OLD PAINT (Ride Around, Little Dogies)</p>
<p id="pageNo">7</p>
<p id="title">ON MY WAY (Got My Travelin� Shoes)</p>
<p id="pageNo">5</p>
<p id="title">OOH! WHAT YOU DID!</p>
<p id="pageNo">14</p>
<p id="title">PLAY ME A HURTIN� TUNE</p>
<p id="pageNo">8</p>
<p id="title">PLEASE MR. SUN</p>
<p id="pageNo">3</p>
<p id="title">RETREAT</p>
<p id="pageNo">5</p>
<p id="title">RIVER, STAY �WAY FROM MY DOOR</p>
<p id="pageNo">10</p>
<p id="title">SAD</p>
<p id="pageNo">14</p>
<p id="title">SHRIMP BOATS</p>
<p id="pageNo">10</p>
<p id="title">SILLY DREAMER</p>
<p id="pageNo">10</p>
<p id="title">SINCE MY LOVE HAS GONE</p>
<p id="pageNo">3</p>
<p id="title">SLOWPOKE</p>
<p id="pageNo">7</p>
<p id="title">SWEETHEART SERENADE, THE</p>
<p id="pageNo">16</p>
<p id="title">TAKE ME BACK</p>
<p id="pageNo">11</p>
<p id="title">TAKE ME HOME</p>
<p id="pageNo">11</p>
<p id="title">TELL ME WHY</p>
<p id="pageNo">14</p>
<p id="title">THIS IS MY NIGHT TO DREAM</p>
<p id="pageNo">11</p>
<p id="title">TULIPS AND HEATHER</p>
<p id="pageNo">7</p>
<p id="title">UNDECIDED</p>
<p id="pageNo">14</p>
<p id="title">UNTIL</p>
<p id="pageNo">3</p>
<p id="title">WE WON'T LIVE IN A CASTLE</p>
<p id="pageNo">17</p>
<p id="title">WHEEL OF FORTUNE</p>
<p id="pageNo">3</p>
<p id="title">WHEN I TAKE MY SUGAR TO TEA</p>
<p id="pageNo">3</p>
<p id="title">WHERE I MAY LIVE WITH MY LOVE</p>
<p id="pageNo">8</p>
<p id="title">WIMMIN</p>
<p id="pageNo">10</p>
<p id="title">YOU'RE GONNA LOVE SOMEBODY, SOMEDAY</p>
<p id="pageNo">10</p>
</POPULAR>
</pam:article>
this is the TOC page of a book. Im looping through each page from the beginning in my script. and some pages might not be in the TOC page. if I found a TOC page, and its one of the feature pages (XML Feature page nos) I want to do X, if its page from the Popular pages (XML popular page nos) I want to do Y. if none of the above, DO Z. I have the necessary code for X,Y and Z. Im unable to get this comparison sorted. Please help..!
答案1
得分: 0
你可以在FEATURE
和POPULAR
的XML中获取所有带有属性pageNo
的p
标签的页码,并将它们存储在相应的映射中。
可以使用Go中的XPath解析器和XPath //FEATURES/p[@id='pageNo']
以及//POPULAR/p[@id='pageNo']
。
然后在循环遍历其他页面时,获取页面号并检查它是否存在于feature-map或popular-map中,分别执行X、Y或Z操作。
Playground: go.dev/play/p/GzzHAPfhd1D
使用了:github.com/go-xmlpath/xmlpath
英文:
You can get all page numbers(all p tags with attribute pageNo) in FEATURE
and POPULAR
xml and store them in respective maps.
Can use xpath //FEATURES/p[@id='pageNo']
and //POPULAR/p[@id='pageNo']
with xpath parser in Go.
Then while looping through other pages, get the page number and check if it is present in feature-map or popular-map or none, to do X Y or Z repectively.
Playground: go.dev/play/p/GzzHAPfhd1D
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论