Lokesh
Lokesh

Reputation: 51

Xml Xpath Expression for Nested XML

I have a XML

<font-text content-format="howdy" data-type="text">
<Index>1</Index>
<Root>1</Root>
<Result>302</Result>
</font-text>

What would be the Xpath expression for the above XMS if I want to take out "Index","Root" & "Result" values in the format "302-01-01"

Upvotes: 0

Views: 118

Answers (1)

Yitzhak Khabinsky
Yitzhak Khabinsky

Reputation: 22321

XPath string-join() and format-number() functions to the rescue.

format-number() function exists in XSLT 2.0

It was moved from XSLT 3.0 to XPath 3.0

XPath 3.0

string-join((/font-text/Result/text(), format-number(/font-text/Root/text(),'00'), format-number(/font-text/Index/text(),'00')), "-")

XPath 2.0

string-join((/font-text/Result/text()
, substring(concat("00", /font-text/Root/text()), string-length(concat("00",/font-text/Root/text())) - 1)
, substring(concat("00", /font-text/Index/text()), string-length(concat("00",/font-text/Index/text())) - 1))
, "-")

XPath 1.0

concat(/font-text/Result/text()
, "-"
, substring(concat("00", /font-text/Root/text()), string-length(concat("00",/font-text/Root/text())) - 1)
, "-"
, substring(concat("00", /font-text/Index/text()), string-length(concat("00",/font-text/Index/text())) - 1)
)

Upvotes: 2

Related Questions