【发布时间】:2014-06-22 11:31:47
【问题描述】:
我正在尝试原型转换以将xsl:schema 转换为 php 接口。我在匹配具有与xsd:element 元素的type 属性匹配的name 属性的xsd:simpleType 元素时遇到了一些问题。假设我有这样的架构:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Foo" type="Bar"/>
<xsd:simpleType name="Bar">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="32"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
我希望得到以下结果。
<?php
interface Foo {
public abstract function buildFooXmlString(Bar $a);
}
这是我目前拥有的 xslt:
<xsl:stylesheet version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="*"/>
<xsl:template match="/"><?php <xsl:apply-templates select="xsd:schema"/></xsl:template>
<xsl:template match="xsd:schema">interface FromXsd { <xsl:apply-templates select="xsd:element"/> }</xsl:template>
<xsl:template match="xsd:element">
<xsl:apply-templates select="xsd:annotation"/>
abstract public function build<xsl:value-of select="normalize-space(@name)"/>XmlString(<xsl:apply-templates select="@type"/>);
</xsl:template>
<xsl:template match="xsd:annotation">/* <xsl:value-of select="normalize-space()"/> */</xsl:template>
<xsl:template match="@type"><xsl:apply-templates select="//xsd:simpleType[@name=normalize-space()]"/></xsl:template>
<xsl:template match="xsd:simpleType"><xsl:value-of select="local-name()"/> $a</xsl:template>
</xsl:stylesheet>
它产生了大部分期望的结果,但不包括括号内的任何内容:
<?php
interface Foo {
public abstract function buildFooXmlString();
}
如何选择name 属性与element 的type 匹配的simpleType 节点? (对于 xsd 类型,名称必须是唯一的。)
【问题讨论】: