栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从XPath表达式填充XML模板文件?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从XPath表达式填充XML模板文件?

此转换从“表达式”创建具有所需结果结构的XML文档-仍需将其转换为最终结果:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:my="my:my"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:variable name="vPop" as="element()*">    <item path="/create/article[1]/id">1</item>    <item path="/create/article[1]/description">bar</item>    <item path="/create/article[1]/name[1]">foo</item>    <item path="/create/article[1]/price[1]/amount">00.00</item>    <item path="/create/article[1]/price[1]/currency">USD</item>    <item path="/create/article[1]/price[2]/amount">11.11</item>    <item path="/create/article[1]/price[2]/currency">AUD</item>    <item path="/create/article[2]/id">2</item>    <item path="/create/article[2]/description">some name</item>    <item path="/create/article[2]/name[1]">some description</item>    <item path="/create/article[2]/price[1]/amount">00.01</item>    <item path="/create/article[2]/price[1]/currency">USD</item> </xsl:variable> <xsl:template match="/">  <xsl:sequence select="my:subTree($vPop/@path/concat(.,'/',string(..)))"/> </xsl:template> <xsl:function name="my:subTree" as="node()*">  <xsl:param name="pPaths" as="xs:string*"/>  <xsl:for-each-group select="$pPaths"    group-adjacent=        "substring-before(substring-after(concat(., '/'), '/'), '/')">    <xsl:if test="current-grouping-key()">     <xsl:choose>       <xsl:when test=          "substring-after(current-group()[1], current-grouping-key())">         <xsl:element name="{substring-before(concat(current-grouping-key(), '['), '[')}">          <xsl:sequence select= "my:subTree(for $s in current-group()   return      concat('/',substring-after(substring($s, 2),'/'))       ) "/>        </xsl:element>       </xsl:when>       <xsl:otherwise>        <xsl:value-of select="current-grouping-key()"/>       </xsl:otherwise>     </xsl:choose>     </xsl:if>  </xsl:for-each-group> </xsl:function></xsl:stylesheet>

当此转换应用于任何XML文档(未使用)时,结果为:

<create>   <article>      <id>1</id>      <description>bar</description>      <name>foo</name>      <price>         <amount>00.00</amount>         <currency>USD</currency>      </price>      <price>         <amount>11.11</amount>         <currency>AUD</currency>      </price>   </article>   <article>      <id>2</id>      <description>some name</description>      <name>some description</name>      <price>         <amount>00.01</amount>         <currency>USD</currency>      </price>   </article></create>

注意事项:

  1. 您需要将给出的“表达式”转换为此转换中使用的格式-这是简单而直接的。

  2. 在最后的转换中,您需要“按原样”复制每个节点(使用身份规则),除了顶级节点应在"http://predic8.com/wsdl/material/ArticleService/1/”命名空间中生成。请注意,“模板”中存在的其他名称空间不会使用,可以安全地省略。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/498082.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号