問題描述
我正在嘗試原型轉(zhuǎn)換以將 xsl:schema
轉(zhuǎn)換為 php 界面.我在匹配 xsd:simpleType
元素時遇到了一些麻煩,這些元素的 name
屬性與 xsd:element<的
type
屬性匹配/code> 元素.假設(shè)我有一個這樣的架構(gòu):
I'm trying to prototype a transform to turn xsl:schema
into a php interface. I'm having a little trouble matching xsd:simpleType
elements that have a name
attribute matching the type
attribute of xsd:element
elements. Suppose I have a schema like this:
<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>
我希望得到以下結(jié)果.
<?php
interface Foo {
public abstract function buildFooXmlString(Bar $a);
}
這是我目前擁有的 xslt:
Here's the xslt I have so far:
<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="http://xsd:simpleType[@name=normalize-space()]"/></xsl:template>
<xsl:template match="xsd:simpleType"><xsl:value-of select="local-name()"/> $a</xsl:template>
</xsl:stylesheet>
它產(chǎn)生大部分所需的結(jié)果,但不包括括號內(nèi)的任何內(nèi)容:
It produces most of the desired result, but doesn't include anything inside the parentheses:
<?php
interface Foo {
public abstract function buildFooXmlString();
}
如何選擇name
屬性與元素
的type
匹配的simpleType
節(jié)點?(名稱對于 xsd 類型必須是唯一的.)
How can I select the simpleType
node with the name
attribute matching the type
of the element
? (Names have to be unique for xsd types.)
推薦答案
simpleType
節(jié)點永遠不會被選中,因為在您匹配 xsd:schema
的模板中,您只需要將模板應(yīng)用于 xsd:element
子樹.xsd:simpleType
兄弟永遠不會被處理.
The simpleType
node is never selected because in the template where you match xsd:schema
, you only apply the templates to the xsd:element
child subtree. The xsd:simpleType
sibling will never be processed.
如果你想允許處理一個節(jié)點的所有子節(jié)點,你應(yīng)該在 xsd:schema
中包含一個空的
> 模板.
If you want to allow the processing of all children of a node, you should include an empty <xsl:apply-templates/>
inside the xsd:schema
template.
那仍然不會產(chǎn)生你想要的結(jié)果.它實際上要簡單得多.要生成您期望的代碼片段,您不需要讀取 xsd:simpleType
元素,因為包含您想要的類型的屬性可以直接從 @type使用
xsd:value-of
的 xsd:element
的 code> 屬性,您可以在它之后立即打印 $a
:
That still won't generate the result you want. It's actually much simpler. To generate the code fragment you expect, you don't need to read the xsd:simpleType
element, since the attribute that contains the type you want can be directly obtained from the @type
attribute of xsd:element
using xsd:value-of
, and you can just print the $a
immediately after it:
XmlString(<xsl:value-of select="@type"/> $a)
由于您正在生成文本,因此您應(yīng)該使用 <xsl:text>
元素來控制您的空白的分布方式.例如,如果您使用:
Since you are generating text, you should use the <xsl:text>
elements to control how your whitespace will be distributed. For instance, if you use:
<xsl:template match="/">
<xsl:text><?php </xsl:text>
<xsl:apply-templates select="xsd:schema"/>
</xsl:template>
您無需擔(dān)心總是將 <?php
文本緊跟在 <xsl:template>
之后,并且可以正??s進您的代碼.您還可以包含帶有 

字符的換行符(它不會破壞您的格式):
You won't need to worry about always placing the <?php
text immediately after the <xsl:template>
and can indent your code normally. You can also include newlines with the 

character (and it won't break your formatting):
<xsl:template match="xsd:schema">
<xsl:text>interface FromXsd {
</xsl:text>
<xsl:apply-templates select="xsd:element"/><xsl:text>
</xsl:text>
<xsl:apply-templates select="xsd:annotation"/>
<xsl:text>
}</xsl:text>
</xsl:template>
我編輯了您的 XSL 并在下面的 XSL 文檔中進行了這些更改:
I edited your XSL and made these changes in the XSL document below:
<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="/">
<xsl:text><?php </xsl:text>
<xsl:apply-templates select="xsd:schema"/>
</xsl:template>
<xsl:template match="xsd:schema">
<xsl:text>interface FromXsd {
</xsl:text>
<xsl:apply-templates select="xsd:element"/><xsl:text>
</xsl:text>
<xsl:apply-templates select="xsd:annotation"/>
<xsl:text>
}</xsl:text>
</xsl:template>
<xsl:template match="xsd:element">
<xsl:apply-templates select="xsd:annotation"/>
<xsl:text> abstract public function build</xsl:text>
<xsl:value-of select="normalize-space(@name)"/>
<xsl:text>XmlString(</xsl:text>
<xsl:value-of select="@type"/><xsl:text> $a</xsl:text>
<xsl:text>);</xsl:text>
</xsl:template>
<xsl:template match="xsd:annotation">
<xsl:text> /* </xsl:text>
<xsl:value-of select="normalize-space(.)"/>
<xsl:text> */</xsl:text>
</xsl:template>
</xsl:stylesheet>
如果您有一個輸入,例如:
If you have an input such as:
<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:annotation>
<xsd:documentation>This is a comment</xsd:documentation>
</xsd:annotation>
</xsd:schema>
結(jié)果如下:
<?php interface FromXsd {
abstract public function buildFooXmlString(Bar $a);
/* This is a comment */
}
這篇關(guān)于匹配具有動態(tài)屬性值的元素的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!