随笔 - 175  文章 - 202  trackbacks - 0
<2009年10月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

第一个Blog,记录哈哈的生活

常用链接

留言簿(16)

随笔分类

随笔档案

文章分类

文章档案

收藏夹

Java links

搜索

  •  

最新评论

阅读排行榜

评论排行榜


xsl 里增加

   function valueOf(context, elem) {
    return tools.valueOf(context, elem);
   }
代码里增加


 public String valueOf(XSLProcessorContext context, ElemExtensionCall elem) {
  String value = elem.getAttribute("select");
  String maxStr = elem.getAttribute("max");
  try {
   value = executeExpression(context, elem, value);
  } catch (TransformerException e) {
  }
  if (isNotBlank(maxStr)) {
   try {
    int max = Integer.parseInt(maxStr);
    value = shortStr(value, max);
   } finally {
   }
  }
  return value;
 }

 private String executeExpression(XSLProcessorContext context, ElemExtensionCall elem, String value)
   throws TransformerException {
  XPathContext xctxt = context.getTransformer().getXPathContext();
  XPath path = new XPath(value, elem, xctxt.getNamespaceContext(), XPath.SELECT);
  XObject data = path.execute(xctxt, context.getContextNode(), elem);
  if (data != null)
   value = data.xstr().toString();
  return value == null ? "" : value;
 }











参考了下面的文档






[prev in list] [next in list] [prev in thread] [next in thread]
List: xalan-j-users Subject: Re: Extension element with subelements From: John Gentilin <gentijo () eyecatching ! com> Date: 2007-09-07 21:48:37 Message-ID: 46E1C735.2080504 () eyecatching ! com [Download message RAW] Oops... this might be what you are after instead... executeChildTemplatesToXML or ToString or ToXMLString may be what you are after.. /** * A repository for common functions that can be used but the extension * functions and extension elements. * * * Title: Base Functions * Copyright: Copyright (c) 2005 * Company: Eye Catching Solutions Inc. * @version 1.0 * @author John Gentilin */ public class BaseFunctions { private static Category m_Log = Category.getInstance("RapidXSL.Interface"); private static DocumentBuilderFactory m_dfactory = null; private static DocumentBuilder m_docBuilder = null; /** * @param context * @param elem * @throws TransformerException */ protected void executeChildTemplates( XSLProcessorContext context, ElemExtensionCall elem ) throws TransformerException { TransformerImpl transf = context.getTransformer(); transf.executeChildTemplates(elem, context.getContextNode(), context.getMode(), transf.getResultTreeHandler()); } /** * @param context * @param elem * @throws TransformerException */ protected Document executeChildTemplatesToXML(Document doc, XSLProcessorContext context, ElemExtensionCall elem ) throws XMLServerException { DOMBuilder builder = null; TransformerImpl transformer = context.getTransformer(); try { builder = new DOMBuilder(doc); transformer.executeChildTemplates(elem, context.getContextNode(), context.getMode(), builder); } catch (Exception e) { throw new XMLServerException("RXSL_IFACE", "XSL Interface Error - " + e.getLocalizedMessage()); } return doc; } /** * @param context * @param elem * @throws TransformerException */ protected String executeChildTemplatesToString(XSLProcessorContext context, ElemExtensionCall elem ) throws XMLServerException { TransformerImpl transformer = context.getTransformer(); try { TextSerializationHandler handler = new TextSerializationHandler(); transformer.executeChildTemplates(elem, context.getContextNode(), context.getMode(), handler); return handler.toString(); } catch (Exception e) { throw new XMLServerException("RXSL_IFACE", "XSL Interface Error - " + e.getLocalizedMessage()); } } protected String executeChildTemplatestoXMLString(XSLProcessorContext context, ElemExtensionCall elem) throws TransformerException, SAXException { try { TransformerImpl transformer = context.getTransformer(); // This should be worked on so that the output format can be // defined by a first child of the redirect element. OutputProperties format = transformer.getOutputFormat(); Properties prop = format.getProperties(); format.setProperty("indent","no"); format.setProperty("{http://xml.apache.org/xalan}indent-amount","0"); ByteArrayOutputStream ostream = new ByteArrayOutputStream(); SerializationHandler serializer = transformer.createSerializationHandler( new StreamResult(ostream), format); serializer.startDocument(); transformer.executeChildTemplates(elem, context.getContextNode(), context.getMode(), serializer); serializer.flushPending(); serializer.endDocument(); return ostream.toString("ISO-8859-1"); } catch (UnsupportedEncodingException e) { m_Log.fatal("Error Encoding Bytes from XML - " + e.getLocalizedMessage()); return new String(); } } /** * Get the Node Value of a Stylesheet Element. This will be used to collect * the data value when populating a Control..... * * @return */ protected String getNodeValue( ElemExtensionCall elem ) { Node child = elem.getFirstChild(); if (child != null) { return child.getNodeValue(); } return null; } /** * Resolve the Attribute Selection for the fixed attribute "select" * * @return * @throws TransformerException */ protected String getStringValueFromSelectExpr(XSLProcessorContext context, ElemExtensionCall elem ) throws TransformerException { return getStringValueFromAttribute("select", context, elem); } /** * Find the Attribute named "attrib" and convert that to an XPath statement * that can be executed. * * Return null if the Attribute did not exist. * * @param attrib * @return * @throws TransformerException */ protected static XPath getAttributeExpr( String attrib, XSLProcessorContext context, ElemExtensionCall elem ) throws TransformerException { String expr = elem.getAttribute (attrib, context.getContextNode(), context.getTransformer()); if(null != expr) { org.apache.xpath.XPathContext xctxt = context.getTransformer().getXPathContext(); return new XPath( expr, elem, xctxt.getNamespaceContext(), XPath.SELECT); } return null; } /** * * Get the value of an Attribute. It will first try to treat the attribute as an XPath * if that fails then just return the attribute value itself. * * @param attrName * @param context * @param elem * @return */ protected String getStringValueFromAttribute( String attrName, XSLProcessorContext context, ElemExtensionCall elem ) { XPath path; String value = null; try { path = getAttributeExpr(attrName, context, elem); if (path != null) { XObject data = executeXPath(path, context, elem); if (data != null) value = data.xstr().toString(); } } catch (TransformerException e) { value = elem.getAttribute(attrName); } if (value == null) value = ""; return value; } /** * A helper function to execute an XPath Object. * * @param path * @return * @throws TransformerException */ protected static XObject executeXPath( XPath path, XSLProcessorContext context, ElemExtensionCall elem ) throws TransformerException { org.apache.xpath.XPathContext xctxt = context.getTransformer().getXPathContext(); return path.execute(xctxt, context.getContextNode(), elem); } protected Document getEmptyDocument() { initDocFactory(); return m_docBuilder.newDocument(); } protected static void initDocFactory() { if (m_dfactory != null) return; try { m_dfactory = DocumentBuilderFactory.newInstance(); m_docBuilder = m_dfactory.newDocumentBuilder(); } catch (Exception e) { m_Log.fatal("RapidXSL Interface, failure to init XML Subsystem - " + e.getLocalizedMessage()); } } } Cynepnaxa wrote: > Hi, everybody! > I start to write simple extension element, but a problem occurs. I want to > access extension element's subelements in java+xalan. My xsl contains > following block of code: > <my-ext:request> > <body><xsl:value-of select="$body"/></body> > <contentType>text/html</contentType> > <metaData><![CDATA[<?xml version="1.0" encoding="utf-8"?>]]><metaData> > <xsl:value-of select="$metaData"/> > </metaData> > </metaData> > </my-ext:request> > In extension element Java-method i have "XSLProcessorContext context" and > "ElemExtensionCall elem". I can get contentType by calling for example > "elem.getFirstChild().getNextSibling().getFirstChild().getTextContent()", > but i want to access subelements by tag name(for example, by > elem.getElementsByTagName("metadata"), or XPath). I have no idea how to > access in java content of $body variable and xml - encapsulated $metaData. > All google - examples shining no light. Is simple xslt - subelements access > method exist? Big thanx for help. > P.S. Sorry for my English. I hope you understand me. > > [prev in list] [next in list] [prev in thread] [next in thread]


Configure | About | News | Donate | Add a list | Sponsors: 10EastKoreLogicTerra-InternationalChakpak.com
posted on 2009-10-21 17:19 哈哈的日子 阅读(353) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: