【问题标题】:XSLT enrich text in XMLXSLT 丰富 XML 中的文本
【发布时间】:2017-06-30 09:17:24
【问题描述】:

我有这个 XML 请求:

 <soapenv:Body>
      <compte>
         <login>Lilian00</login>
         **<password>?</password>**
         <name>Lilian</name>
      </v1:compte>
 </soapenv:Body>

我想用 XSLT 来丰富&lt;password&gt;?&lt;/password&gt; 字段,得到这个结果:

 <soapenv:Body>
      <compte>
         <login>Lilian00</login>
         **<password>admin</password>**
         <name>Lilian</name>
      </v1:compte>
 </soapenv:Body> 

我试试这个,但它不起作用:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    >
    <xsl:param name="password" />


    <xsl:output encoding="UTF-8" indent="yes" />

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

    <xsl:template match="/soapenv:Body/compte/password">
        <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
        <xsl:value-of select="$password" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

谁能帮帮我,我该怎么做?

【问题讨论】:

  • 如果您只是从模板匹配中删除第一个斜杠,它是否有效? IE。做&lt;xsl:template match="soapenv:Body/compte/password"&gt;
  • Thnx @TimC 不,它不起作用,XSLT 文件是否正确?因为我从来没有用过 XSLT
  • 您的 XML 格式不正确:&lt;compte&gt;&lt;/v1:compte&gt; 不匹配。如果compte在命名空间中,那么您的第二个模板将不匹配它。也没有命名空间声明将任一前缀绑定到命名空间 URI。
  • 我删除了命名空间 但它不起作用,我认为我的 XSLT 有问题
  • 您能否编辑您的问题以显示包含所有命名空间声明的 XML 的完整版本? (即 xmlns:soap="..." 和 xmlns:v1="...")。谢谢!

标签: xml xslt xslt-1.0 xslt-2.0


【解决方案1】:

对我来说只需稍作调整即可:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    >
    <xsl:output encoding="UTF-8" indent="yes" />

    <xsl:variable name="password" select="'foo'"/>

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

    <xsl:template match="/soapenv:Body/compte/password">
        <xsl:copy>
            <xsl:apply-templates select="@* | ./*"/>
            <xsl:value-of select="$password" />
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

小提琴:http://xsltransform.net/jxDigU7

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2021-11-14
    • 2011-05-21
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 2021-12-06
    相关资源
    最近更新 更多