如果存儲屬性文件的XML格式,例如
<Properties>
<Property value="abc">def</Property>
<Property value="...">...</Property>
</Properties>
那麼你可以使用XSLT同時處理XML文件,並用高清的人代替ABC元素,等等。
例如
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="props" select="document('Properties.xml')"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="Content/*">
<xsl:variable name="repl" select="$props/Properties/Property[@value=name(current())]"/>
<xsl:choose>
<xsl:when test="$repl">
<xsl:element name="{$repl}">
<xsl:apply-templates/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
當施加到
<?xml version="1.0" encoding="UTF-8"?>
<Content>
<abc>xxx</abc>
<def>zzz</def>
<ghi>ccc</ghi>
</Content>
與屬性文件
<?xml version="1.0" encoding="UTF-8"?>
<Properties>
<Property value="abc">def</Property>
<Property value="def">www</Property>
</Properties>
這導致
<?xml version="1.0" encoding="UTF-8"?>
<Content>
<def>xxx</def>
<www>zzz</www>
<ghi>ccc</ghi>
</Content>
加成由傑文
------------------------------------------------------------------------------------
My question is that if i want to access
'value' attribute(input xml file tags)
ex:
<Property value="abc">def</Property>
我想「ABC」,以在一些其它變量properties.xml文件訪問說「repl1」,使用類似「$道具/屬性/ ..」的選擇? 這怎麼能實現。
回答Maestro13
還不清楚你想要做的事情,所以我只會給你一些提示,這是希望有用。
value屬性可以通過Xpath表達式來訪問,包括/@value
。你當然需要有一個當前的Property節點。這可以通過任一做一個循環如下進行:
<xsl:for-each select="$props/Properties/Property">
<w><xsl:value-of select="@value"/></w>
</xsl:for-each>
在環路內的當前節點是一個環繞在,和@value
作用在該節點上。
或者你也可以定義一個模板做同樣的事情(一個模板,除非命名和直接調用,將被所有匹配的節點重複調用)。
另一種方式來檢索屬性的值爲首先通過選擇第n發生具有XPath表達式,其選擇恰好一個Property元素,例如如下:
<xsl:value-of select="$props/Properties/Property[3]/@value"/>
在上述屬性的情況下文件,此將返回ghi
。
無法單獨使用XSLT訪問非XML文件。你有什麼其他的編程語言?此外,**你有什麼嘗試?** – Tomalak 2012-03-30 07:04:38