久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

匹配具有動態(tài)屬性值的元素

Match an element with a dynamic attribute value(匹配具有動態(tài)屬性值的元素)
本文介紹了匹配具有動態(tài)屬性值的元素的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試原型轉(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="/">&lt;?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 元素,因為包含您想要的類型的屬性可以直接從 @typexsd:value-ofxsd: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>&lt;?php </xsl:text>
    <xsl:apply-templates select="xsd:schema"/>
</xsl:template>

您無需擔(dān)心總是將 &lt;?php 文本緊跟在 <xsl:template> 之后,并且可以正??s進您的代碼.您還可以包含帶有 &#xa; 字符的換行符(它不會破壞您的格式):

You won't need to worry about always placing the &lt;?php text immediately after the <xsl:template> and can indent your code normally. You can also include newlines with the &#xa; character (and it won't break your formatting):

<xsl:template match="xsd:schema">
    <xsl:text>interface FromXsd {&#xa;</xsl:text>
    <xsl:apply-templates select="xsd:element"/><xsl:text>&#xa;</xsl:text>
    <xsl:apply-templates select="xsd:annotation"/>
    <xsl:text>&#xa;}</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>&lt;?php </xsl:text>
        <xsl:apply-templates select="xsd:schema"/>
    </xsl:template>

    <xsl:template match="xsd:schema">
        <xsl:text>interface FromXsd {&#xa;</xsl:text>
        <xsl:apply-templates select="xsd:element"/><xsl:text>&#xa;</xsl:text>
        <xsl:apply-templates select="xsd:annotation"/>
        <xsl:text>&#xa;}</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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 個表)
How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 設(shè)置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數(shù)組自動填充選擇框)
PHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 從 MSSQL-SELECT 產(chǎn)生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 国产免费一区二区 | 国产一区二区免费电影 | 99久久国产 | m豆传媒在线链接观看 | 亚洲视频在线播放 | 欧美日韩一 | 日韩成人在线网站 | 日本一区二区视频 | 一级黄色播放 | 久久999| 亚洲成人一区 | 久久精品超碰 | 国产高清免费视频 | 中文字幕不卡在线观看 | 精品欧美一区二区三区 | 99久久婷婷国产综合精品电影 | 国产成在线观看免费视频 | 国产精品二区三区在线观看 | 欧美高清视频在线观看 | 在线视频91 | 国产福利资源 | 在线播放国产一区二区三区 | 国产一区91精品张津瑜 | 亚洲综合首页 | 99福利视频 | 国产日韩欧美一区 | 欧美日韩在线一区 | 午夜免费影视 | 中文字幕 亚洲一区 | 91高清免费观看 | 日韩一二区| 欧美一级片在线观看 | 国产免费观看久久黄av片涩av | 美女福利网站 | 久久在线 | 亚洲精品久久久一区二区三区 | 日本a视频 | 9191av| 成人免费一区二区三区视频网站 | 国产福利在线播放 | 97精品国产97久久久久久免费 |