BaseX XQuery 内容问题:无法添加属性,也不知道如何添加标签,只知道如何添加值

问题描述 投票:0回答:1

PS-请告诉管理员启用更多可用性来审查代码缩进,任务太粗糙,必须使用链接,并且当代码格式有多种时它会强制使用格式,我必须删除 javadoc

我过去常常避免在这里提问,因为我已经提出了一个关于 mysql 错误的问题,最终成为一个不完整的查询,但这次我无法找到用于我的研究的 BaseX 文档,而且我遇到了问题。

理论上,在理论上会出现 xml 本机 dbms 和支持 xml 的 dbms 的历史记录和信息,然后在摘要中出现 xquery 与 BaseX 的语法以及更新插入等语法。

但是,缺少如何添加属性。我老师的回答更令人困惑,因为他告诉我,当使用baseX修改属性时,我必须在拍摄中修改整个xml,并且他提到了我在“Utilitats.xml”(utils)中的代码,其中他用 jaxb 代替,这让我更困惑。

好吧,问题是,添加属性的正确语法应该是哪种?我添加基类、类数据处理程序,我在插入方法和测试文件中遇到问题

顺便说一句,正如我在类似的答案中看到的那样 - 不,老师不会让我使用 XPath 来代替。

关于标题中所述的节点,我不明白如何添加标签/节点以同时在其中添加值,或者如何首先添加节点以及如何让系统知道 xquery 的新节点必须插入值,因为我没有找到如何检查标签内没有值或标签,并且 count 会返回所有相同的标签。 它在带有 Netbeans 和 OpenJDK 11 的 LXDE 虚拟机中使用,这是唯一合适的 jdk,API 是 Basex。 我很难在网上找到关于此的信息,因为它显然已经过时了。 另外,请记住,我没有设法检查添加节点的尝试,在我在网上找到的一些文档之后,可能错过了它们上的节点词 .

我认为唯一相关的问题是第一个出现在我面前的问题,我认为它并没有澄清我的困惑,因为它使用了 for-return,我不知道它如何用于显示内容、替换或插入。

我尝试使用 xquery 添加属性,但 basex 中的语法文档不完整,我错过了网络中的信息。我期望添加该属性,然后在管理器中的方法 inserir 中同时添加节点的值

基类(模型)

`
package eac4.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name ="ciutat")
//@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(propOrder = {"nom", "latitud", "longitud", "superficie", "alcada", "habitants", "patro", "llocsInteres"})
public class Ciutat {

    private String nom;
    private double latitud;
    private double longitud;
    private double superficie;
    private int alcada;
    private int habitants;
    private String patro;
    private final ArrayList<String> llocsInteres = new ArrayList<>();

    
    public Ciutat() {
        
    }
    
    public Ciutat(String nom, double latitud, double longitud, double superficie, int alcada, int 
    habitants, String patro, List<String> llocsInteres) {
        this.nom = nom;
        this.latitud = latitud;
        this.longitud = longitud;
        this.superficie = superficie;
        this.alcada = alcada;
        this.habitants = habitants;
        this.patro = patro;
        this.llocsInteres.addAll(llocsInteres);
    }

    @XmlAttribute
    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    @XmlElement
    public double getLatitud() {
        return latitud;
    }

    public void setLatitud(double latitud) {
        this.latitud = latitud;
    }

    @XmlElement
    public double getLongitud() {
        return longitud;
    }

    public void setLongitud(double longitud) {
        this.longitud = longitud;
    }

    @XmlElement
    public double getSuperficie() {
        return superficie;
    }

    public void setSuperficie(double superficie) {
        this.superficie = superficie;
    }

    @XmlElement
    public int getAlcada() {
        return alcada;
    }

    public void setAlcada(int alcada) {
        this.alcada = alcada;
    }

    @XmlElement
    public int getHabitants() {
        return habitants;
    }

    public void setHabitants(int habitants) {
        this.habitants = habitants;
    }

    @XmlElement
    public String getPatro() {
        return patro;
    }

    public void setPatro(String patro) {
        this.patro = patro;
    }

    @XmlElementWrapper(name = "llocs_interes")
    @XmlElement(name = "lloc_interes")
    public ArrayList<String> getLlocsInteres() {
        return llocsInteres;
    }

    public void setLlocsInteres(List<String> llocsInteres) {
        this.llocsInteres.clear();
        this.llocsInteres.addAll(llocsInteres);
    }

    @Override
    public int hashCode() {
        int hash = 3;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Ciutat other = (Ciutat) obj;
        if (Double.doubleToLongBits(this.latitud) != Double.doubleToLongBits(other.latitud)) {
            return false;
        }
        if (Double.doubleToLongBits(this.longitud) != Double.doubleToLongBits(other.longitud)) {
            return false;
        }
        if (Double.doubleToLongBits(this.superficie) != Double.doubleToLongBits(other.superficie))       
        {
            return false;
        }
        if (this.alcada != other.alcada) {
            return false;
        }
        if (this.habitants != other.habitants) {
            return false;
        }
        if (!Objects.equals(this.nom, other.nom)) {
            return false;
        }
        if (!Objects.equals(this.patro, other.patro)) {
            return false;
        }
        return Objects.equals(this.llocsInteres, other.llocsInteres);
    }
    
    
}`

Manager类(方法为inserir,错误位于exprInsert.executeQuery(comandaNom)中的inserir;

`package eac4.gestors;

import eac4.model.Ciutat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.xquery.XQConnection;
import javax.xml.xquery.XQException;
import javax.xml.xquery.XQExpression;


public class GestorCiutat {

    //arrel del document (permet simplificar les expressions)
    public static final String ARREL = "doc(\"ciutats/ciutats.xml\")/collection(\"ciutats\")/";

    private final XQConnection conn;

    public GestorCiutat(XQConnection conn) {
        this.conn = conn;
    }

    public void eliminarTot() throws GestorException {
        try {
            String consulta = "delete node " + ARREL + "ciutats/ciutat";
            XQExpression exp = conn.createExpression();
            exp.executeQuery(consulta);
        } catch (XQException ex) 
        {
            Logger.getLogger(GestorCiutat.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(ex.getMessage());
            throw new GestorException("Error en esborrar els ciutats", ex);
        }
    }

    public void inserir(Ciutat ciutat) throws GestorException 
    {
        
        try
        {
             
            String comandaNom = "update insert node attribute nom {'" + ciutat.getNom() + "'} into 
            ciutats/ciutat";
            String comandaLatitud = "update insert " + String.valueOf(ciutat.getLatitud()) + " into 
            ciutats/ciutat[@nom[.=" + ciutat.getNom() + "]/latitud";
            String comandaLongitud = "update insert " + String.valueOf(ciutat.getLongitud()) + " 
            into ciutats/ciutat[@nom=" + String.valueOf(ciutat.getNom()) + "]/longitud";
            String comandaSuperficie = "update insert " + String.valueOf(ciutat.getSuperficie()) + 
            " into ciutats/ciutat[@nom=" + String.valueOf(ciutat.getNom()) + "]/superficie";
            String comandaAlcada = "update insert " + String.valueOf(ciutat.getAlcada()) + " into 
            ciutats/ciutat[@nom=" + String.valueOf(ciutat.getNom()) + "]/alcada";
            String comandaHabitants = "update insert " + String.valueOf(ciutat.getHabitants()) + " 
            into ciutats/ciutat[@nom=" + String.valueOf(ciutat.getNom()) + "]/habitants";
            String comandaPatro = "update insert " + ciutat.getPatro()+ " into ciutats/ciutat[nom=" 
            + String.valueOf(ciutat.getNom()) + "]/patro";
        
            
            XQExpression exprInsert = conn.createExpression();
            exprInsert.executeQuery(comandaNom);
            exprInsert.executeQuery(comandaLatitud);
            exprInsert.executeQuery(comandaLongitud);
            exprInsert.executeQuery(comandaSuperficie);
            exprInsert.executeQuery(comandaAlcada);
            exprInsert.executeQuery(comandaHabitants);
            exprInsert.executeQuery(comandaPatro);
            exprInsert.executeQuery("update insert " + ciutat.getLlocsInteres().indexOf(0) + " 
            into ciutats/ciutat[nom=" + String.valueOf(ciutat.getNom()) + "]/llocsinteres/lloc")
            for(int iterPos = 1; iterPos < ciutat.getLlocsInteres().size(); iterPos++)
            {
                exprInsert.executeQuery("update insert lloc/" + 
                ciutat.getLlocsInteres().indexOf(iterPos) + " following ciutats/ciutat[nom=" + 
                String.valueOf(ciutat.getNom()) + "]/llocsinteres/lloc[position=" + iterPos+"]");
            }
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
            e.printStackTrace(System.out);
            throw new GestorException("Error no gestionat al inserir una ciutat!");
        }       
    }

    public void eliminar(String nom) throws GestorException {
        //TODO
        throw new UnsupportedOperationException("Mètode no implementat");
    }

    public Ciutat obtenirCiutat(String nom) throws GestorException {
        //TODO
        throw new UnsupportedOperationException("Mètode no implementat");
    }

    
    public List<Ciutat> obtenirCiutatAmbLlocInteres(String llocInteres) throws GestorException {
        //TODO
        throw new UnsupportedOperationException("Mètode no implementat");
    }

}`

测试(它给了我很多格式问题)

https://pastebin.com/z2h4iAtn

错误:

`
 javax.xml.xquery.XQException:
  XQJQS001 - Invalid XQuery syntax, syntax does not pass static validation.
  javax.xml.xquery.XQException:
  XQJQS001 - Invalid XQuery syntax, syntax does not pass static validation.
  Root Cause:
  net.xqj.core.xqparser.v30.ParseException: Encountered " "insert" "insert "" at line 1, column 8.
Was expecting one of:`

部分堆栈跟踪:

 `at net.xqj.core.xqparser.XQueryStatement.compile(XQueryStatement.java:269)
  at net.xqj.core.XQExpression3.executeQuery(XQExpression3.java:89)
  at net.xqj.core.XQExpression3.executeQuery(XQExpression3.java:171)
  at eac4.gestors.GestorCiutat.inserir(GestorCiutat.java:71)
  at eac4.gestors.TestGestors.testInserir(TestGestors.java:155)`
xml attributes basex
1个回答
0
投票

这就是 XQuery 3.0 及更高版本。

在 BaseX 10.8 beta 中进行测试。

XQuery

declare context item := document {
<root>
  <city>Miami</city>
  <city>Dallas</city>
</root>
};

copy $input := .
modify insert node attribute state {'FL'} into $input/root/city[1]
return $input

输出

<root>
  <city state="FL">Miami</city>
  <city>Dallas</city>
</root>
© www.soinside.com 2019 - 2024. All rights reserved.