2012-03-30 53 views
1

考虑XML文件作为如何创建访问性能的XSL文件文件,以便为XML到XML文件转换

<Content> 
    <abc>....</abc> 
</Content> 

我有一个特性与假设

abc=def 

和文件我最终转换XML文件看起来像这transformes第一个XML文件,建议立即进行删除

<Content> 
    <def>....</def> 
</Content> 

所以我的XSL文件d使用上述属性文件并对其进行转换。任何人都可以建议我们如何使用XSLT实现这一目标?

+2

无法单独使用XSLT访问非XML文件。你有什么其他的编程语言?此外,**你有什么尝试?** – Tomalak 2012-03-30 07:04:38

回答

0

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

+1

你的身份模板错了。 – Tomalak 2012-03-30 07:53:14

+0

@ Maestro13:我们不能在Properties.xml文件中使用其他变量(比如'repl1')中的'value'属性,在select中使用类似'$ props/Properties/..'的东西吗? – 2012-04-03 05:20:30

+0

@ Maestro13:你可以尽快给我解决方案。提前致谢。 – 2012-04-03 05:30:51