【问题标题】:Creating XSL for multiple XML with different element为具有不同元素的多个 XML 创建 XSL
【发布时间】:2014-03-21 20:19:02
【问题描述】:

我一直在寻找为多个 XML 创建一个 XSL。下面是示例 XML,它是众多 XML 之一。不同之处在于<ItemRequest> 代替<m.Currency> 还有其他元素。我只需要显示应该与所有 XML 兼容的 ItemRequest

<?xml version="1.0" standalone="yes" ?>
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl" ?>
<ServiceRequest xmlns:m="urn:messages.service.com"
    xmlns="urn:control.services.com" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <RequestHeader>
        <Service>Blah</Service>
        <Operation>Blah1</Operation>
    </RequestHeader>
    <m:ItemRequest>
        <ServiceRequest xmlns="urn:control.com" xmlns:m="urn:messages.service.com" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <RequestHeader>
                <Service>Blah</Service>
                <Operation>Blah2</Operation>
            </RequestHeader>
            <m:Currency>
                <m:MaintType>a</m:MaintType>
                <m:BackOffice>b</m:BackOffice>
                <m:Code>c</m:Code>
                <m:Number>%00</m:Number>
                <m:EditCode>1</m:EditCode>
            </m:Currency>
        </ServiceRequest>
    </m:ItemRequest>
</ServiceRequest>  

输出:类似于

<h1>Operation: Currency</h1>

<table>
    <tr>
        <td>MainType</td>
        <td>BackOffice</td>
        <td>Code</td>
        <td>Number</td>
        <td>EditCode</td>
    </tr>
    <tr>
        <td>a</td>
        <td>b</td>
        <td>c</td>
        <td>%00</td>
        <td>1</td>
    </tr>
</table>

【问题讨论】:

  • 您的 xml 格式不正确。您的序言应该是&lt;?xml version="1.0" encoding="UTF-8"?&gt;,并且您的样式表处理说明不完整。
  • 对不起。我是stackoverflow的新手。我确实格式化了xml代码。正如你提到的prolog和上面一样。
  • 您确定可以在m:ItemRequest 中包含ServiceRequest 吗?您是否有一个 XSD 来描述您可能作为输入的不同 XML?
  • 您还有两个不同的默认命名空间:urn:control.services.com 用于外部ServiceRequest 中的元素,urn:control.com 用于内部ServiceRequest 中的无前缀元素。对吗?

标签: xml xslt xpath


【解决方案1】:

由于您在 XML 源的不同部分有两个默认命名空间,我将每个命名空间映射到不同的前缀:ns1 用于外部命名空间,ns2 用于内部命名空间。我使用了忽略部分节点层次结构的 XPath 表达式。这适用于您发布的输入 XML(从 m:Currency 提取数据):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:ns1="urn:control.services.com" 
    xmlns:ns2="urn:control.com"
    xmlns:m="urn:messages.service.com" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output indent="yes" method="html"/>

    <xsl:template match="/">
        <html>
            <body>
                <h1><xsl:value-of select="ns1:ServiceRequest/m:ItemRequest//ns2:Operation"/></h1>
                <xsl:apply-templates select="ns1:ServiceRequest/m:ItemRequest//m:Currency"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="m:Currency">
        <table>
            <tr><xsl:apply-templates select="*" mode="headers"/></tr>
            <tr><xsl:apply-templates select="*" mode="data"/></tr>
        </table>
    </xsl:template>

    <xsl:template match="m:Currency/*" mode="headers">
        <td><xsl:value-of select="substring-after(name(.), ':')"/></td>
    </xsl:template>

    <xsl:template match="m:Currency/*" mode="data">
        <td><xsl:value-of select="."/></td>
    </xsl:template>

</xsl:stylesheet>

你说m:Currency只是一个例子,你还有其他元素。此 XSL 仅适用于该示例。如果您向您的问题添加描述您接受的其他输入的 XML 模式,则可以为其他场景编写样式表。

【讨论】:

  • 大约有 20 个 xml,其结构始终相同,但不是货币,而是地址类型、国家、实体类型等。您能否建议是否可以使用某种变量来代替该元素??
  • 我尝试通过更改 "m:*" 来运行我所有的 XML,并且成功了...非常感谢
猜你喜欢
  • 2019-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
相关资源
最近更新 更多