0

Here's my data structure in XML:

<MyFile>
<Body>
    <Data>
        <row AW="1" AX="SPC" AY="011" AZ="" BA="5" BB="38.482" />
        <row AW="2" AX="CDR" AY="011" AZ="" BA="8" BB="39.812" />
        <row AW="3" AX="FFD" AY="011" AZ="" BA="9" BB="41.115" />
    </Data>
</Body>

Actually there're not only AW~BB, but A~Z+AA~AZ+BA~BZ....more then 100 attribute, If I want select all attribute start with B, that is B+BA~BZ, In this case, there're only BA & BB, with newline after every row ends that is:

5;38.482;
8;39.812;
9;41.115;

how do I do that? Here's how I got so far:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes"/>
  <xsl:template match="/">
    <xsl:variable name="headers1" 
                  select="MPIFile/Body/Data/row/@*
                          [starts-with(name(), 'B')]"/>
       <xsl:for-each select='$headers1'>
         <xsl:value-of select="."/> 
         <xsl:text>;</xsl:text>
      </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

But It output as

5;38.482;8;39.812;9;41.115;

It's not right, how to fix this? thanks a lot

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
David4866
  • 43
  • 7
  • 3
    Possible duplicate of [Producing a new line in XSLT](https://stackoverflow.com/questions/723226/producing-a-new-line-in-xslt) in particular [this answer](https://stackoverflow.com/a/3522008/208809) – Gordon Nov 01 '17 at 16:55
  • I liked to add the code for a new line in a variable that I call "new_line". I find it clearer to call `` than to have the hex code each time (refer to proposed duplicate question for that code). – AntonH Nov 01 '17 at 17:01
  • First thing you should do is set up a template for `row` elements and apply templates to the `row`s so you can operate on each `row` as a separate piece. – JLRishe Nov 01 '17 at 19:09

1 Answers1

0

In your output, you want something to happen once for every row in the input. As a basic principle of software construction, that should suggest to you that you want some piece of your XSLT program to be evaluated once for every row in the input.

But you have no part of your transform that can be described as handling exactly one row of the input. So there is no place in your current sketch where you could put the newline so as to have it inserted once per row.

Consider a structure like this:

<xsl:stylesheet ... >
  <xsl:output method="text"/>

  <xsl:template match="row">
    <xsl:apply-templates select="@*[starts-with(name(), 'B')]"/>
    <xsl:message>We have just done one row!</xsl:message>
    ... other end-of-row code here ...
  </

  <xsl:template match="@*">
    <xsl:value-of select="concat(., ';')"/>
  </

</ 

Now it should be obvious to you where to put an XSLT expression to generate a newline in your output. And if you consult the question marked as a near-duplicate, you should know how to write such an expression.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65