Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using XSLT in a concise way to modify XML elements

I have the xml file that I totally didn’t get from somewhere else

<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

As a test, I want to modify all the prices to the nonsense "i win"

And so I wrote the following XSLT file

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match = "/bookstore/book/price">
        <price>i win</price>
    </xsl:template>
</xsl:stylesheet>

I then use the following vb.net code to parse it


Module Module1

    Sub Main()
        Dim xslt = New XslCompiledTransform
        dim curDir = My.Computer.FileSystem.CurrentDirectory
        xslt.Load(curDir & "\testtransform.xsl")
        xslt.Transform(curDir & "\testinv2.xml",curDir & "\outinv.xml")
    End Sub

End Module

When I ran this, outinv.xml contained the following

<?xml version="1.0" encoding="utf-8"?>
  
    The Autobiography of Benjamin Franklin
    
      Benjamin
      Franklin
    
    <price>i win</price>
  
  
    The Confidence Man
    
      Herman
      Melville
    
    <price>i win</price>
  
  
    The Gorgias
    
      Plato
    
    <price>i win</price>

So essentially it thanos snapped all the tags that weren’t those specific price tags, which I think makes sense. My question is whether there is a way through xslt to modify those tags and keep everything else the same, without having to make templates for every single possible tag that I expect. Thanks

>Solution :

In XSLT 3 you can declare <xsl:mode on-no-match="shallow-copy"/>. You would need to use Saxon for XSLT 3, Microsoft doesn’t support it.

For an XSLT 1.0 processor you would need to add "the identity transformation template" to your code:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading