Short hand for the number of tags having a longer [and almost the same] Xpath

For example: this is xslt

<xsl:template match="/root/sub-root/parent/child/grand_child/dummy1/dummy2/dummy3/dummy4/node_1
|/root/sub-root/parent/child/grand_child/dummy1/dummy2/dummy3/dummy4/node_2
|/root/sub-root/parent/child/grand_child/dummy1/dummy2/dummy3/dummy4/node_3
 .
 .
|/root/sub-root/parent/child/grand_child/dummy1/dummy2/dummy3/dummy4/node_N"/>

In the above code, can I use XPath /root/sub-root/parent/child/grand_child/dummy1/dummy2/dummy3/dummy4 only once [use braces or something else] and reduce the bulk of the code?

As you can see, all nodes are siblings of each other, therefore, except for their name, their Xpath is the same. Is there a short property?

Does XSLT 1.0 (or Xpath1.0) allow?

+3
source share
3 answers

Tested only in the xpath online tool, so not quite sure, but it should work

<xsl:template match="/root/sub-root/parent/child/grand_child/dummy1/dummy2/dummy3/dummy4/node()[name() = 'node_1' or name()='node_2' ... or name()='node_N']"/>

If there is not a single node in dummy4 that you want to omit, just release [..]

+1

, . , , , , , .

, self:

<xsl:template match="/.../dummy4/*[self::node_1 or self::node_2 ...]" />

,

<xsl:template match="/.../dummy4/*[substring-before(name(), '_') = 'node']" />
+3

XSLT 1.0 ( Xpath1.0)?

XPath 1.0:

root/sub-root/parent/child/grand_child
              /dummy1/dummy2/dummy3/dummy4
                /*
                 [starts-with(name(), 'node_')
                and
                  substring-after(name(), 'node_') >= 1
                and
                  not(substring-after(name(), 'node_') > $N)
                  ]

XPath, . , XSLT 1.0 xsl:.

, N , (, 1000) XPath , , XSLT 1.0.

, , . node - "---", node , .

, ::

 *
 [starts-with(name(), 'node_')
and
  substring-after(name(), 'node_') >= 1
and
   not(substring-after(name(), 'node_') > {N})

 ]

{N} - $N.

Or, in the simplest case (it happens quite often), if there are four nodes and no value is required , you can simply use:

 node_1|node_2|node_3|node_4
+2
source

Source: https://habr.com/ru/post/1738447/


All Articles