开发者

XSLT for creating template XML

I am out of my depth regarding xsl transforms for generating a sample XML file. Basically the process is I am trying to generate a large and complex XML template file from an application generated XSD.

I have used XMLSpy to generate a sample XML file from the XSD my next step is to convert the values to the required values.

example of what I am trying to convert...

<TEST>
  <VALUE1>String</VALUE1>
  <VALUE1>aaaa</VALUE1>
  <VALUE1>aaaaaaaaaaaaa</VALUE1>
</TEST>
<TEST>
  <VALUE1>String</VALUE1>
  <VALUE1>aaaa</VALUE1>
  <VALUE1>aaaaaaaaaaaaa</VALUE1>
</TEST>

into

<TEST>
  <VALUE1>TEST_1.VALUE1_1.text</VALUE1>
  <VALUE1>TEST_1.VALUE1_2.tex开发者_开发问答t</VALUE1>
  <VALUE1>TEST_1.VALUE1_3.text</VALUE1>
</TEST>
<TEST>
  <VALUE1>TEST_2.VALUE1_1.text</VALUE1>
  <VALUE1>TEST_2.VALUE1_2.text</VALUE1>
  <VALUE1>TEST_2.VALUE1_3.text</VALUE1>
</TEST>

Any help is greatly appreciated as I'm new to XML transformations.

Thank you for you feedback, and sorry I wasn't as clear as possible with the request.

In summary I am looking at having some sort of counter for the elements and replacing the value within the XML with the augmented value and count.

i.e. if this is the first time that TEST is found and the first time VALUE1 is found then replace the TAG value with TEST_1.VALUE1_1.text and then repeated throughout the tree structure. So if I have :-

<TEST><VALUE1><SUBVAL1>aaaaa</TEST></VALUE1></SUBVAL1> then I would look at replacing aaaaaa with TEST_1.VALUE1_1.SUBVAL1_1.text (ignoring the aaaaa because I dont care)

I suppose I will ask two questions here :-

  1. Is this possible?
  2. If so would XSLT be the best tool here (if needed I could wrap it with Java and use DOM/SAX to parse and replace - just thought that would be overkill)


  1. Yes it is.
  2. It's an OK tool for the job.

You'll need a container for your TEST elements so you have a single root element. Then the following should do the job.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="node()">
    <xsl:copy>
       <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="text()">
    <xsl:apply-templates select=".." mode="text" />
    <xsl:text>text</xsl:text>
  </xsl:template>
  <xsl:template match="*" mode="text">
    <xsl:apply-templates select=".." mode="text" />
    <xsl:value-of select="local-name()"/>
    <xsl:text>_</xsl:text>
    <xsl:value-of select="count(preceding-sibling::*) + 1"/>
    <xsl:text>.</xsl:text>
  </xsl:template>
  <xsl:template match="/*" mode="text" />
</xsl:stylesheet>


With the sample code you have provided there does not seem to be any relationship between the data values in your source and dest files.

Perhaps you could supply some real (or better fake) data, so we can see what the actual data transform is. (what you show above is a simple substitution only, I am guessing that is not what you really want)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜