0

I know that generating fixed width text from XMl is a common need. And I know this question has been asked before, but every answer I've found is unclear to me due to my lack of understand of XSLT functions.

I have several examples of using an XSLT function to pad a string to a particular width. But none of the examples I'f found are very clear on how to use these functions.

I need to take the contents of an XML element, and pad that to a fixed with with spaces. I have found such functions at the below links.

But in each example they pass in a string, such as "1234 Street". How do I use such functions by passing an element such as <Address>1234 Street</Address>

7
  • I am afraid your question is not clear. 1234 Street is a string, not an element. <StreetAddress>1234 Street</StreetAddress> is an element - and you would use <xsl:value-of select="StreetAddress"/> to get the element's text value and pass it to a string function. Commented Aug 1, 2014 at 18:49
  • My question is if i just put, <xsl:value-of select="StreetAddress"/> in the function call where the examples show placing something like "1234 Street"? Commented Aug 1, 2014 at 21:07
  • A practical example would be useful, but I believe the answer is yes. Note also that an implicit conversion can take place, so that string-function(StreetAddress) ends up being the same thing as string-function("1234 Street"). This is probably the more frequent form of use you will see. Commented Aug 1, 2014 at 21:13
  • I tried to include the element tags in my question, but i don't know how to make them appear. Commented Aug 1, 2014 at 21:15
  • Use the code sample button {}to format them. Commented Aug 1, 2014 at 21:16

1 Answer 1

1

As Michael already explained, any argument is string-normalized if a function expects a string. If you take your first example from FunctX then you can do the following;

Input XML

<root>
    <address>1234 Street</address>
<root>

XSLT

In the below examples I assume you have copied the code from FunctX and bound it to a namespace (I use the funtcx prefix).

One way to do it (. refers to the context node):

<xsl:template match="address">
    <!-- we are now inside address element -->
    <xsl:value-of select="functx:pad-string-to-length(., ' ', 20)
</xsl:template>

Other way to do it:

<xsl:template match="root">
    <!-- we are now inside root element -->
    <!-- note, this will process ALL children under "root" that match "address" -->
    <xsl:value-of select="functx:pad-string-to-length(address, ' ', 20)
</xsl:template>

Basically: just use any XPath expression that selects the element you want to be passed on to the function should do it. The function has its argument defined as xs:string, which will require the processor to get the value of of the element before it passes it on to the function.

You do not need to change the function body in any way, you can just use them as-is.

Sign up to request clarification or add additional context in comments.

Comments

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.