2012-05-03 23 views
6

Tôi đang cố gắng làm điều gì đó có vẻ như nó rất đơn giản, nhưng tôi không thể làm việc đó, và dường như tôi không tìm thấy bất kỳ ví dụ nào không liên quan đến rất nhiều thứ không liên quan. Tôi muốn cập nhật nội dung văn bản của thẻ xml cụ thể thành một giá trị cụ thể (được thông qua dưới dạng tham số, XSLT này sẽ được sử dụng từ kiến). Một ví dụ đơn giản:Cập nhật văn bản của một phần tử với XSLT dựa trên thông số

Tôi muốn chuyển đổi

<foo> 
    <bar> 
    baz 
    </bar> 
</foo> 

Để

<foo> 
    <bar> 
     something different 
    </bar> 
</foo> 

Đây là kiểu mà tôi cố gắng, mà kết quả chỉ trong các thẻ, không có văn bản nào cả

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <!-- identity transformation, to keep everything unchanged except for the stuff we want to change --> 
    <!-- Whenever you match any node or any attribute --> 
    <xsl:template match="node()|@*"> 
     <!-- Copy the current node --> 
     <xsl:copy> 
      <!-- Including any attributes it has and any child nodes --> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <!-- change the text of the bar node, in the real template the value won't be specified inline --> 
    <xsl:template match="/foo/bar/"> 
     <xsl:param name="baz" value="something different"/> 
      <xsl:value-of select="$baz"/> 
    </xsl:template> 
</xsl:stylesheet> 

Cảm ơn trước!

Trả lời

10

Có một số vấn đề với mã được cung cấp mà dẫn đến thời gian biên dịch lỗi:

<xsl:template match="/foo/bar/"> 
    <xsl:param name="baz" value="something different"/> 
     <xsl:value-of select="$baz"/> 
</xsl:template> 
  1. Các mô hình phù hợp với quy định trên mẫu này là cú pháp bất hợp pháp - một biểu thức XPath không thể kết thúc bằng ký tự /.

  2. xsl:param không thể có một thuộc tính không rõ như value

Giải pháp:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:param name="pReplacement" select="'Something Different'"/> 

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

<xsl:template match="foo/bar/text()"> 
    <xsl:value-of select="$pReplacement"/> 
</xsl:template> 
</xsl:stylesheet> 

Khi chuyển đổi này được áp dụng trên các tài liệu XML cung cấp:

<foo> 
    <bar> 
    baz 
    </bar> 
</foo> 

các truy nã, kết quả chính xác được sản xuất:

<foo> 
    <bar>Something Different</bar> 
</foo> 
0
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 
<xsl:output indent="yes" encoding="UTF-8" method="xml"/> 
<xsl:param name="param-from-ant" as="xs:string"/> 

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

<xsl:template match="/foo/bar"> 
<xsl:value-of select="$param-from-ant"/> 
</xsl:template> 
</xsl:stylesheet> 
+0

Tôi cố gắng đó (cả trong ví dụ hơi phức tạp hơn tôi, và trên [ ví dụ cơ sở] (http://www.xsltcake.com/slices/Y8fojh/4)), nhưng dường như không hoạt động? Tôi nhận được một thẻ foo trống. Tôi đã sao chép một cái gì đó sai? – user1126518

+0

Sean, "mã XSLT" được cung cấp gây ra lỗi thời gian biên dịch với bất kỳ bộ xử lý XSLT 1.0 tuân thủ nào. –

0

Một cách tiếp cận hơi khác nhau:

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

    <xsl:param name="replace"/> 

    <xsl:template match="node()|@*"> 
    <xsl:choose> 
     <xsl:when test="text() and name(..)='bar' and name(../..)='foo'"> 
     <xsl:value-of select="$replace"/> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

Các vấn đề liên quan