如果存储属性文件的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