文本属性作为另一个元素的值

发布于 2024-07-10 04:29:48 字数 604 浏览 8 评论 0原文

对于下面的 XML,我想知道如何获取 case_id 节点中的文本值作为下面 xsl 工作表中隐藏输入标记的属性。 这可能吗?

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="data.xsl"?>
<NewDataSet>
<Cases>
<Case>
<case_id>30</case_id>
...
...
</Case>
</Cases>
</NewDataset>
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<input type="hidden" name="case_num" value="?"/>
</xsl:template>
</xsl:stylesheet>

With the XML below, I would like to know how to get the value of text in the case_id node as an attribute for the hidden input tag in the xsl sheet below.
Is this possible?

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="data.xsl"?>
<NewDataSet>
<Cases>
<Case>
<case_id>30</case_id>
...
...
</Case>
</Cases>
</NewDataset>
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<input type="hidden" name="case_num" value="?"/>
</xsl:template>
</xsl:stylesheet>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

巴黎盛开的樱花 2024-07-17 04:29:48

试试这个

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <input type="hidden" name="case_num">
      <xsl:attribute name="value">
        <xsl:value-of select="/NewDataSet/Cases/Case/case_id/text()"/>
      </xsl:attribute>
    </input>
  </xsl:template>
</xsl:stylesheet>

或者你可以像这样内联

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <input type="hidden" name="case_num" value="{/NewDataSet/Cases/Case/case_id/text()}"/>
  </xsl:template>

Try this

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <input type="hidden" name="case_num">
      <xsl:attribute name="value">
        <xsl:value-of select="/NewDataSet/Cases/Case/case_id/text()"/>
      </xsl:attribute>
    </input>
  </xsl:template>
</xsl:stylesheet>

or you can inline like this

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <input type="hidden" name="case_num" value="{/NewDataSet/Cases/Case/case_id/text()}"/>
  </xsl:template>

小忆控 2024-07-17 04:29:48

只需将您的 XSLT 更改为此,假设您只有 1 个 case_id,否则,您将需要使用更具体的模板匹配,并删除我用作示例的 XPATH 值中的一些路径。

<input type="hidden" name="case-num">
    <xsl:attribute name="value">
        <xsl:value-of select="/NewDataSet/Cases/Case/case_id" />
    </xsl:attribute>
</input>

Just change your XSLT to this, this assumes that you do only have 1 case_id, otherwise, you will need to go with a more specific template match, and remove some of the path in the XPATH value I used as an example.

<input type="hidden" name="case-num">
    <xsl:attribute name="value">
        <xsl:value-of select="/NewDataSet/Cases/Case/case_id" />
    </xsl:attribute>
</input>
清眉祭 2024-07-17 04:29:48

您需要使用一些更具体的匹配来扩展 XSLT。

以下内容应输出包含每个 Casecase_id 值的 input 元素。 我假设每个 Case 有一个 case_id。 我已尝试使 XSLT 尽可能明确,但如果您不愿意,则无​​需在实现中如此精确。

 <xsl:template match="/">
   <xsl:apply-templates />
 </xsl:template>

 <xsl:template match="Case">
    <xsl:element name="input">
       <xsl:attribute name="type">
          <xsl:text>hidden</xsl:text>
       </xsl:attribute>
       <xsl:attribute name="name">
          <xsl:text>case_num</xsl:text>
       </xsl:attribute>
       <xsl:attribute name="value">
          <xsl:value-of select="case_id"/>
       </xsl:attribute>
    </xsl:element>
 </xsl:template>

You need to expand your XSLT with some more specific matches.

The following should output input elements that contain your case_id values for each Case. I've assumed there is one case_id per Case. I've tried to make the XSLT as explicit as I can, but you don't need to be this exact in your implementation if you don't want to be.

 <xsl:template match="/">
   <xsl:apply-templates />
 </xsl:template>

 <xsl:template match="Case">
    <xsl:element name="input">
       <xsl:attribute name="type">
          <xsl:text>hidden</xsl:text>
       </xsl:attribute>
       <xsl:attribute name="name">
          <xsl:text>case_num</xsl:text>
       </xsl:attribute>
       <xsl:attribute name="value">
          <xsl:value-of select="case_id"/>
       </xsl:attribute>
    </xsl:element>
 </xsl:template>
柠檬心 2024-07-17 04:29:48

只需使用 AVT(属性值模板)像这样:

<input type="hidden" name="case_num" value="{*/*/*/case_id}"/>

Just use an AVT (Attribute Value Template) like this:

<input type="hidden" name="case_num" value="{*/*/*/case_id}"/>
夏了南城 2024-07-17 04:29:48

我将其更改为:

<input type="hidden" name="case-num">
    <xsl:attribute name="value">
        <xsl:value-of select="case_id" />
    </xsl:attribute>
</input>

因为它位于 foreach 循环中。
谢谢你们,这真是一种享受!

I changed it to:

<input type="hidden" name="case-num">
    <xsl:attribute name="value">
        <xsl:value-of select="case_id" />
    </xsl:attribute>
</input>

as it's in a foreach loop.
Thanks guys, that worked a treat!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文