xsl 未转换为所需的输出

Posted

技术标签:

【中文标题】xsl 未转换为所需的输出【英文标题】:xsl not transforming to desired output 【发布时间】:2020-05-09 17:40:48 【问题描述】:

我正在尝试复制位于此处的示例 Click Here

但是,我无法达到预期的效果。 输出根本没有按需要生成。 我使用的输入:

<?xml version="1.0" encoding="UTF-8" ?>
<Order xmlns="http://www.book.org">
  <Bundle>
    <authors>
        <author>
            <authorId>100</authorId>
            <authorName>Kathisiera</authorName>
        </author>
        <author>
            <authorId>200</authorId>
            <authorName>Bates</authorName>
        </author>
        <author>
            <authorId>300</authorId>
            <authorName>Gavin King</authorName>
        </author>
    </authors>
    <books>
        <book>
            <orderId>1111</orderId>
            <bookName>Head First Java</bookName>
            <bookAuthorId>100</bookAuthorId>
        </book>
        <book>
            <orderId>5555</orderId>
            <bookName>Head First Servlets</bookName>
            <bookAuthorId>200</bookAuthorId>
        </book>
        <book>
            <orderId>1111</orderId>
            <bookName>Hibernate In Action</bookName>
            <bookAuthorId>300</bookAuthorId>
        </book>
    </books>
</Bundle>
</Order>
**The Schema I have used in for my transformation**

<!-- begin snippet: js hide: false console: true babel: false -->

我用于转换的 XSLT:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
                xmlns:socket="http://www.oracle.com/XSL/Transform/java/oracle.tip.adapter.socket.ProtocolTranslator"
                xmlns:oracle-xsl-mapper="http://www.oracle.com/xsl/mapper/schemas"
                xmlns:dvm="http://www.oracle.com/XSL/Transform/java/oracle.tip.dvm.LookupValue"
                xmlns:mhdr="http://www.oracle.com/XSL/Transform/java/oracle.tip.mediator.service.common.functions.MediatorExtnFunction"
                xmlns:oraxsl="http://www.oracle.com/XSL/Transform/java"
                xmlns:oraext="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:xp20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xref="http://www.oracle.com/XSL/Transform/java/oracle.tip.xref.xpath.XRefXPathFunctions"
                xmlns:ns0="http://www.book.org"
                exclude-result-prefixes="oracle-xsl-mapper xsi xsd xsl ns0 socket dvm mhdr oraxsl oraext xp20 xref">
  <oracle-xsl-mapper:schema>
    <!--SPECIFICATION OF MAP SOURCES AND TARGETS, DO NOT MODIFY.-->
    <oracle-xsl-mapper:mapSources>
      <oracle-xsl-mapper:source type="XSD">
        <oracle-xsl-mapper:schema location="../Schemas/BooksOrder.xsd"/>
        <oracle-xsl-mapper:rootElement name="Order" namespace="http://www.book.org"/>
      </oracle-xsl-mapper:source>
    </oracle-xsl-mapper:mapSources>
    <oracle-xsl-mapper:mapTargets>
      <oracle-xsl-mapper:target type="XSD">
        <oracle-xsl-mapper:schema location="../Schemas/BooksOrder.xsd"/>
        <oracle-xsl-mapper:rootElement name="Order" namespace="http://www.book.org"/>
      </oracle-xsl-mapper:target>
    </oracle-xsl-mapper:mapTargets>
    <!--GENERATED BY ORACLE XSL MAPPER 12.2.1.2.0(XSLT Build 161003.0739.0018) AT [THU JAN 23 13:13:32 IST 2020].-->
  </oracle-xsl-mapper:schema>
  <!--User Editing allowed BELOW this line - DO NOT DELETE THIS LINE-->
  <xsl:key name="k" match="ns0:Order/ns0:Bundle/ns0:books/ns0:book" use="ns0:orderId"/>
  <xsl:key name="a" match="ns0:Order/ns0:Bundle/ns0:books/ns0:author" use="ns0:authorId"/>
  <xsl:template match="/ns0:Order">
    <xsl:copy>
      <xsl:apply-templates select="//ns0:books"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="//ns0:books">
    <xsl:apply-templates select="ns0:book[generate-id(.) = generate-id(key('k', ns0:orderId))]"/>
  </xsl:template>
  <xsl:template match="ns0:book">
    <Bundle>
      <authors>
        <xsl:apply-templates select="key('a', string(key('k', ns0:orderId)/ns0:bookAuthorId ))" />
      </authors>
      <books>
        <xsl:copy-of select="key('k', ns0:orderId)"/>
      </books>
    </Bundle>
  </xsl:template>
  <xsl:template match="ns0:author">
        <xsl:copy-of select="."/>
  </xsl:template> 
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

但是我得到的输出是:

<?xml version = '1.0' encoding = 'UTF-8'?>
<Order xmlns="http://www.book.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.book.org file:/C:/JDeveloper/mywork/SOADevelopment/XsltLearner/SOA/Schemas/BooksOrder.xsd">
   <Bundle>
      <authors/>
      <books>
         <book>
            <orderId>1111</orderId>
            <bookName>Head First Java</bookName>
            <bookAuthorId>100</bookAuthorId>
        </book>
         <book>
            <orderId>1111</orderId>
            <bookName>Hibernate In Action</bookName>
            <bookAuthorId>300</bookAuthorId>
        </book>
      </books>
   </Bundle>
   <Bundle>
      <authors/>
      <books>
         <book>
            <orderId>5555</orderId>
            <bookName>Head First Servlets</bookName>
            <bookAuthorId>200</bookAuthorId>
        </book>
      </books>
   </Bundle>
</Order>

节点未填充。 我尝试使用 copy-of 查看第二个键的输出,但它什么也没打印。 你能否建议我哪里出错了。 知道我做错了什么吗? 请帮忙 谢谢!

【问题讨论】:

【参考方案1】:

据我了解您发布的文档示例,密钥的模式为&lt;xsl:key name="a" match="ns0:Order/ns0:Bundle/ns0:authors/ns0:author" use="ns0:authorId"/&gt;。我还将在任何使用 key 函数时删除 string 调用,例如使用&lt;xsl:apply-templates select="key('a', key('k', ns0:orderId)/ns0:bookAuthorId )"/&gt;

【讨论】:

以上是关于xsl 未转换为所需的输出的主要内容,如果未能解决你的问题,请参考以下文章

将python中的LIST转换为所需的输出

如何将python字典转换为所需的数据框输出? [复制]

如何将此混合行/列表转换为所需的输出。 (Oracle SQL/发行版:Ora12c)

将连续的字符串数据拆分为所需的垂直输出

将列表中的内容拆分为所需的输出

XSL 转换以输出许多嵌入式 XSL 样式表