Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 31st, 2006, 06:35 AM
Authorized User
 
Join Date: May 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default checking key value in second xml

I want to check if a value is present in an external lookup xml document. I use a document and a key function for it. When the lookup xml has a namespace defined in the first element I get no results when I remove the xmlns attribute it works OK. Does anybody know how I can get the key to work when the lookup table has a xmlns attribute in it (and I don't know if it does or not)??
 Here is de code

validate.xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:key name="identifier" match="term" use="termIdentifier"/>

    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates select="//difficulty | //interactivitylevel"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="//difficulty | //interactivitylevel">
        <xsl:variable name="val" select="./value/langstring"/>
        <xsl:variable name="src" select="./source/langstring"/>
        <xsl:variable name="lookup" select="document(normalize-space($src))"/>
        <xsl:variable name="term" select="$lookup/key('identifier',$val)"/>
        <p>
        Node: <xsl:value-of select="name()"/><br/>
        IDValue: <xsl:value-of select="$val"/><br/>
        Source: <xsl:value-of select="$src"/><br/>
        Description: <xsl:if test="string-length($term)!=0"><xsl:value-of select="$term/description/langstring"/></xsl:if></p>
    </xsl:template>
</xsl:stylesheet>

input xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <educational>
        <interactivitylevel>
            <source>
                <langstring xml:lang="nl">lookup2.xml</langstring>
            </source>
            <value>
                <langstring xml:lang="nl">VMBO2</langstring>
            </value>
        </interactivitylevel>
        <difficulty>
            <source>
                <langstring xml:lang="nl">lookup.xml</langstring>
            </source>
            <value>
                <langstring xml:lang="nl">VMBO2</langstring>
            </value>
        </difficulty>
    </educational>
</root>

lookup.xml:
<?xml version="1.0" encoding="UTF-8"?>
<vdex xmlns="http://www.imsglobal.org/xsd/imsvdex_v1p0">
    <term>
        <termIdentifier>VMBO1</termIdentifier>
        <caption>
            <langstring xml:lang="nl">Key1</langstring>
        </caption>
        <description>
            <langstring xml:lang="nl">Description key 1</langstring>
        </description>
    </term>
    <term>
        <termIdentifier>VMBO2</termIdentifier>
        <caption>
            <langstring xml:lang="nl">Key2</langstring>
        </caption>
        <description>
            <langstring xml:lang="nl">Description key 2</langstring>
        </description>
    </term>
</vdex>

lookup2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<vdex>
    <term>
        <termIdentifier>VMBO1</termIdentifier>
        <caption>
            <langstring xml:lang="nl">Key1</langstring>
        </caption>
        <description>
            <langstring xml:lang="nl">Description key 1</langstring>
        </description>
    </term>
    <term>
        <termIdentifier>VMBO2</termIdentifier>
        <caption>
            <langstring xml:lang="nl">Key2</langstring>
        </caption>
        <description>
            <langstring xml:lang="nl">Description key 2</langstring>
        </description>
    </term>
</vdex>

This is the output I get:
<html><body><p>
        Node: interactivitylevel<br>
        IDValue: VMBO2<br>
        Source: lookup2.xml<br>
        Description: Description key 2</p><p>
        Node: difficulty<br>
        IDValue: VMBO2<br>
        Source: lookup.xml<br>
        Description: </p></body></html>
 
Old June 4th, 2006, 05:01 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The reference to a schema doesn't affect things, but the reference to a namespace does. Read

http://www.dpawson.co.uk/xsl/sect2/N5536.html

for example item 15.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 6th, 2006, 05:37 AM
Authorized User
 
Join Date: May 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's a lot of information there. Thanx.
But I'm still desperate
I still want to find a way to search through an unknown lookup table (with or without a namespace or nullnamespace). Example 15 doesn't give me a whole answer yet.
The lookup source and the lookup value are defined in the input XML document and I have to check if the given value is defined in the lookuptable. So I don't know the namespace yet when creating the XSLT. I can find the namespace in the XML document with

<xsl:variable name="namespace" select="namespace-uri(document($src)/*)"/>

and can create a new element and a new namespace with
<xsl:namespace name="x" select="$namespace">
But do I have to process the lookup-table first and give every element a new namespace that I define in my XSLT template or is there an other way? And how do i define the key syntax?
 
Old June 6th, 2006, 05:57 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In principle, dealing with an unknown namespace in your source document is the same as dealing with elements whose local name is unknown: if you don't know the full name of the elements you are looking for (the full name consists of a namespace name and a local name) then you have to find the elements based on what you do know. For example, if you only know the content of the element you can find them using *[.='content']; or if you only know the local part of the name then you can find them using *[local-name()='local']. Note that this is likely to be less efficient than finding elements by their full name.

An alternative approach is to preprocess the lookup document so that all the elements have known names.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 6th, 2006, 10:54 AM
Authorized User
 
Join Date: May 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanx that helps me a lot:

I've made a beginning of first converting the lookuptable in a namespace I know like this:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:vdx="http://dummy.org/xsl">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <vdx:vdex>
            <xsl:apply-templates select="/*[local-name()='vdex']/*"/>
        </vdx:vdex>
    </xsl:template>
    <xsl:template match="*[local-name()='term']">
        <vdx:term>
            <vdx:termIdentifier>
                <xsl:value-of select="./*[local-name()='termIdentifier']"/>
            </vdx:termIdentifier>
            <xsl:apply-templates select="./*[local-name()='caption']"/>
            <xsl:apply-templates select="./*[local-name()='description']"/>
        </vdx:term>
    </xsl:template>
    <xsl:template match="*[local-name()='caption']">
        <vdx:caption>
            <vdx:langstring xml:lang="nl">
                <xsl:apply-templates select="./*[local-name()='description']"/>
            </vdx:langstring>
        </vdx:caption>
    </xsl:template>
    <xsl:template match="*[local-name()='description']">
        <vdx:description>
            <vdx:langstring xml:lang="nl">
                <xsl:apply-templates select="./*[local-name()='langstring']"/>
            </vdx:langstring>
        </vdx:description>
    </xsl:template>
    <xsl:template match="*[local-name()='langstring']">
        <xsl:value-of select="."/>
    </xsl:template>
</xsl:stylesheet>

and this is the result

<?xml version="1.0"?>
<vdx:vdex xmlns:vdx="http://dummy.org/xsl">
    <vdx:term>
        <vdx:termIdentifier>VMBO2</vdx:termIdentifier>
        <vdx:caption>
            <vdx:langstring xml:lang="nl"/>
        </vdx:caption>
        <vdx:description>
            <vdx:langstring xml:lang="nl">Description key 2</vdx:langstring>
        </vdx:description>
    </vdx:term>
    <vdx:term>
        <vdx:termIdentifier>VMBO3</vdx:termIdentifier>
        <vdx:caption>
            <vdx:langstring xml:lang="nl"/>
        </vdx:caption>
        <vdx:description>
            <vdx:langstring xml:lang="nl">Description key 3</vdx:langstring>
        </vdx:description>
    </vdx:term>
</vdx:vdex>

now I Know the namespace output. the only :( is the processing time.. but getting there.
 
Old June 6th, 2006, 11:05 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm surprised you can't do the conversion more generically:

<xsl:template match="*">
  <xsl:element name="vdx:{local-name()}" namespace="http://dummy.org/xsl">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 6th, 2006, 12:26 PM
Authorized User
 
Join Date: May 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You notice I'm a newby, got your book but still not read all of it:)
This is a great routine, it's generic and I can use it everywhere I want to change the namespace.
(should be in the book XSLT Programmer's Reference if not already)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking for duplicate primary key in Access Indo77 ASP.NET 2.0 Basics 2 November 29th, 2006 12:19 PM
<bean:message key="PNR.INPUT"/> key has null value warsha_14 Struts 1 November 13th, 2006 07:26 AM
TAB KEY working together KEY PRESS event thomaz C# 4 August 20th, 2006 02:47 PM
checking for recently added xml files fogofogo XML 0 February 13th, 2006 06:39 AM
XML to XML, checking attributes raoulvb XSLT 4 December 9th, 2004 10:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.