Question

I have an XML document that have additonal nodes in it at different levels that I want to remove via XSLT.

My intuition is to generate an XSD (using Altova or the like), remove the elements I don't want to appear in my output, then let the editor (Altova or the like) auto generate an XSLT to transform the old XSD into the new XSD.

Back in the day, I used to write XSL for this by hand...but with all these cool tools, is there really a reason to or can someone suggest an alternative way? I haven't done this stuff in a couple of years, so I figured I would ask...

I ended up coming up with this, which is pretty simple, so thanks for the suggestion:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" >
<xsl:output method="xml" indent="yes"/>

  <xsl:template xpath-default-namespace="http://www.tempuri.org" match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template xpath-default-namespace="http://www.tempuri.org" match="SomeNode">
  </xsl:template>
  <xsl:template xpath-default-namespace="http://www.tempuri.org" match="TheNode[@type!='SomeType' and @type!='OtherType']">
  </xsl:template>

</xsl:stylesheet>

The one remaining thing I need to do is check for a missing element "SomeSubElement" of "TheNode" and insert an empty element if it's missing.

Any suggestions on how to do this? Thanks.

Was it helpful?

Solution

A simple identity transform with empty templates for what you want to discard:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  version="2.0">

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="tagToBeRemoved"/>

</xsl:stylesheet>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top