簡體   English   中英

從 r 中的 xml 文件中刪除父節點

[英]Removing parent nodes from xml file in r

我下面有 xml output。 我正在嘗試使用 XML 或 R 中的 xml2 庫刪除父母。我在這里找到了類似的帖子,但這些函數不知何故給了我一個錯誤。 我試圖刪除那些父母,直到analysisdata父母。 這就是我要解析的內容。

<assessment>
  <itembank />
  <instrument id="797393  ">
    <form id="Math G3">
      <formitem id="8243" position="0" />

    </form>
  </instrument>
  <admin id="797393  " instrument="797393  ">
    <genatt name="adminperiod" value="SP19" />
    <responseanalyses>
      <responsebankref id="3M" />
      <analysisdefs>
        <analysisoption name="mark" value="no" />
        <analysisoption name="notreached" value="ASNOTPRESENTED" />
        <statentity type="item" id="8243" varname="I_8243">
          <genatt name="type" value="CR" />
          <genatt name="codestart" value="0" />
          <genatt name="validcodes" value="2" />
          <genatt name="omitcode" value="9" />
          <genatt name="notpresentedcode" value="8" />
          <statentity code="0.000000" id="0.000000" type="itemscorept" />
          <statentity code="1.000000" id="1.000000" type="itemscorept" />
          <statentity code="9" id="omit" type="itemscorept" />
        </statentity>

          <genatt name="type" value="CR" />
          <genatt name="codestart" value="0" />
          <genatt name="validcodes" value="2" />
          <genatt name="omitcode" value="9" />
          <genatt name="notpresentedcode" value="8" />
          <statentity code="0.000000" id="0.000000" type="itemscorept" />
          <statentity code="1.000000" id="1.000000" type="itemscorept" />
          <statentity code="9" id="omit" type="itemscorept" />
        </statentity>
        <statentity id="DIFGROUP_TTS" varname="DIFGROUP_TTS" type="popgroup" description="DIFGROUP_TTS">
          <statentity code="0.000000" id="0.000000" description="0.000000" samplesize="40501" type="popsubgroup" />
          <statentity code="1.000000" id="1.000000" description="1.000000" samplesize="5532" type="popsubgroup" />
        </statentity>

      </analysisdefs>
      <genatt name="xmloutputfile" value="J:\out.xml" />
      <analysis creator="ST" description="" level="form" type="classical">
    <analysisdata formid="Math G3">

          <statentityref id="8243" type="item">
            <statval type="OmitCount" value="0.000000" />
            <statval type="NotReachedCount" value="43.000000" />
            <statval type="PolyserialCorrelation" value="0.644181" />
            <statval type="AdjustedPolyserial" value="0.644181" />
            <statval type="AverageScore" value="0.223074" se="0.011957" />
            <statval type="NCount" value="2609.000000" />
            <statval type="StdevItemScore" value="0.416308" />
              </statentityref>

            </statentityref>
          </statentityref>
        </analysisdata>
      </analysis>
    </responseanalyses>
    <responsebank findin="J:\outAM.am" sourcetype="AM" id="3M" />
  </admin>
</assessment>

我嘗試了 library(xml2) library(XML) newdoc <- xpathApply(doc, "/assessment/admin/responseanalyses")運行后出現此錯誤: Error in UseMethod("xpathApply"): no applicable method for 'xpathApply' applied to an object of class "c('xml_document', 'xml_node')" 此處建議了此解決方案。

刪除那些父節點后,我想只有在刪除評估/管理/響應分析父節點后才有analysisdata節點。 想要的出來是這樣的:

<analysisdata formid="Math G3">
  <statentityref id="8243" type="item">
    <statval type="OmitCount" value="0.000000" />
    <statval type="NotReachedCount" value="43.000000" />
    <statval type="PolyserialCorrelation" value="0.644181" />
    <statval type="AdjustedPolyserial" value="0.644181" />
    <statval type="AverageScore" value="0.223074" se="0.011957" />
    <statval type="NCount" value="2609.000000" />
    <statval type="StdevItemScore" value="0.416308" />
      </statentityref>

    </statentityref>
  </statentityref>
</analysisdata>

有什么想法嗎? 謝謝!

您可以使用html_node庫中的rvest

 rvest::html_node(doc, 'analysisdata')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM