Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT remove source's stylesheet

Tags:

xml

xslt

I have my XSLT working, except I can't get it to remove (to not copy, to delete) the source file's <?xsl-stylesheet... element/directive. Here's what I have for my XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
   <xsl:output method="xml" indent="yes"/>
   <xsl:template match="@* | node()">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="xsl:stylesheet"/>

   <xsl:template match="TestCase">
    ... remainder of file

I've tried it with and without a "?", with and without the "xsl:" portion in the match attribute, no luck. (So, I've tried the "match" being "xsl:stylesheet", "?xsl:stylesheet", "stylesheet".) The xml source starts off like this:

    <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    <?xml-stylesheet type="text/xsl" href="../../testUtil testLogToHtmlDisplay.xsl" ?>
    <TestSuite Name="APIC2EChartAddRoute">

TIA.

like image 535
MJF Avatar asked Feb 10 '26 03:02

MJF


1 Answers

Anything starting <? (other then the <?xml version=...?> declaration) is a processing instruction, which you can match with a pattern of processing-instruction('name') (for a specific <?name ...?>) or just processing-instruction() (for any PI, regardless of name, the same way you would use * for any element node):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
   <xsl:output method="xml" indent="yes"/>
   <xsl:template match="@* | node()">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="processing-instruction('xml-stylesheet')"/>

   <xsl:template match="TestCase">
    ... remainder of file
like image 85
Ian Roberts Avatar answered Feb 15 '26 19:02

Ian Roberts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!