This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: xpath expression in <xsl:stylesheet> element
- To: <xsl-list at lists dot mulberrytech dot com>
- Subject: RE: [xsl] xpath expression in <xsl:stylesheet> element
- From: "Sullivan, Dan" <dsullivan at develop dot com>
- Date: Fri, 24 Aug 2001 12:35:56 -0700
- Reply-To: xsl-list at lists dot mulberrytech dot com
If you are willing to keep all you favorite namespaces and schema
location in a configuration file, for example
mySchemas.xml file
<schemas>
<schema>
<namespace>urn:sample:A</namespace>
<uri>http://www.develop.com/a.xsd</uri>
</schema>
<schema>
<namespace>urn:sample:B</namespace>
<uri>http://www.develop.com/b.xsd</uri>
</schema>
<schema>
<namespace>urn:sample:C</namespace>
<uri>http://www.develop.com/c.xsd</uri>
</schema>
</schemas>
The this transform will add the appropriate schemaLocation to an input
file (watch for line wraps).
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.0">
<!-- generic identity xform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- matches if document element is in a namespace, but there is no
schema locations -->
<xsl:template match="/*[not(@schemaLocation) and
namespace-uri()!='']">
<!-- lookup schema location from a configuration file -->
<xsl:variable name="location"
select="document('mySchemas.xml')/*/schema[namespace=namespace-uri(curre
nt())]"/>
<xsl:copy>
<xsl:if test="$location">
<!-- this schema is in our configuration file, so add schema
location -->
<xsl:attribute name="schemaLocation"
namespace="http://www.w3.org/2001/XMLSchema-instance">
<xsl:value-of select="$location/namespace"/><xsl:text>
</xsl:text> <xsl:value-of select="$location/uri"/>
</xsl:attribute>
</xsl:if>
<!-- build out rest of document -->
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Dan
-----Original Message-----
From: astockley@orincon.com [mailto:astockley@orincon.com]
Sent: Friday, August 24, 2001 1:05 PM
To: xsl-list@lists.mulberrytech.com
Subject: [xsl] xpath expression in <xsl:stylesheet> element
I am trying to create a generic stylesheet for a number of different
input.xml. Each input.xml has a different factType which in turn has its
own schema whose location and name I want to specify in the output. (For
Example ideally I want the output root element to be
<LoginHypothesis
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:ia="http://www.orincon.com/ia" xsi:schemaLocation=
"http://www.orincon.com/ia LoginHypothesis.xsd">)
I would also like to automatically get the name of the schema from the
incoming document as opposed to having it hard coded in the stylesheet.
1) How do I get the "xsi:schemaLocation="http://www.orincon.com/ia
LoginHypothesis.xsd" text to appear?
2) Can I use xpath in the <xsl:stylesheet> element to extract this
LoginHypothesis factType value from the input.xml?
I have tried xsi:schemaLocation="http://www.orincon.com/ia
{ia:unorderedFact/child::factType/text()}.xsd" with no joy.
Any suggestions on how I handle this gratefully received.
Thank you
Annie
This is what I have at the moment
input.xml
<ia:unorderedFact
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:schemaLocation="http://www.orincon.com/ia UnorderedFact.xsd"
xmlns:ia="http://www.orincon.com/ia">
<factType>LoginHypothesis</factType>
<slot>
<tag>userName</tag>
<ia:userName>jpublic</ia:userName>
</slot>
<slot>
<tag>hostName</tag>
<ia:hostName>linux1</ia:hostName>
</slot>
...
</ia:unorderedFact>
stylesheet.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform"
xmlns:ia="http://www.orincon.com/ia"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="ia:unorderedFact">
<xsl:element name="{child::factType/text()}">
<xsl:text/>
<xsl:for-each select="//slot">
<xsl:choose>
<xsl:when test="count(child::*)=2">
<xsl:element name="{child::tag/text()}">
<xsl:value-of select="./*[position()=2]"/>
</xsl:element>
<xsl:text/>
</xsl:when>
<xsl:when test="count(child::*)>2">
......
</xsl:stylesheet>
output.xml
<?xml version="1.0" encoding="UTF-8"?>
<LoginHypothesis
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:ia="http://www.orincon.com/ia">
<userName>jpublic</userName>
<hostName>linux1</hostName>
....
</LoginHypothesis>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list