adding new data to XML file, updating attribute counter, via XSLT

I want add some data to the end of an existing XML file.
In a simplified form my original XML file looks like this

<?xml version="1.0"?>
<file>
  <table>
    <comment/>
    <xref>yes</xref>
    <test key="1">
      <name>nameA</name>
      <type>type7</type>
    </test>
    <test key="2">
      <name>nameC</name>
      <type>type9</type>
    </test>
  </table>
</file>

Open in new window


I want to add a new test element group with name=nameG and type=type4
I want to add it after all the existing test elements
I don't know how many test elements there are in the original XML files although i do know that value of the key attribute will match the test element count i.e. 3rd test element would have key="3"

So, knowing that I want to add a  new test element group with name=nameG and type=type4 I would want to end up with this:

<?xml version="1.0"?>
<file>
  <table>
    <comment/>
    <xref>yes</xref>
    <test key="1">
      <name>nameA</name>
      <type>type7</type>
    </test>
    <test key="2">
      <name>nameC</name>
      <type>type9</type>
    </test>
    <test key="3">
      <name>nameG</name>
      <type>type4</type>
    </test>
  </table>
</file>

Open in new window


I need a way to add this new information in via XSLT.
Can anyone point me in the right direction of how to do this?

Thanks.
LVL 1
LetterpartAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
In you xslt stylesheet add one template that does an identity transform

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

Open in new window

0
Geert BormansInformation ArchitectCommented:
If you now execute the xslt, you would get the source copied exactly as the result
Now we can add a specialisation for test ( a new template that will overrule the existing only for the test element
0
Geert BormansInformation ArchitectCommented:
<xsl:template match="test">
   <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="node()"/>
   </xsl:copy>
<xsl:if test="not(following-sibling::test)">
   Add your element here
</xsl:if>
</xsl:template>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LetterpartAuthor Commented:
Excellent - thanks very much Geert for the quick response, solution, and explanation!
0
Geert BormansInformation ArchitectCommented:
Welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.