简体   繁体   English

用条件语句替换XSL中的文本

[英]Replace text in XSL with conditional statements

I am working with XML files that are generated by a digital video camera. 我正在使用由数码摄像机生成的XML文件。 The camera allows the user to save all of the camera's settngs to an SD card so that the settings can be recalled or loaded into another camera. 摄像机允许用户将摄像机的所有设置保存到SD卡中,以便可以调出设置或将设置加载到另一台摄像机中。 The XSL stylesheet I am writing will allow users to view the camera's settings, as saved to the SD card in a web browser. 我正在编写的XSL样式表将允许用户查看相机的设置,该设置已保存到Web浏览器中的SD卡中。

While most of the values in the XML file -- as formatted by my stylesheet -- make sense to humans, some do not. 虽然XML文件中的大多数值(按我的样式表设置的格式)对人类都是有意义的,但有些则没有意义。 What I would like to do is have the stylesheet display text that is based on the value in the XML file but more easily understood by humans. 我想做的是让样式表显示基于XML文件中的值的文本,但人类更容易理解。

My sample XML file may be viewed here: http://josephthomas.info/Alexa/Setup_120511_140322.xml 我的示例XML文件可以在这里查看: http : //josephthomas.info/Alexa/Setup_120511_140322.xml

A few lines down the page you will see: 在页面下方几行中,您将看到:

Color GAMMA-SxS    Rec_Log

While "Rec_Log" is a value that the cameras understand, it is not a value that the camera's users will understand. 尽管“ Rec_Log”是摄像机可以理解的值,但它不是摄像机用户可以理解的值。 What I would like for the stylesheet to do is to display "LogC" instead. 我想要样式表执行的是改为显示“ LogC”。

In the XML file this value is defined thusly: 因此,在XML文件中定义了该值:

<DteLut lowerLimit="0" upperLimit="2">Rec_Log</DteLut>

The XSL formatting the sample page for this value is: 对该示例页面进行格式化的XSL格式为:

<tr>
  <td class="title_column">Color GAMMA-SxS</td><td><xsl:value-of select="Settings/Groups/Recording/DteLut"/>
  </td>
</tr>

So what I hope to do is have "LogC" displayed on the page rather than Rec_Log. 因此,我希望做的是在页面上显示“ LogC”,而不是Rec_Log。

It seems to me that the "when" conditional statement is the correct approach, but I am not familiar enough with the syntax to cause this to happen. 在我看来,“何时”条件语句是正确的方法,但是我对导致这种情况发生的语法不甚了解。 There are other values in the XML file that want replacing but the above is a good example of my mission. XML文件中还有其他值需要替换,但是以上是我的使命的一个很好的例子。

What you could do is make use of make use of template matching, to match the exceptions to what you want to change. 您可以做的就是利用模板匹配,将异常与您要更改的内容进行匹配。 Firstly, add the following template to your XSL 首先,将以下模板添加到您的XSL中

<xsl:template match="DteLut[. = 'Rec_Log']">
   <xsl:text>LogC</xsl:text>
</xsl:template>

Then, instead of the following line 然后,而不是下面的行

<xsl:value-of select="Settings/Groups/Recording/DteLut"/>

Do the following line 执行以下行

<xsl:apply-templates select="Settings/Groups/Recording/DteLut"/>

When the value of* DteLut * is "Rec_Log", then the custom template will be matched to output "LogC" instead. 当* DteLut *的值为“ Rec_Log”时,自定义模板将与输出“ LogC”匹配。 When there is not a match, the default behaviour will kick in which will be to just output the text value as-is. 如果没有匹配项,则将执行默认行为,即仅按原样输出文本值。

I would use a data-driven approach. 我将使用数据驱动的方法。 Have a mapping file that gives all the translations: 有一个提供所有翻译的映射文件:

<translations>
  <translate from="Rec_log" to="LogC"/>
  <translate .../>
</translations>

then define a key: 然后定义一个键:

<xsl:key name="trans" match="translate" use="@from"/>

and then change 然后改变

<xsl:value-of select="Settings/Groups/Recording/DteLut"/>

to

<xsl:value-of select="key('trans', Settings/Groups/Recording/DteLut,
                          doc('translations.xml'))/@to"/>

if using XSLT 2.0, or 如果使用XSLT 2.0,或者

<xsl:variable name="val" select="Settings/Groups/Recording/DteLut"/>
<xsl:for-each select="document('translations.xml')">
  <xsl:value-of select="key('trans', $val)/@to"/>
</xsl:for-each>

if you're stuck with 1.0. 如果您坚持使用1.0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM