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


问题内容

从XPath表达式的映射填充(或生成)XML模板文件的最佳方法是什么?

要求是我们需要从模板开始(因为它可能包含XPath表达式中未捕获的信息)。

例如,起始模板可能是:

<s11:Envelope xmlns:s11='http://schemas.xmlsoap.org/soap/envelope/'>
    <ns1:create xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/'>
      <article xmlns:ns1='http://predic8.com/material/1/'>
        <name>?XXX?</name>
        <description>?XXX?</description>
        <price xmlns:ns1='http://predic8.com/common/1/'>
          <amount>?999.99?</amount>
          <currency xmlns:ns1='http://predic8.com/common/1/'>???</currency>
        </price>
        <id xmlns:ns1='http://predic8.com/material/1/'>???</id>
      </article>
    </ns1:create>
  </s11:Body>
</s11:Envelope>

然后提供给我们,类似于:

expression: /create/article[1]/id                => 1
expression: /create/article[1]/description       => bar
expression: /create/article[1]/name[1]           => foo
expression: /create/article[1]/price[1]/amount   => 00.00
expression: /create/article[1]/price[1]/currency => USD
expression: /create/article[2]/id                => 2
expression: /create/article[2]/description       => some name
expression: /create/article[2]/name[1]           => some description
expression: /create/article[2]/price[1]/amount   => 00.01
expression: /create/article[2]/price[1]/currency => USD

然后,我们应该生成:

<ns1:create xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/'>
    <article xmlns:ns1='http://predic8.com/material/1/'>
        <name xmlns:ns1='http://predic8.com/material/1/'>foo</name>
        <description>bar</description>
        <price xmlns:ns1='http://predic8.com/common/1/'>
            <amount>00.00</amount>
            <currency xmlns:ns1='http://predic8.com/common/1/'>USD</currency>
        </price>
        <id xmlns:ns1='http://predic8.com/material/1/'>1</id>
    </article>
    <article xmlns:ns1='http://predic8.com/material/2/'>
        <name>some name</name>
        <description>some description</description>
        <price xmlns:ns1='http://predic8.com/common/2/'>
            <amount>00.01</amount>
            <currency xmlns:ns1='http://predic8.com/common/2/'>USD</currency>
        </price>
        <id xmlns:ns1='http://predic8.com/material/2/'>2</id>
    </article>
</ns1:create>

我是用Java实现的,尽管如果可能的话,我会首选基于XSLT的解决方案。


问题答案:

此转换从“表达式”创建具有所需结果结构的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/”命名空间中生成。请注意,“模板”中存在的其他名称空间不会使用,可以安全地省略。