81,122
社区成员




<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ben="http://www.bennadel.com/xslt"
xmlns:func="http://exslt.org/functions"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="func str ben">
<func:function
name="ben:list-find"
ben:return-type="boolean"
ben:hint="I take a delimited list and a value and return true or false as to whether that value is a list item in the given list.">
<!-- Define arguments. -->
<xsl:param
name="list"
ben:type="string"
ben:required="true"
ben:hint="I am the delimited list of values that is being searched."
/>
<xsl:param
name="value"
ben:type="string"
ben:required="true"
ben:hint="I am the value for which we are searching in the above list."
/>
<xsl:param
name="delimiters"
select="','"
ben:type="string"
ben:required="false"
ben:hint="I am the collection of delimiters used to deliniate the list values. I can be one or more characters (each character is used as a valid delimiter). I am defaulted to the comma."
/>
<xsl:variable
name="tokens"
select="str:tokenize( $list, $delimiters )"
/>
</func:function>
<!-- Match the root girls node. -->
<xsl:template match="/girls">
<girls>
<xsl:copy-of
select="./girl[ ben:list-find( 'athletic,hot', @type ) ]"
/>
</girls>
</xsl:template>
</xsl:transform>
<?xml version="1.0"?>
<girls>
<girl type="cute">
<name>Sarah</name>
</girl>
<girl type="athletic">
<name>Tricia</name>
</girl>
<girl type="hot">
<name>Katie</name>
</girl>
<girl type="cute">
<name>Libby</name>
</girl>
</girls>
static Transformer m_transformer ;
static String m_strLastXslPath = "" ;
public static String xsl_transform_with_stylesheet(String xml_text ,String xsl_stylesheet_path){
String strRet = null ;
try {
//
TransformerFactory ddd = TransformerFactory.newInstance() ;
if ( m_strLastXslPath != xsl_stylesheet_path ){ //xsl
StreamSource SourceXsl = new StreamSource(xsl_stylesheet_path) ;
m_transformer = ddd.newTransformer (SourceXsl);
m_strLastXslPath = xsl_stylesheet_path;
}
//xml
ByteArrayInputStream bisXml = new ByteArrayInputStream(xml_text.getBytes());
StreamSource SourceXml = new StreamSource(bisXml) ;
//html ouput
ByteArrayOutputStream bosHtml = new ByteArrayOutputStream();
Result ResultHtml = new StreamResult( bosHtml );
//do it
m_transformer.transform(SourceXml, ResultHtml);
strRet = bosHtml.toString();
}
catch (TransformerConfigurationException e) {
e.printStackTrace( );
}
catch (Exception e) {
e.printStackTrace( );
}
return strRet ;
}