How does XSLT utilize prefixed attributes from a separate so

Here should go questions about transforming XML with XSLT and FOP.
iBall
Posts: 9

How does XSLT utilize prefixed attributes from a separate so

Thu Mar 26, 2015 6:07 am

I take an XML file and transform it to a fixed width utilizing XSLT, yet I'm forced to use attributes of a certain prefix. For the transformation to function properly I have to upload my finished XSLT file to a website, run it, and the website produces the necessary fixed width file.
What I want to know is how can I perform the same transformation without uploading the XSLT?
What kind of file (Schema, XSL) must I produce and link, to make sense of these attributes?

For example:

Code: Select all

<!-- note_XML.xml -->   
<?xml version="1.0" encoding="UTF-8"?>

    <note>
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>Don't forget me this weekend!</body>
    </note>


XSLT:

Code: Select all

<!-- note_XSL.xsl -->   
<?xml version-"1.0" encoding="UTF-8"?>
    <xsl:stylesheet smlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result prefixes="xs"
        xmlns:joe="urn:com.website/joe" version="2.0">

        <xsl:template match="/">
            <Case joe:separator="&#xd;&#xa;">

                <Tape>
                    <PersonReceiving joe:fixedLength="4">
                        <xsl:value-of select="/note/to"/>
                    </PersonReceiving>

                    <Sender joe:fixedLength="4" joe:align="right">
                        <xsl:value-of select="/note/from"/>
                   </Sender>
                   etc...
                </Tape>
            </Case>
        </xsl:template>
    <xsl:stylesheet>

Notice the 'joe' prefixed attributes, what do I do to make sense of them without having to upload the XSLT and have the server do it for me? I'd prefer to do the work in a separate file versus adding variables/functions to the same note_XSL.xsl stylesheet.

I appreciate your input, thanks.
Patrik
Posts: 216
Location: Hamburg/Germany

Re: How does XSLT utilize prefixed attributes from a separat

Thu Mar 26, 2015 2:15 pm

An xslt transformation would probably do the trick. Here's a small demonstration how to change the text content of elements with the joe:fixedLength attribute (ignoring the seperator and align attributes so far):

Test-Input:

Code: Select all

<Case xmlns:joe="urn:com.website/joe" joe:separator="&#xD;&#xA;">
   <Tape>
      <PersonReceiving joe:fixedLength="4">T</PersonReceiving>
      <Sender joe:fixedLength="4" joe:align="right">J</Sender>
   </Tape>
</Case>


XSL:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
   xmlns:xsl   = "http://www.w3.org/1999/XSL/Transform"
   xmlns:xs   = "http://www.w3.org/2001/XMLSchema"
   xmlns:joe   = "urn:com.website/joe"
   exclude-result-prefixes="xs">
   
   <!-- identity transformation as default templates -->
   <xsl:template match="document-node() | element()">
      <xsl:copy>
         <xsl:apply-templates select="attribute() | node()"/>
      </xsl:copy>
   </xsl:template>
   
   <xsl:template match="text() | attribute() | processing-instruction() | comment()">
      <xsl:copy/>
   </xsl:template>
   
   <!-- handling elements with joe:fixedlength attribute -->
   <xsl:template match="*[@joe:fixedLength]">
      <xsl:copy>
         <!-- don't copy any sub-items except the text -->
         <xsl:value-of select="for $i in (1 to (xs:integer(@joe:fixedLength) - string-length(text()))) return ' '" separator=""/>
         <xsl:copy-of select="text()"/>
      </xsl:copy>
   </xsl:template>
   
</xsl:stylesheet>


Output:

Code: Select all

<Case xmlns:joe="urn:com.website/joe" joe:separator="&#xD;&#xA;">
   <Tape>
      <PersonReceiving>   T</PersonReceiving>
      <Sender>   J</Sender>
   </Tape>
</Case


Regards,

Patrik
iBall
Posts: 9

Re: How does XSLT utilize prefixed attributes from a separat

Thu Mar 26, 2015 4:36 pm

Thank you for responding Patrik;

So create another XSL and add it as a second transformation, correct?

I'll try that out right now.
Patrik
Posts: 216
Location: Hamburg/Germany

Re: How does XSLT utilize prefixed attributes from a separat

Thu Mar 26, 2015 5:42 pm

Of course you could adapt the existing stylesheet as well by droping the joe-attibutes and directly formatting the values. But I understood that you WANT to use another transformation...
iBall
Posts: 9

Re: How does XSLT utilize prefixed attributes from a separat

Thu Mar 26, 2015 7:31 pm

Patrik,
I believe its missing something, I can't get the fixedLength attribute to work; should I have declared the attribute somehow? I'm sorry, I'm quite new to XSL/XML.
And how do we link-up the stylesheets? I went to "Configure Transformation Scenario" and selected "Additional XSLT stylesheets"; is that how its done?

thanks again.
Patrik
Posts: 216
Location: Hamburg/Germany

Re: How does XSLT utilize prefixed attributes from a separat

Fri Mar 27, 2015 12:18 am

Sorry, but I can't help you since I don't understand what you actually want and my assumptions appear to be wrong.

Regards,
Patrik

Return to “XSLT and FOP”

Who is online

Users browsing this forum: No registered users and 0 guests