循环遍历页面编号以获取XML数据。

huangapple go评论87阅读模式
英文:

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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;pam:article xml:lang=&quot;en-US&quot;&gt;
&lt;FEATURES&gt;
&lt;p id=&quot;title&quot;&gt;&quot;WITH A SONG IN MY HEART�&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;6&lt;/p&gt;
&lt;p&gt;The screen version of the Jane Froman story performed in brilltant technicolor.&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;LES PAUL AND MARY FORD&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;12&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;SONG OF THE MONTH&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;14&lt;/p&gt;
&lt;/FEATURES&gt;
&lt;POPULAR&gt;
&lt;p id=&quot;title&quot;&gt;A GARDEN IN THE RAIN&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;10&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;A HEART FULL OF LOVE&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;17&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;A WEAVER OF DREAMS&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;13&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;ALLEGHENY FIDDLER&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;15&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;ANY TIME&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;7&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;AT LAST! AT LAST!&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;3&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;BABY, WE&#39;RE REALLY IN LOVE&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;16&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;BE MY-LIFE&#39;S COMPANION&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;15&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;CALL ME A DREAMER&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;15&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;CHINATOWN, MY CHINATOWN&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;16&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;CLOSER YOU ARE, THE&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;13&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;COME WHAT MAY&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;11&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;CRY&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;3&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;DANCE ME LOOSE&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;5&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;DID ANYONE CALL&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;15&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;8TH STREET ASSOCIATION&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;16&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;ETERNALLY&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;8&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;EV&#39;RY DAY&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;16&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;GENTRE HANDS&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;10&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;GET HAPPY&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;6&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;GOODBYE SWEETHEART&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;3&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;GRAND CENTRAL STATION&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;8&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;HEART OF A CLOWN, THE&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;9&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;IF YOU GO&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;17&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;I&#39;LL ALWAYS BE FOLLOWING YOU&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;8&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;I&#39;LL STILL LOVE YOU&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;8&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;I&#39;LL WALK ALONE&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;4&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;IT&#39;S RAINING&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;15&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;LITTLE WHITE CLOUD THAT CRIED, THE&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;7&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;MAKE WITH THE MAGIC&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;10&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;MARSHMALLOW MOON&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;9&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;MOTHER AT YOUR FEET IS KNEELING&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;5&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;NECESSARY EVIL&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;13&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;NEITHER AM I&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;9&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;OLD PAINT (Ride Around, Little Dogies)&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;7&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;ON MY WAY (Got My Travelin� Shoes)&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;5&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;OOH! WHAT YOU DID!&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;14&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;PLAY ME A HURTIN� TUNE&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;8&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;PLEASE MR. SUN&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;3&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;RETREAT&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;5&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;RIVER, STAY �WAY FROM MY DOOR&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;10&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;SAD&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;14&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;SHRIMP BOATS&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;10&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;SILLY DREAMER&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;10&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;SINCE MY LOVE HAS GONE&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;3&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;SLOWPOKE&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;7&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;SWEETHEART SERENADE, THE&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;16&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;TAKE ME BACK&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;11&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;TAKE ME HOME&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;11&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;TELL ME WHY&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;14&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;THIS IS MY NIGHT TO DREAM&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;11&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;TULIPS AND HEATHER&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;7&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;UNDECIDED&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;14&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;UNTIL&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;3&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;WE WON&#39;T LIVE IN A CASTLE&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;17&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;WHEEL OF FORTUNE&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;3&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;WHEN I TAKE MY SUGAR TO TEA&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;3&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;WHERE I MAY LIVE WITH MY LOVE&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;8&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;WIMMIN&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;10&lt;/p&gt;
&lt;p id=&quot;title&quot;&gt;YOU&#39;RE GONNA LOVE SOMEBODY, SOMEDAY&lt;/p&gt;
&lt;p id=&quot;pageNo&quot;&gt;10&lt;/p&gt;
&lt;/POPULAR&gt;
&lt;/pam:article&gt;

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

你可以在FEATUREPOPULAR的XML中获取所有带有属性pageNop标签的页码,并将它们存储在相应的映射中。

可以使用Go中的XPath解析器和XPath //FEATURES/p[@id=&#39;pageNo&#39;]以及//POPULAR/p[@id=&#39;pageNo&#39;]

然后在循环遍历其他页面时,获取页面号并检查它是否存在于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=&#39;pageNo&#39;] and //POPULAR/p[@id=&#39;pageNo&#39;] 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

Uses: github.com/go-xmlpath/xmlpath

huangapple
  • 本文由 发表于 2022年11月21日 23:07:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/74520969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定