【问题标题】:Seperate XML content from a single XML file using XQuery使用 XQuery 从单个 XML 文件中分离 XML 内容
【发布时间】:2015-03-27 15:26:59
【问题描述】:

我有一个包含多个 XML 节点的 XML 文件。我想分开两个 XML 注释并将它们存储在单独的变量中。我将如何使用 XQuery 编写此功能?我在下面添加了我的 XML 文件。在 XML 文件中,我有一个 division 根元素,Divetop-song 是两个子元素。现在我想读取一个变量中的Dive XML 内容和另一个变量中的top-song 内容。谁能帮我解决这个问题?

   <?xml version="1.0" encoding="UTF-8"?>
     <division>
     <Dive ID="2"><!-- I want this node in one variable -->
      <DiverFName>Joe</DiverFName>
      <DiverLName>Diver</DiverLName>
      <Number>2</Number>
      <Divedate>1998-03-30</Divedate>
      <Country ID="1">Bahamas</Country>
      <City ID="2">Freeport</City>
      <Place ID="2">
        <Site>South Pass</Site>
        <Lat>24.865062</Lat>
        <Lon>-77.871094</Lon>
      </Place>
      <Divetime>36.00</Divetime>
      <Depth Scale="METRIC">5.48</Depth>
      <Buddy IDs="2" Names="Tim Diver" />
      <Comments>Great dive, saw 5 Caribbean Reef Sharks.  Performed compass navigation skills for Scuba Diver certification.</Comments>
      <Water>Salt</Water>
      <Entry>Boat</Entry>
      <Divetype>Research</Divetype>
      <Tanktype>Alu</Tanktype>
      <Tanksize>11.43</Tanksize>
      <PresS>179.26</PresS>
      <PresE>82.73</PresE>
      <Gas>Air</Gas>
      <Weather>Clear</Weather>
      <UWCurrent>Medium Current</UWCurrent>
      <MarineLife>
        <Animal>
            <Type>Nurse Shark</Type>
            <Abundance>1</Abundance>
            <Size>3 ft</Size>
            <Description>Dormant on the bottom, not swimming.</Description>
            <Image>
                <Filename></Filename>
                <Path></Path>
                <Caption></Caption>
            </Image>
        </Animal>
        <Animal>
            <Type>Blue Tang Surgeonfish</Type>
            <Abundance>25+</Abundance>
            <Size>4 in</Size>
            <Description>Blue with white "scalpel" near base </descreption>
            <Image>
                <Filename></Filename>
                <Path></Path>
                <Caption></Caption>
            </Image>
        </Animal>
      </MarineLife>
    </Dive>
<top-song><!-- I want this node in another variable -->
 <title >Try Again</title>
  <artist >Aaliyah</artist>
  <weeks last="2008-06-17">
    <week>2008-06-17</week>
  </weeks>
  <album> The
  Album</album>
  <released>February 29, 20008</released>
  <formats>
    <format>CD</format>
    <format>12 single</format>
  </formats>
  <recorded>january2012</recorded>
  <genres>
    <genre>R&amp;B</genre>
  </genres>
  <lengths>
    <length>4:04</length>
  </lengths>
  <label>Blackground</label>
  <writers>
    <writer></writer>
    <writer></writer>
  </writers>
  <producers>
    <producer></producer>
  </producers>
  <descr>
   <p>hai hello</p>
  </descr>
</top-song>
</division>

【问题讨论】:

  • 请分享您的代码,即使已损坏..

标签: xpath xquery marklogic


【解决方案1】:

目前尚不清楚您要在高层次上完成什么,但您可以使用一些简单的 XQuery/Xpath 选择这些元素:

let $dive := doc('mydoc.xml')/division/Dive
let $top-song := doc('mydoc.xml')/division/top-song

但是,仅查看文档就可以清楚地看出,这两个元素处于完全不相关的模式中,并且作为 MarkLogic 的一般建议,它们可能应该在摄取之前将它们分开并作为单独的文档插入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多