使用XSLT维护XML标识,通过输入文本替换值所有属性XML

时间:2016-06-15 10:20:10

标签: xml xslt

从这个示例XML:

<?**xml** version='*1.0*' encoding='*UTF-8*' ?>
<**config**>
  <**Connections**>
    <**DbMetaData** tablePrefix="*example.*"/>
    <**AGSConnection**
                  outputDirectory="*C:\directory2\directories\isoutput*"                  virtualOutputDirectory="*https://directory1.example1.net/ex/rest/output*"/>
    <**SDEConnection**
                  server="*srv01*" 
                  instance="*sde:sqlserver:srv01*" 
                  database="*DBex*" 
                  authenticationMode="*DBMS*"
                  user="*user1*" 
                  password="*pass123*" 
                  version="*sde.DEFAULT*"
                  sdeConnectionPath="*C:\\tmp\\ex\\conexion\\ser01.sde*"/>
 </**Connections**>
 </**config**>

使用XSLT,用可编辑的文本字段替换所有属性值,维护缩进(XML结构)。我怎么能得到这样的东西?:

final result in browser

感谢,

2 个答案:

答案 0 :(得分:0)

让我试着猜猜你的意思:你只想用“一些输入”替换属性值。

在xslt中,您可以通过使用document()函数读取的单独xml文档传递“some input”,也可以使用参数将值传递到样式表中:(修改后的身份转换为待替换属性的额外模板)

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>

<xsl:param name="tablePrefix" select="''"/>
<xsl:param name="outputDirectory" select="''"/>
<xsl:param name="virtualOutputDirectory" select="''"/>
<xsl:param name="server" select="''"/>
<xsl:param name="instance" select="''"/>
<xsl:param name="database" select="''"/>
<xsl:param name="authenticationMode" select="''"/>
<xsl:param name="user" select="''"/>
<xsl:param name="password" select="''"/>
<xsl:param name="version" select="''"/>
<xsl:param name="sdeConnectionPath"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>  
    </xsl:template>

    <xsl:template match="@tablePrefix">
        <xsl:choose>
            <xsl:when test="$tablePrefix != ''">
                <xsl:attribute name="tablePrefix">
                    <xsl:value-of select="$tablePrefix"/>
                </xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- repeat for each of the attributes replacing all "tablePrefix" by the attribute -->

</xsl:stylesheet>

你如何通过params取决于你的xslt处理器,例如使用xsltproc的--stringparam name value或使用xalanj的-param name value

答案 1 :(得分:0)

XML解析器并不告诉XSLT处理器属性之间有多少空格,因此一旦数据在XSLT中,这些信息就会丢失。您可以使用<xsl:output indent='yes'/>让XSLT处理器对输出进行自己的缩进,但它不一定与原始缩进相同。 (对于它的价值,输入中的缩进,如果我们忽略奇怪的星号,看起来好像是使用Saxon制作的。)