Home > OS >  Read XML-Stylesheet attribute in a XSLT file
Read XML-Stylesheet attribute in a XSLT file

Time:10-27

I need to know if a XML file does have the href attribute from < ?xml-stylesheet type="text/xsl" href="recibo.xsl"? > to determine what kind of Invoice it is (in my XSLT file). I´ve spend quite the time searching but couldn´t find any information on it. Is it even possible to read/acces those attributes in a XSLT?

CodePudding user response:

Reading the processing instruction is easy enough: string(/processing-instruction('xml-stylesheet')). But that won't parse out the "pseudo-attributes" (type and href) in its value. You have to do that yourself, unless your processor provides a vendor extension such as saxon:get-pseudo-attribute.

CodePudding user response:

In XSLT 3 you could try e.g.

<xsl:variable name="stylesheet-href" as="xs:string?" select="parse-xml-fragment('&lt;pi ' || /processing-instruction('xml-stylesheet') || '/>')/pi/@href"/>

to read out the xml-stylesheet processing instruction node and parse its target into attributes from which you extract the href pseudo attribute value.

Note that document-uri(document('')) might also give you the stylesheet URI of the XSLT run.

  • Related