Get the first child node in XSLT using local-name ()

Suppose we have this simple xml ...

 <books>   
    <book>
       <author/>
       <title/>
    </book>
    <book>
       <author/>
       <title/>
    </book>
 </books>

I use this xpath to get the elements of the first copy of the book.

//books[1]/*

Returns

<author/>
<title/>

And this works fine, but I need to get it to work using local-name (). I tried the following, but none of these work ...

//*[local-name()='books']/*

this returns repeating author elements and titles, not good ones, I only need them from the first child

//*[local-name()='books'][0]/*

it returns nothing

Basically, I want to create a CSV file, so the first line in the output will be a heading that lists the names of the attributes of the book, followed by arbitrary data values. I only need to get part of the header.

author,title
john,The End is Near
sally,Looking for Answers
+5
3

//books[1]/*

( ) <books> node. <books> , ,

/books/*

<book> , , , node.

, , local-name node, /*,

/*/*[1]

node <books> node - ,

//*[local-name()='books']/*[1]

, , XPath // , , node .

+9

FAQ - XPath [] (), - //.

:

//someElemName[1]

someElemName, , , XML, .

, .

(//*[local-name() = 'book'])[1]/*

. XPath 1, 0.

XSLT:

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

 <xsl:template match="/">
  <xsl:copy-of select=
  "(//*[local-name() = 'book'])[1]/*"/>
 </xsl:template>
</xsl:stylesheet>

, XML-:

<books>
    <book num="1">
        <author num="1"/>
        <title num="1"/>
    </book>
    <book num="2">
        <author num="2"/>
        <title num="2"/>
    </book>
</books>

:

<author num="1"/>
<title num="1"/>
+12

I have to face the same problems. I decided:

//*[local-name()='MYNODENAME' and position()=X]

A good day.

0
source

All Articles