XSLT: Match last text() descendant in an element
I am working on an XSLT (1.0) template that needs to match the last text node in an element that has a particular attribute and inject . The hard part is I need to know the last descendant, and not necessarily the last child. The schema is not consistent. Some text Some more text Some other text. SELECT ME The closest I have gotten is the following, but it selects the last text() on every element, not the last element with a text node (e3 in the example above). *[ancestor-or-self::*[@condition='optional']]/text()[last()]
Tagged:
2
Best Answer
-
This is what I came up with, it seems to be working.1
Answers
-
Are you getting caught by order of precedence? The predicate operator has a higher precedence than / and // operators. Edit 1: Unfortunately, it looks like XSL 1.0 patterns can't handle adding the parentheses to avoid the precedence issue. I tried moving the conditions into the predicate and had some success. *[ancestor-or-self::*[@condition='optional'] and text() and not(descendant::*) and not(following::*//text())] See: [
http://www.xsltcake.com/slices/vGAxJN/3][1] Edit 2: Your new input raises some interesting questions. I have a new solution that works on both of these inputs, but fails in other scenarios. - [
http://www.xsltcake.com/slices/lFUpC7][2] - [
http://www.xsltcake.com/slices/lFUpC7/2][3] Fails (I'm unsure on how to get the order of operations correct on a `[last()]` predicate to grab the last node: - [
http://www.xsltcake.com/slices/lFUpC7/3][4] (this raises a question if you would want to ignore certain text markup elements?) - [
http://www.xsltcake.com/slices/lFUpC7/4][5] It is funny how easy this seems to be with `select`: `(//text())[last()]`. On that note, what are you trying to due with this last text node? Maybe an alternative can be found. [1]:
http://www.xsltcake.com/slices/vGAxJN/3 [2]:
http://www.xsltcake.com/slices/lFUpC7 [3]:
http://www.xsltcake.com/slices/lFUpC7/2 [4]:
http://www.xsltcake.com/slices/lFUpC7/3 [5]:
http://www.xsltcake.com/slices/lFUpC7/41 -
Note this is XSL 1.0 (Added to question). I am getting an exception when trying to parse that line due to parenthesis.0
-
I edited my answer with the constraints of XSL 1.0 and that this needs to be a match pattern, not any valid XPath.0
-
Thanks, that works, though I do have an issue now where there is a trailing
that is messing up the match.
http://www.xsltcake.com/slices/vGAxJN/40 -
I have a new solution that works on both of these inputs, but fails in other scenarios.
http://www.xsltcake.com/slices/lFUpC7
http://www.xsltcake.com/slices/lFUpC7/2 Fails:
http://www.xsltcake.com/slices/lFUpC7/3 (and raises a question if you would want to ignore certain text markup elements?)
http://www.xsltcake.com/slices/lFUpC7/4 I've edited my answer as well with additional comments.0 -
Couldn't you use
to clean up the empty text nodes? I'd think that would take care of the
and
elements messing things up.0 -
I haven't tried this and it's been a long time since I looked at XSLT1.0 but would it not work if you put `*[ancestor-or-self::*[@condition='optional']]/text()` in a variable and you then selected `$var[last()]`?0
-
Matt, one thing to note is following doesn't work in all cases because there will be other text outside of the sub-clause.0
-
Anyone able to unprove this?
http://www.xsltcake.com/slices/vGAxJN/120 -
The following seems to work for me:0
-
Look at the comment chain in Matthew Sorenson's answer to see why this isn't enough.0
-
Something I didn't consider before is that this may take precedence over currently existing text node templates (or newly added/existing templates may override this template). For example, the special character translator and footnote reference cleaner templates also match against "text()".0
-
Ultimately the issue is bad content, but they won't have a fix in time. I need to wrap certain text nodes with [] brackets. I can always call the special character translate directly if needed, but for the content the [] are much more important.0
Categories
- All Categories
- 6 AHS
- 37 Alpha
- 161 App Studio
- 4 Block Chain
- 4 Bot Platform
- 16 Connected Risk APIs
- 47 Data Fusion
- 30 Data Model Discovery
- 608 Datastream
- 1.3K DSS
- 577 Eikon COM
- 4.9K Eikon Data APIs
- 7 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- Trading API
- 2.7K Elektron
- 1.3K EMA
- 236 ETA
- 519 WebSocket API
- 33 FX Venues
- 10 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 20 Messenger Bot
- 2 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 59 Open Calais
- 264 Open PermID
- 39 Entity Search
- 2 Org ID
- PAM
- PAM - Logging
- 8.4K Private Comments
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 20 RDMS
- 1.4K Refinitiv Data Platform
- 367 Refinitiv Data Platform Libraries
- 3 Refinitiv Due Diligence
- LSEG Due Diligence Portal API
- 3 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.1K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 10 World-Check Customer Risk Screener
- 990 World-Check One
- 44 World-Check One Zero Footprint
- 45 Side by Side Integration API
- Test Space
- 3 Thomson One Smart
- 1.2K TR Internal
- Global Hackathon 2015
- 2 Specialists Who Code
- 10 TR Knowledge Graph
- 150 Transactions
- 142 REDI API
- 1.7K TREP APIs
- 4 CAT
- 21 DACS Station
- 117 Open DACS
- 1.1K RFA
- 103 UPA
- 172 TREP Infrastructure
- 224 TRKD
- 886 TRTH
- 5 Velocity Analytics
- 5 Wealth Management Web Services
- 59 Workspace SDK
- 9 Element Framework
- 5 Grid
- 13 World-Check Data File
- Yield Book Analytics
- 46 中文论坛