1

I have the following XML node:

<parent>
   <child1name>value</child1name>
   <child2name>value</child2name>
   <child3name>value</child3name>
   <child4name>value</child4name>
   <others />
</parent>

I want to loop through each node with name formatted as text[digit]text. So I did:

<xsl:for-each select="parent/child*name">
   Value <xsl:value-of select="position()" />: <xsl:value-of select="." />
</xsl:for-each>

But it didn't work.

What would be the correct pattern? "child\d{1}name" maybe?

2 Answers 2

1

A correct pattern would be

<xsl:for-each select="parent/*[starts-with(./name(),'child')]">

Else, if you need a tougher restriction:

<xsl:for-each select="parent/*[starts-with(./name(),'child') and ends-with(./name(),'name')]">

Besides, it is not good practice to incorporate text in a stylesheet like this. Rather, you would enclose any text in xsl:text elements.

An entire stylesheet that works with the input snippet you have shown:

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:for-each select="parent/*[starts-with(./name(),'child') and ends-with(./name(),'name')]">
  <xsl:text>Value </xsl:text>
  <xsl:value-of select="position()" />
  <xsl:text>: </xsl:text>
  <xsl:value-of select="." />
  <xsl:text>&#10;</xsl:text>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

This gives the following output:

Value 1: value
Value 2: value
Value 3: value
Value 4: value
Sign up to request clarification or add additional context in comments.

6 Comments

When deploying it tells me: ERROR: 'Sintax error 'root/parent/*[starts-with(./name(),'node')]'.'
It works with XSLT 2.0 and Saxon 9.1. What do you work with? Also, I did not write starts-with(./name(),'node').
I know, I know I'm working with 1.0 It deploys correctly with "[starts-with(name(), 'child')]", but no node was retrieved, I've tested it with count()
This solution does exactly what you have asked. Of course, if you change the requirements (i.e. include more code or change the structure of input XML) - then it is not guaranteed to work anymore.
Yep, let's suppose that what I'm looking for is a solution, not the solution. Let's also suppose that because of confidential issues I'm not available to post a full example. Then, suppose where I've said "parent" I was meaning "path with some nodes", and of course, let's suppose that I'm not working with your code requirements (version) and you give crossover version solution. Thanks.
|
0

I would change the XML structure. For me it is easier to run through the child-elements without pattern like:

<parent>
  <childs>
    <child>
      <id>1</id>
      <name>value</name>
    </child>
    <child>
      <id>2</id>
  <name>value</name>
    </child>
    <child>
      <id>3</id>
  <name>value</name>
    </child>
    <child>
      <id>4</id>
  <name>value</name>
    </child>
   </childs>
   <others />
 </parent>

I think the structure is more clearly and just a differnt approach.

regards

1 Comment

Thanks, but I cannot change XML structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.