1

Dear Stackoverflow Community

I searched so long but I can't finde any thing about this topic maybe one of you here can help me.

I search for a XSLTransform query which allows me to find and get a file by its name.

I try to search and put more information in to a Text File for example the Path: C:/ErrorLogs/2019-03/Day1.txt

I thank you in advance.

Best regards PassCody

Btw. I write in XSLT-2.0

----EDIT----

I writen the sorce now and It works dynamic but if there came a new input the Error Message will be apears behind the first xD Maybe someone of you guys can help me with this problem.

For example: ErrorMessage1ErrorMassage2

My goal: ErrorMessage1 [TAB] ErrorMessage2

2 Answers2

2

If you know the name of an XML file then you can convert the filename to a URI and supply it to the doc() or document() functions.

"Finding" a file if you don't know its name is a bit more of a challenge; it depends what information you are starting with. The collection() function is available in XSLT 2.0; the specification describes it in very abstract terms, but many implementations provide some search capabilities here: see for example

https://www.saxonica.com/html/documentation/sourcedocs/collections.html

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • 1
    Hi Michael, I will try this. Thanks for your quick answer and help. –  Mar 07 '19 at 07:26
0

Hi PassCody,

First of all welcome to StackOverflow:D!

I have a few questions for you:

  • What do you want to do with the file? Do you want to relocate/rename it, or do you want to perform certain operations on the contents of the file?
  • In what kind of mapstructure is the file located? Can you provide an example? Are there multiple files in the same folder?

This information is necessary to give you a correct answer. The more you provide, the better the answer will help your specific situation.

For example, if you would want to get the name of an xml file in your current location. You could include:

<URL:http://www.w3.org/TR/xpath-functions/#func-document-uri>
<xsl:value-of select="document-uri(/)"/>

This will provide you with the full URI (location) of your file, to get the filename you need to select the string after the last "/". You could use sub-string functions to specify parts of string that you would want to use.

If you can include the path of your files in a variable (see this question) you could loop over files in your current directory to create a new file with the same content, which you could write to a new location or do all other kinds of things with.

Lets just say that there are a lot of fun things you can do with xslt, but people will need a bit more info to get to your specific case :). A lot of usefull XSLT functions can be found on this website. I'm sure if you play around this website for a bit, you will be able to find whatever you are looking for as well.

I hope this helps! Have a nice day.

Jesper

Edit: I have changed some of my answer to not include erroneous information.

Edit: Answer to question in the comments: You can add a "new line" part in your xslt within your code loop. Add <xsl:text>&#10;</xsl:text> or <xsl:text>&#xa;</xsl:text>. Either of the two will work.

Instead of:
ErrorMessage1ErrorMessage2,

You will get:
ErrorMessage1 ErrorMessage2

Jesper Vernooij
  • 173
  • 1
  • 15
  • 1
    How would `xsl:for-each select="*.xml"` "loop over files"? The select expression `*.xml` would just give an XPath syntax error. – Martin Honnen Mar 06 '19 at 12:37
  • It does indeed, I edited my answer to which I think it will not error out. Feel free to correct me if it does though! :) – Jesper Vernooij Mar 06 '19 at 13:40
  • 1
    The syntax error that `*.xml` alone produces does not vanish by prefixing it with `pathToYourFiles/`. I am not sure in what way you think you can put a URI or file pattern into the `select` attribute of `xsl:for-each`, but files are not loaded or searched that way in XSLT/XPath. Do you have the `collection` function in mind that Michael Kay mentioned in his answer? There, depending on the XSLT processor (Altova?), a syntax like `select="collection('pathToYourFiles/*.xml')"` might work. – Martin Honnen Mar 06 '19 at 14:21
  • I have once more edited my post to skip over bad suggestions and keep it to the point. Thanks for pointing out the bad stuff. – Jesper Vernooij Mar 06 '19 at 15:04
  • 1
    Hello Jesper and Martin, A1: I try to search and put more information in to a Text File for example the Path: C:/ErrorLogs/2019-03/Day1.txt I writen the sorce now and It works dynamic but if there came a new input the Error Message will be apears behind the first xD maybe some one of you guys can help me with this problem. –  Mar 07 '19 at 07:24
  • 1
    For example: ErrorMessage1ErrorMassage2 My goal: ErrorMessage1 [TAB] ErrorMessage2 –  Mar 07 '19 at 08:19
  • I answered your comment in a question edit, you might want to edit your question to encorporate your current example. It might help future readers :) – Jesper Vernooij Mar 07 '19 at 09:03
  • I'm glad to hear! Please consider to accept either Michael's or my answer :). – Jesper Vernooij Mar 07 '19 at 10:14
  • 1
    *Thanks for your awnser of my comment/edit question. It works fine now with thanks for your help ;-) –  Mar 07 '19 at 10:51