1

I'm trying to develop a really simple XML validator based on XIDEL.

Here's my SwiftUI code so far, that executes the embedded XIDEL binary, but I can't figure out how to pass XML that should be validated. My goal is to select a XML file from my computer and show XIDEL results in a content view inside my app.

struct ContentView: View {

@State var message = "Hello, World!"
@State var isRunning = false

var body: some View {
    VStack {
        Text("XML Validator")
            .font(.largeTitle)
            .padding()
        HStack {
            TextField("Message", text: $message)
                .padding(.leading)
            Button(action: {

                let task = Process()
                let bundle = Bundle.main
                let execURL = bundle.url(forResource: "xidel", withExtension: nil)
                guard execURL != nil else {
                    print("XIDEL executable could not be found!")
                    return
                }
                task.executableURL = execURL!
                task.arguments = ["-e=//recipe/flavor1/text() my.xml"]
                do {
                    try task.run()
                    print("XIDEL executed successfully!")
                    self.isRunning = true
                } catch {
                    print("Error running XIDEL: \(error)")
                    self.isRunning = false
                }
                                    
            }) {
                Text("Validate")
            }.disabled(isRunning)
                .padding(.trailing)
        }
    }.frame(maxWidth: .infinity, maxHeight: .infinity)
}

}

enter image description here

Marc H.
  • 57
  • 4

1 Answers1

1

Try the following

    guard let path = Bundle.main.path(forResource: "my", ofType: "xml") else {
       print("my.xml could not be found!")
       return 
    }
    task.arguments = ["-e=//recipe/flavor1/text()", path]
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Is there also a way to do kind of a UNIX pipe? Like so /usr/local/bin/xidel -e="//recipe/flavor1/text() my.xml | awk '!seen[$0]++' | awk '!/NA/' – Marc H. Aug 19 '20 at 12:32