0

I've this xml

<namedAnchor>
  {'Beacon': 'ORG', 'One': 'CARDINAL', 'Meadows': 'ORG', 'Congress': 'ORG', 'end of the month': 'DATE', 'second': 'ORDINAL', 'Tuesday': 'DATE', 'Wednesday': 'DATE', 'third': 'ORDINAL', 'New Yorker': 'NORP', 'Scramble for Medical Equipment Johnson City': 'ORG', 'US': 'GPE'}
</namedAnchor>

I need to print all the keys in comma separated in page and I've tried this.

<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body>
<xsl:for-each select="tokenize(namedAnchor, ':')">
  <p><xsl:value-of select="." /></p>
</xsl:for-each>
</body>
</html>

What i want is Beacon, One, Meadows Can someone provide answer for my question.>

Cloud Learner
  • 111
  • 1
  • 1
  • 8

1 Answers1

1

The tokenize() function requires XSLT 2.0. libxslt is an XSLT 1.0 processor. However, libxslt does support the EXSLT str:split() extension function, so you could do:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <output>
        <xsl:for-each select="str:split(normalize-space(namedAnchor), ', ')" >
            <item>
                <xsl:value-of select='translate(substring-before(., ":"), "{}&apos;", "")'/>
            </item>
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

to get:

<?xml version="1.0" encoding="UTF-8"?>
<output>
  <item>Beacon</item>
  <item>One</item>
  <item>Meadows</item>
  <item>Congress</item>
  <item>end of the month</item>
  <item>second</item>
  <item>Tuesday</item>
  <item>Wednesday</item>
  <item>third</item>
  <item>New Yorker</item>
  <item>Scramble for Medical Equipment Johnson City</item>
  <item>US</item>
</output>

Note that this assumes none of the keys contains the pattern ", " (which technically they could, since they are enclosed in quotes). To properly parse the contents, you would need an XSLT 3.0 processor that can process JSON.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • As JSON requires double quotes to delimit strings and the shown sample uses single quotes I don't think there is a straight-forward way with XSLT 3 and its JSON support. – Martin Honnen Mar 26 '20 at 12:03
  • @MartinHonnen I believe it would trivial to use `translate()` before feeding the result to `json-to-xml()`. – michael.hor257k Mar 26 '20 at 12:10
  • Trivial for the sample in the question but easily broken if any value also contains a single quote. – Martin Honnen Mar 26 '20 at 12:22
  • @MartinHonnen Can you think of a case where exchanging the two characters would not work? – michael.hor257k Mar 26 '20 at 12:31
  • Assuming that content is just Javascript object literal it could be `{ 'foo' : 'This is "quoted" text' }` for instance but also `{ foo: 'This is "quoted" text' }` or also `{ "foo" : "This is \"quoted\" text" }` and with all the variations I think it will be hard to use translate to morph them to something `parse-json` or `json-to-xml` accepts. – Martin Honnen Mar 26 '20 at 12:50
  • @MartinHonnen I think this is a bit far-fetched, but it's up to OP to consider. – michael.hor257k Mar 26 '20 at 12:57