Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Mixed XML Content In .NET

Status
Not open for further replies.

cash23523

Programmer
Apr 12, 2007
4
I'm fairly new to programming .NET and XML together. What I am doing is trying to create a small web application that reads an XML file. The XML file(s) contain passages from the bible. But the files are mixed content. For example Genesis Chapter 1 appears like:

<verse number="1">
In the
<strongs number="7225">beginning</strongs>
<strongs number="430">God</strongs>
<strongs number="1254">created</strongs>
<strongs number="*853" />
the
<strongs number="8064">heaven</strongs>
and the
<strongs number="776">earth</strongs>
.
</verse>

The strongs child element, attribute number, is a dictionary reference number which I will evetually use to hyperlink each word that can be found in the text to the Hebrew or Greek word origin in the dictionary. However, I cannot come up with anything at the moment to read entirely through the verse element without skipping words such as "the" and "and the" found in the XML above mainly because I am not that familiar with working with XML yet.

Basically what I am trying to is parse a string together with the html tags for each verse before I send it to a string builder and then place it in a label. I would like to be able to read through each verse and basically switch the <strongs><\strongs> with <a href="number goes here"></a> like:

Dim str as String
str = "In the beginning <a href="7225">God</a>......"

I figured there was an easier way to read through the string rather than using something like reader.ReadInnerXML to return the entire verse and then going through and removing, adding, or modifying the <strong> tags with something like the .contains() method.

And help would be appreciated.
 
REMINDER: I'm using .NET for the application.
 
You should device an xsl source file to group all the detail of the transformation. In the .net application, you call transformNode() or transformNodeToObject() method to get the transformation done.

To read the verse, you can do something like this (completely open to refining).
[tt]
<xsl:template match="verse">
<div>
<h2>verse <xsl:value-of select="@number" /></h2>
<xsl:apply-templates select="strongs|text()" />
</div>
</xsl:template>
<xsl:template match="strongs">
<a href="{@number}"><xsl:value-of select="." /><xsl:text>&#x20;</xsl:text></a>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)" /><xsl:text>&#x20;</xsl:text>
</xsl:template>
[/tt]
 
tsuji,
<a href="{@number}">
I thought you needed to use a value-of clause to pull elements and attributes from the source file. So how does the above work? Does it have something to do with the curly braces?

- Dan
 
>Does it have something to do with the curly braces?
It does and it is the shortcut to the expanded form of establishing it.
[tt]
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="@name" />
</xsl:attribute>
<xsl:value-of select="." /><xsl:text>&#x20;</xsl:text>
</xsl:element>
[/tt]
That's what I understand what it is.
 
Dan,

That is called an attribute value template which you may read about here.

Attribute value templates are constrained in the places where you may use them. They are a shortened notation that some find useful. Here is a longer way of doing the same thing:
Code:
<a><xsl:attribute name="href">
    <xsl:value-of select="@number"/>
    </xsl:attribute><xsl:value-of select="." /><xsl:text>&#x20;</xsl:text></a>

My personal prejudice is against using attribute value templates because they tend to cause code maintenance issues. They are not well understood by most practitioners and, at least to my old eyes, they tend to disappear among all the noise characters in an XSLT stylesheet.


Tom Morrison
 
It is a matter of life that every time one uses the awt on a construction in this instance, one can vary it in the expanded format. But to push the syntactic variation to the matter "strong" preference, I would say that it is a bit vain. I had posted "help" to threads in the expanded format and I was presented times and again the argument of conciseness. Now, the exact opposite happens this time and I am sure will not be the last.

If awt is just a device complete replaceable by an eqivalent script, I would not have the intention of adding this note. One way or another, it is based on preference and opinion. But, how about one can present a generic case where awt is absolutely irreplaceable? In that case, we cannot say those team members sitting in the task group are there proposing awt for nothing but a tautology/duplication. To come up with an instance after reflection is not difficult once pointed out. We all have come across it only we don't look at it in this angle.

[1] Suppose we want to make a dynamic element.
[1.1] Suppose it is through a variable.
[tt]
<xsl:template match="strongs">
<xsl:variable name="x">a</xsl:variable>
<xsl:element name="{x}">
<xsl:attribute name="href">
<xsl:value-of select="@name" />
</xsl:attribute>
<xsl:value-of select="." />
</xsl:element>
<xsl:text>&#x20;</xsl:text> <!-- It is better to put the space after the anchor -->
</xsl:template>
[/tt]
In this case, can we replace the awt ({x})? No. The reason simply is that the name attribute is mandatory for xsl:element.

[1.2] Similarly, if we have to use xsl built-in function to set the element name, we encounter the same irreplaceability. Suppose instead of <a> we make <strongs> (which itself can be some wildcard), then it would be this.
[tt]
<xsl:template match="strongs">
<xsl:element name="{local-name(.)}">
<xsl:attribute name="href">
<xsl:value-of select="@name" />
</xsl:attribute>
<xsl:value-of select="." />
</xsl:element>
<xsl:text>&#x20;</xsl:text>
</xsl:template>
[/tt]
Can we replace the awt? Not that I know of to this moment.

[2] The use of curly bracket in the awt is in fact falling inline with what practice is some other scripting language, such as php... So, I would say those members had done their job trying to maintain continuity of notation and not invention for the sake of itself.

I think I have re-present the case enough.
 
The corresponding line should be read like this.
[tt] <xsl:element name="{[red]$[/red]x}">[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top