XML text formatting for XSL conversion

I hope that my title will justify this question. Please consider the following XML block and sample XSL block.

<root>
<level_one>
My first line of text on level_one
<level_two>
My only line of text on level_two
</level_two>
My second line of text on level_one
</level_one>
</root>

<xsl:template match="level_one">
<xsl:value-of select="text()"/>
<br/>
<xsl:apply-templates select="level_two"/>
</xsl:template>

<xsl:template match="level_two">
<xsl:value-of select="text()"/>
<br/>
</xsl:template>

In its current form, the output (modified here for reading) when performing the above

My first line of text on level_one
<br/>
My only line of text on level_two
<br/>

I miss the second line of text on level_one. Therefore, I am interested in two things.

  • Is XML Valid? From what I know, the answer is yes, but am I wrong?
  • How to change XSL to get the second line (or even more lines in my case than I showed)?

thank

+3
source share
3 answers

Is XML Valid? From what I know, the answer is yes, but am I wrong?

, XML . , , , ( , ) XML. , XML. , . ( , .)

XSL, ( , )?

, , , , , <xsl:value-of select="text()"/>.

, XML, level_one level_two , text():

  <xsl:template match="text()">
    <xsl:value-of select="."/>
    <br/>
  </xsl:template>

:

  My first line of text on level_one
  <br/>
  My only line of text on level_two
  <br/>
  My second line of text on level_one
  <br/>

, level_one level_two:

  <xsl:template match="level_one/text()|level_two/text()">
    <xsl:value-of select="."/>
    <br/>
  </xsl:template>

, .

, .

0

XSLT xsl: apply-templates.

<xsl:template match="*">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()">
<xsl:value-of select="."/>
<br/>
</xsl:template>

<xsl:value-of select="text()"/> - . XSLT 1.0 node ( ). XSLT 2.0 , , , , , , . ( , , .)

+1

Even without matching the pattern, text()you can output two text()node children of the current node ( level_one) using a replacement :

<xsl:value-of select="text()"/>

with

<xsl:copy-of select="text()"/>

In XSLT 1.0, it is very important to know what <xsl:value-of select="$someNodeSet"/>creates the string value of only the first node (in document order) $someNodeSetnode-set.

On the other hand :

<xsl:copy-of select="$someNodeSet"/>

copies all nodes contained in $someNodeSet.

+1
source

All Articles