2010-07-21 118 views
0

现在我有如下的XML文件:复制在XSLT属性

<DM Name="A DM"> 
    <DV id="SQL:Select something from db" Name="DV 1"> 
    <Sample aid="SQL:Select something from db" /> 
    </DV> 
    <DV id="SQL:Select something from db" Name="DV 2"> 
    <Sample aid="SQL:Select something from db" name ="DC"> 
     good 
    </Sample> 
    </DV> 
</DM> 

我想使用XSLT转换它,就在这个tamplet,以确定哪些DV应该转变一个参数:如果参数($ dvIndex = 0),然后只保留所有的元素和属性,只是将attritributes转换为以“SQL:”开头的值,if($ dvindext> 0),只转换特定的DV,其他DV)。现在我编写如下的XSLT,但它错过了DM的属性,我不知道如何复制DM的属性。我不知道是否有更好的解决方案。 XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
    xmlns:user="urn:my-scripts" 
> 
    <xsl:output method="xml" indent="yes"/> 
    <msxsl:script language="C#" implements-prefix="user"> 
    <![CDATA[ 
    public string UpperCase(string value){ 
     return value.ToUpper(); 
    } 
     ]]> 
    </msxsl:script> 

    <xsl:param name="dvIndex" select="2" /> 

    <xsl:template match="DM" > 
    <xsl:copy> 
     <xsl:choose> 
     <xsl:when test="$dvIndex > 0"> 
      <xsl:apply-templates select="DV[$dvIndex]"/> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
      </xsl:copy> 
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <!--[starts-with(translate(substring(.,1,4),'SQL:','sql:'),'sql:')]--> 
    <xsl:template match="@*[user:UpperCase(substring(.,1,4))='SQL:']"> 
    <xsl:attribute name="{name()}"> 
     <xsl:value-of select="'parsedSQL'"/> 
    </xsl:attribute> 
    </xsl:template> 
</xsl:stylesheet> 

这个问题也涉及到我的问题2#(How to only convert an XML file's attribute using XSLT, and leave the other content?

非常感谢!

+0

请添加到您的发布一些期望输出的例子。 – 2010-07-21 15:49:51

+1

请尝试选择“@ * | DV [$ dvIndex]”。它解决了吗? – 2010-07-21 16:42:39

回答

0

以下可能会做你所需要的。请注意额外的<xsl:apply-templates select="@*" />复制属性。

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
    xmlns:user="urn:my-scripts"> 

    <xsl:output method="xml" indent="yes"/> 
    <msxsl:script language="C#" implements-prefix="user"> 
    <![CDATA[ 
    public string UpperCase(string value){ 
     return value.ToUpper(); 
    } ]]> 
    </msxsl:script> 

    <xsl:param name="dvIndex" select="0" /> 

    <xsl:template match="DM" > 
    <xsl:copy> 
     <xsl:apply-templates select="@*" /> 
     <xsl:choose> 
     <xsl:when test="$dvIndex > 0"> 
      <xsl:apply-templates select="DV[$dvIndex]"/> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:apply-templates select="DV"/> 
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <!--[starts-with(translate(substring(.,1,4),'SQL:','sql:'),'sql:')]--> 
    <xsl:template match="@*[user:UpperCase(substring(.,1,4))='SQL:']"> 
    <xsl:attribute name="{name()}"> 
     <xsl:value-of select="'parsedSQL'"/> 
    </xsl:attribute> 
    </xsl:template> 
</xsl:stylesheet> 
1

这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:param name="dvIndex" select="2" /> 
    <xsl:template match="DM" > 
     <xsl:copy> 
      <xsl:apply-templates select="@*|DV[$dvIndex]"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="@*[starts-with(translate(substring(.,1,4),'SQL:','sql:'),'sql:')]"> 
     <xsl:attribute name="{name()}"> 
      <xsl:value-of select="'parsedSQL'"/> 
     </xsl:attribute> 
    </xsl:template> 
</xsl:stylesheet> 

结果:

<DM Name="A DM"> 
<DV id="parsedSQL" Name="DV 2"> 
<Sample aid="parsedSQL" name="DC"> 
     good 
    </Sample> 
</DV> 
</DM> 

随着PARAM dvIndex0

<DM Name="A DM"></DM> 

注意:避免使用脚本:它不是标准脚本,每次使用时都会强制加载脚本引擎。

编辑:如果你要处理每DV当$ dvIndex是0,那么这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:param name="dvIndex" select="2" /> 
    <xsl:template match="DM" > 
     <xsl:copy> 
      <xsl:apply-templates select="@*|DV[$dvIndex]|DV[not($dvIndex)]"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="@*[starts-with(translate(substring(.,1,4),'SQL:','sql:'),'sql:')]"> 
     <xsl:attribute name="{name()}"> 
      <xsl:value-of select="'parsedSQL'"/> 
     </xsl:attribute> 
    </xsl:template> 
</xsl:stylesheet> 

随着$ dvIndex是0,输出:

<DM Name="A DM"> 
<DV id="parsedSQL" Name="DV 1"> 
<Sample aid="parsedSQL"></Sample> 
</DV> 
<DV id="parsedSQL" Name="DV 2"> 
<Sample aid="parsedSQL" name="DC"> 
     good 
    </Sample> 
</DV> 
</DM> 
+0

这很符合我的期望,非常感谢。 – Leonard 2010-07-22 02:46:36